Skip to content

Commit

Permalink
expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Lack30 committed Nov 29, 2024
1 parent 1a6723b commit 347ced5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pkg/data/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
)

const (
LocatorObject = "$"
LocatorHeader = "#"
LocatorProperty = "@"
LocatorObject = "d$" // data object
LocatorHeader = "h$" // headers
LocatorProperty = "p$" // properties
)

type ObjectContainer struct {
Expand Down
8 changes: 6 additions & 2 deletions pkg/expression/expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ func New(ctx context.Context) *Expr {
itemAwareLocators: map[string]data.IItemAwareLocator{},
}
engine.env = map[string]interface{}{
data.LocatorObject: engine.fetchItem(data.LocatorObject),
data.LocatorHeader: engine.fetchItem(data.LocatorHeader),
data.LocatorObject: engine.fetchItem(data.LocatorObject),
}
return engine
}

func (engine *Expr) CompileExpression(source string) (result expression.ICompiledExpression, err error) {
result, err = expr.Compile(source, expr.Env(engine.env), expr.AllowUndefinedVariables())
opts := []expr.Option{
expr.Env(engine.env),
expr.AllowUndefinedVariables(),
}
result, err = expr.Compile(source, opts...)
return
}

Expand Down
34 changes: 31 additions & 3 deletions pkg/expression/expr/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"testing"

"github.com/expr-lang/expr/ast"
"github.com/stretchr/testify/assert"

"github.com/olive-io/bpmn/schema"
Expand Down Expand Up @@ -52,6 +53,16 @@ func TestExprSum(t *testing.T) {
assert.True(t, ok)
}

type Visitor struct {
Identifiers []string
}

func (v *Visitor) Visit(node *ast.Node) {
if n, ok := (*node).(*ast.IdentifierNode); ok {
v.Identifiers = append(v.Identifiers, n.Value)
}
}

type dataObjects map[string]data.IItemAware

func (d dataObjects) PutItemAwareById(id schema.IdRef, itemAware data.IItemAware) {
Expand Down Expand Up @@ -85,11 +96,28 @@ func TestExpr_getDataObject(t *testing.T) {
"dataObject1": container1,
}
engine.SetItemAwareLocator(data.LocatorObject, objs)
compiled, err := engine.CompileExpression("$('dataObject1').msg == 'hello'")
assert.Nil(t, err)
compiled, err = engine.CompileExpression("$('dataObject') == 1")

h1 := data.NewContainer(nil)
h1.Put(2)
var headers dataObjects = map[string]data.IItemAware{
"a": h1,
}
engine.SetItemAwareLocator(data.LocatorHeader, headers)

compiled, err := engine.CompileExpression("d$('dataObject1').msg == 'hello'")
assert.Nil(t, err)
result, err := engine.EvaluateExpression(compiled, map[string]interface{}{})
assert.Nil(t, err)
assert.True(t, result.(bool))
compiled, err = engine.CompileExpression("d$('dataObject') == 1")
assert.Nil(t, err)
result, err = engine.EvaluateExpression(compiled, map[string]interface{}{})
assert.Nil(t, err)
assert.True(t, result.(bool))

compiled, err = engine.CompileExpression("h$('a') + 1")
assert.Nil(t, err)
result, err = engine.EvaluateExpression(compiled, map[string]interface{}{})
assert.Nil(t, err)
assert.Equal(t, result.(int), int(3))
}

0 comments on commit 347ced5

Please sign in to comment.