Skip to content

Commit 1042ff4

Browse files
committed
Fix getting default values out of maps
1 parent 211ae69 commit 1042ff4

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

expr_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -828,15 +828,20 @@ func TestExpr_fetch_from_func(t *testing.T) {
828828
assert.Contains(t, err.Error(), "cannot fetch Value from func()")
829829
}
830830

831-
func TestExpr_reference_options(t *testing.T) {
832-
options := []expr.Option{expr.Env(map[string]interface{}{})}
831+
func TestExpr_map_default_values(t *testing.T) {
832+
env := map[string]interface{}{
833+
"foo": map[string]string{},
834+
"bar": map[string]*string{},
835+
}
833836

834-
e, err := expr.Compile("'hello world'", options...)
835-
assert.NoError(t, err)
837+
input := `foo['missing'] == '' && bar['missing'] == nil`
838+
839+
program, err := expr.Compile(input, expr.Env(env))
840+
require.NoError(t, err)
836841

837-
output, err := expr.Run(e, "'hello world'")
838-
assert.NoError(t, err)
839-
assert.Equal(t, "hello world", output)
842+
output, err := expr.Run(program, env)
843+
require.NoError(t, err)
844+
require.Equal(t, true, output)
840845
}
841846

842847
//

vm/runtime.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ func fetch(from interface{}, i interface{}) interface{} {
3636

3737
case reflect.Map:
3838
value := v.MapIndex(reflect.ValueOf(i))
39-
if value.IsValid() && value.CanInterface() {
40-
return value.Interface()
39+
if value.IsValid() {
40+
if value.CanInterface() {
41+
return value.Interface()
42+
}
43+
} else {
44+
elem := reflect.TypeOf(from).Elem()
45+
return reflect.Zero(elem).Interface()
4146
}
4247

4348
case reflect.Struct:

0 commit comments

Comments
 (0)