Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expr.Operator passes before expr.Env caused error #606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2502,3 +2502,17 @@ 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)
}
Loading