Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ var whiteListed = []string{
"compliance/tests/filters.json",
"compliance/tests/functions.json",
"compliance/tests/function_group_by.json",
"compliance/tests/function_let.json",
// "compliance/tests/function_let.json",
"compliance/tests/function_strings.json",
"compliance/tests/identifiers.json",
"compliance/tests/indices.json",
"compliance/tests/lexical_scoping.json",
// "compliance/tests/lexical_scoping.json",
"compliance/tests/literal.json",
"compliance/tests/multiselect.json",
"compliance/tests/ormatch.json",
Expand Down
5 changes: 0 additions & 5 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ func TestSearch(t *testing.T) {
expression: "not a valid expression",
},
wantErr: true,
}, {
args: args{
expression: "let({root: @}, &root).root",
data: map[string]interface{}{},
},
}, {
args: args{
expression: "sort_by(@, &@ *`-1.0`)",
Expand Down
7 changes: 0 additions & 7 deletions pkg/functions/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,6 @@ func GetDefaultFunctions() []FunctionEntry {
{Types: []JpType{JpString}},
},
Handler: jpfLower,
}, {
Name: "let",
Arguments: []ArgSpec{
{Types: []JpType{JpObject}},
{Types: []JpType{JpExpref}},
},
Handler: jpfLet,
}, {
Name: "map",
Arguments: []ArgSpec{
Expand Down
30 changes: 10 additions & 20 deletions pkg/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

type (
JpFunction = func([]interface{}) (interface{}, error)
ExpRef = func(interface{}, map[string]interface{}) (interface{}, error)
ExpRef = func(interface{}) (interface{}, error)
JpType string
)

Expand Down Expand Up @@ -230,7 +230,7 @@ func jpfGroupBy(arguments []interface{}) (interface{}, error) {
}
groups := map[string]interface{}{}
for _, element := range arr {
spec, err := exp(element, nil)
spec, err := exp(element)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -294,22 +294,12 @@ func jpfLower(arguments []interface{}) (interface{}, error) {
return strings.ToLower(arguments[0].(string)), nil
}

func jpfLet(arguments []interface{}) (interface{}, error) {
scope := arguments[0].(map[string]interface{})
exp := arguments[1].(ExpRef)
result, err := exp(nil, scope)
if err != nil {
return nil, err
}
return result, nil
}

func jpfMap(arguments []interface{}) (interface{}, error) {
exp := arguments[0].(ExpRef)
arr := arguments[1].([]interface{})
mapped := make([]interface{}, 0, len(arr))
for _, value := range arr {
current, err := exp(value, nil)
current, err := exp(value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -359,7 +349,7 @@ func jpfMaxBy(arguments []interface{}) (interface{}, error) {
} else if len(arr) == 1 {
return arr[0], nil
}
start, err := exp(arr[0], nil)
start, err := exp(arr[0])
if err != nil {
return nil, err
}
Expand All @@ -368,7 +358,7 @@ func jpfMaxBy(arguments []interface{}) (interface{}, error) {
bestVal := t
bestItem := arr[0]
for _, item := range arr[1:] {
result, err := exp(item, nil)
result, err := exp(item)
if err != nil {
return nil, err
}
Expand All @@ -386,7 +376,7 @@ func jpfMaxBy(arguments []interface{}) (interface{}, error) {
bestVal := t
bestItem := arr[0]
for _, item := range arr[1:] {
result, err := exp(item, nil)
result, err := exp(item)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -456,15 +446,15 @@ func jpfMinBy(arguments []interface{}) (interface{}, error) {
} else if len(arr) == 1 {
return arr[0], nil
}
start, err := exp(arr[0], nil)
start, err := exp(arr[0])
if err != nil {
return nil, err
}
if t, ok := start.(float64); ok {
bestVal := t
bestItem := arr[0]
for _, item := range arr[1:] {
result, err := exp(item, nil)
result, err := exp(item)
if err != nil {
return nil, err
}
Expand All @@ -482,7 +472,7 @@ func jpfMinBy(arguments []interface{}) (interface{}, error) {
bestVal := t
bestItem := arr[0]
for _, item := range arr[1:] {
result, err := exp(item, nil)
result, err := exp(item)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -617,7 +607,7 @@ func jpfSortBy(arguments []interface{}) (interface{}, error) {
}
var sortKeys []interface{}
for _, item := range arr {
if value, err := exp(item, nil); err != nil {
if value, err := exp(item); err != nil {
return nil, err
} else {
sortKeys = append(sortKeys, value)
Expand Down
4 changes: 2 additions & 2 deletions pkg/interpreter/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type FunctionCaller interface {
CallFunction(string, []interface{}, Interpreter) (interface{}, error)
CallFunction(string, []interface{}) (interface{}, error)
}

type functionEntry struct {
Expand Down Expand Up @@ -146,7 +146,7 @@ func typeCheck(a functions.ArgSpec, arg interface{}) error {
return fmt.Errorf("invalid type for: %v, expected: %#v", arg, a.Types)
}

func (f *functionCaller) CallFunction(name string, arguments []interface{}, intr Interpreter) (interface{}, error) {
func (f *functionCaller) CallFunction(name string, arguments []interface{}) (interface{}, error) {
entry, ok := f.functionTable[name]
if !ok {
return nil, errors.New("unknown function: " + name)
Expand Down
21 changes: 2 additions & 19 deletions pkg/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,20 @@ This is a tree based interpreter. It walks the AST and directly
*/
type Interpreter interface {
Execute(parsing.ASTNode, interface{}) (interface{}, error)
WithScope(map[string]interface{}) Interpreter
}

type treeInterpreter struct {
caller FunctionCaller
scope Scope
root interface{}
}

func NewInterpreter(data interface{}, caller FunctionCaller) Interpreter {
return &treeInterpreter{
caller: caller,
scope: newScope(nil),
root: data,
}
}

func (intr *treeInterpreter) WithScope(data map[string]interface{}) Interpreter {
return &treeInterpreter{
caller: intr.caller,
scope: intr.scope.With(data),
root: intr.root,
}
}

// Execute takes an ASTNode and input data and interprets the AST directly.
// It will produce the result of applying the JMESPath expression associated
// with the ASTNode to the input data "value".
Expand Down Expand Up @@ -130,10 +119,7 @@ func (intr *treeInterpreter) Execute(node parsing.ASTNode, value interface{}) (i
return leftNum <= rightNum, nil
}
case parsing.ASTExpRef:
return func(data interface{}, scope map[string]interface{}) (interface{}, error) {
if scope != nil {
return intr.WithScope(scope).Execute(node.Children[0], value)
}
return func(data interface{}) (interface{}, error) {
return intr.Execute(node.Children[0], data)
}, nil
case parsing.ASTFunctionExpression:
Expand All @@ -145,7 +131,7 @@ func (intr *treeInterpreter) Execute(node parsing.ASTNode, value interface{}) (i
}
resolvedArgs = append(resolvedArgs, current)
}
return intr.caller.CallFunction(node.Value.(string), resolvedArgs, intr)
return intr.caller.CallFunction(node.Value.(string), resolvedArgs)
case parsing.ASTField:
key := node.Value.(string)
var result interface{}
Expand All @@ -159,9 +145,6 @@ func (intr *treeInterpreter) Execute(node parsing.ASTNode, value interface{}) (i
if result != nil {
return result, nil
}
if result, ok := intr.scope.GetValue(key); ok {
return result, nil
}
return nil, nil
case parsing.ASTFilterProjection:
left, err := intr.Execute(node.Children[0], value)
Expand Down
37 changes: 0 additions & 37 deletions pkg/interpreter/scopes.go

This file was deleted.

52 changes: 0 additions & 52 deletions pkg/interpreter/scopes_test.go

This file was deleted.