Skip to content

Commit

Permalink
Shorthand function to evaluate with context
Browse files Browse the repository at this point in the history
This is in addition of Evaluate.
  • Loading branch information
mariotoffia committed Mar 16, 2021
1 parent 6ca878f commit 7d7cfa8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion gval.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import (

//Evaluate given parameter with given expression in gval full language
func Evaluate(expression string, parameter interface{}, opts ...Language) (interface{}, error) {
return EvaluateWithContext(context.Background(), expression, parameter, opts...)
}

//Evaluate given parameter with given expression in gval full language using a context
func EvaluateWithContext(c context.Context, expression string, parameter interface{}, opts ...Language) (interface{}, error) {
l := full
if len(opts) > 0 {
l = NewLanguage(append([]Language{l}, opts...)...)
}
return l.Evaluate(expression, parameter)
return l.EvaluateWithContext(c, expression, parameter)
}

// Full is the union of Arithmetic, Bitmask, Text, PropositionalLogic, and Json
Expand Down
7 changes: 6 additions & 1 deletion language.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ func (l Language) NewEvaluable(expression string) (Evaluable, error) {

// Evaluate given parameter with given expression
func (l Language) Evaluate(expression string, parameter interface{}) (interface{}, error) {
return l.EvaluateWithContext(context.Background(), expression, parameter)
}

// Evaluate given parameter with given expression using context
func (l Language) EvaluateWithContext(c context.Context, expression string, parameter interface{}) (interface{}, error) {
eval, err := l.NewEvaluable(expression)
if err != nil {
return nil, err
}
v, err := eval(context.Background(), parameter)
v, err := eval(c, parameter)
if err != nil {
return nil, fmt.Errorf("can not evaluate %s: %v", expression, err)
}
Expand Down

0 comments on commit 7d7cfa8

Please sign in to comment.