Skip to content

Commit

Permalink
Feature/eval (#10)
Browse files Browse the repository at this point in the history
* add eval functionality

* a more fancy eval

* fixes

* fully fleshed out eval

* update

---------

Co-authored-by: tbal999 <[email protected]>
  • Loading branch information
tbal999 and tbal999 authored Mar 15, 2023
1 parent 5befb7d commit 4276483
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
13 changes: 6 additions & 7 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ var baseEnv = initBaseEnv(map[string]Extension{
EvalContextHandler: nil,
},

"eval": {
Func: RunEval,
UndefinedHandler: defaultUndefinedHandler,
EvalContextHandler: defaultContextHandler,
},

"unescape": {
Func: jlib.Unescape,
UndefinedHandler: defaultUndefinedHandler,
Expand Down Expand Up @@ -410,12 +416,6 @@ var baseEnv = initBaseEnv(map[string]Extension{
UndefinedHandler: nil,
EvalContextHandler: nil,
},

"eval": {
Func: RunEval,
UndefinedHandler: defaultUndefinedHandler,
EvalContextHandler: defaultContextHandler,
},
})

func initBaseEnv(exts map[string]Extension) *environment {
Expand Down Expand Up @@ -473,7 +473,6 @@ func undefinedHandlerAppend(argv []reflect.Value) bool {
// Context handlers

func contextHandlerSubstring(argv []reflect.Value) bool {

// If substring() is called with one or two numeric arguments,
// use the evaluation context as the first argument.
switch len(argv) {
Expand Down
55 changes: 50 additions & 5 deletions jsonata.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,74 @@ func (e *Expr) EvalBytes(data []byte) ([]byte, error) {
return json.Marshal(v)
}

func RunEval(expression string) (interface{}, error) {
func RunEval(initialContext reflect.Value, expression ...interface{}) (interface{}, error) {
var s evaluator

s = simple{}

return s.Eval(expression)
var result interface{}

var err error

if len(expression) == 0 {
result, err = s.InitialEval(initialContext.Interface(), "$$")
if err != nil {
return nil, err
}
}

for index := range expression {
expressionStr, ok := expression[index].(string)
if !ok {
return nil, fmt.Errorf("%v not able to be used as a string in eval statement", expression[index])
}
if index == 0 {
result, err = s.InitialEval(initialContext.Interface(), expressionStr)
if err != nil {
return nil, err
}
continue
}

result, err = s.InitialEval(result, expressionStr)
if err != nil {
return nil, err
}
}

return result, nil
}

type evaluator interface {
Eval(expression string) (interface{}, error)
InitialEval(item interface{}, expression string) (interface{}, error)
Eval(override, expression string) (interface{}, error)
}

type simple struct {

}

func (s simple) Eval(expression string) (interface{}, error) {
func (s simple) InitialEval(item interface{}, expression string) (interface{}, error) {
expr, err := Compile(expression)
if err != nil {
return nil, err
}

result, err := expr.Eval(item)
if err != nil {
return nil, err
}

return result, nil
}

func (s simple) Eval(override, expression string) (interface{}, error) {
expr, err := Compile(expression)
if err != nil {
return nil, err
}

result, err := expr.Eval(``)
result, err := expr.Eval(override)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4276483

Please sign in to comment.