Skip to content

Commit 56448f8

Browse files
bizywizyantonmedv
authored andcommitted
expr.Operator passes before expr.Env caused error (#606)
1 parent 55be21a commit 56448f8

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Diff for: checker/checker_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ func TestCheck_TaggedFieldName(t *testing.T) {
632632
tree, err := parser.Parse(`foo.bar`)
633633
require.NoError(t, err)
634634

635-
config := &conf.Config{}
635+
config := conf.CreateNew()
636636
expr.Env(struct {
637637
x struct {
638638
y bool `expr:"bar"`

Diff for: conf/config.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Config struct {
3232
func CreateNew() *Config {
3333
c := &Config{
3434
Optimize: true,
35+
Types: make(TypesTable),
3536
ConstFns: make(map[string]reflect.Value),
3637
Functions: make(map[string]*builtin.Function),
3738
Builtins: make(map[string]*builtin.Function),
@@ -62,7 +63,10 @@ func (c *Config) WithEnv(env any) {
6263
}
6364

6465
c.Env = env
65-
c.Types = CreateTypesTable(env)
66+
types := CreateTypesTable(env)
67+
for name, t := range types {
68+
c.Types[name] = t
69+
}
6670
c.MapEnv = mapEnv
6771
c.DefaultType = mapValueType
6872
c.Strict = true

Diff for: expr_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,20 @@ func TestRaceCondition_variables(t *testing.T) {
25112511
wg.Wait()
25122512
}
25132513

2514+
func TestOperatorDependsOnEnv(t *testing.T) {
2515+
env := map[string]any{
2516+
"plus": func(a, b int) int {
2517+
return 42
2518+
},
2519+
}
2520+
program, err := expr.Compile(`1 + 2`, expr.Operator("+", "plus"), expr.Env(env))
2521+
require.NoError(t, err)
2522+
2523+
out, err := expr.Run(program, env)
2524+
require.NoError(t, err)
2525+
assert.Equal(t, 42, out)
2526+
}
2527+
25142528
func TestArrayComparison(t *testing.T) {
25152529
tests := []struct {
25162530
env any

0 commit comments

Comments
 (0)