Skip to content

Commit

Permalink
expr.Operator passes before expr.Env caused error (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizywizy authored and antonmedv committed Apr 13, 2024
1 parent 55be21a commit 56448f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func TestCheck_TaggedFieldName(t *testing.T) {
tree, err := parser.Parse(`foo.bar`)
require.NoError(t, err)

config := &conf.Config{}
config := conf.CreateNew()
expr.Env(struct {
x struct {
y bool `expr:"bar"`
Expand Down
6 changes: 5 additions & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
func CreateNew() *Config {
c := &Config{
Optimize: true,
Types: make(TypesTable),
ConstFns: make(map[string]reflect.Value),
Functions: make(map[string]*builtin.Function),
Builtins: make(map[string]*builtin.Function),
Expand Down Expand Up @@ -62,7 +63,10 @@ func (c *Config) WithEnv(env any) {
}

c.Env = env
c.Types = CreateTypesTable(env)
types := CreateTypesTable(env)
for name, t := range types {
c.Types[name] = t
}
c.MapEnv = mapEnv
c.DefaultType = mapValueType
c.Strict = true
Expand Down
14 changes: 14 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,20 @@ func TestRaceCondition_variables(t *testing.T) {
wg.Wait()
}

func TestOperatorDependsOnEnv(t *testing.T) {
env := map[string]any{
"plus": func(a, b int) int {
return 42
},
}
program, err := expr.Compile(`1 + 2`, expr.Operator("+", "plus"), expr.Env(env))
require.NoError(t, err)

out, err := expr.Run(program, env)
require.NoError(t, err)
assert.Equal(t, 42, out)
}

func TestArrayComparison(t *testing.T) {
tests := []struct {
env any
Expand Down

0 comments on commit 56448f8

Please sign in to comment.