Skip to content
Open
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
20 changes: 16 additions & 4 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ func TestBuiltin(t *testing.T) {
}
}

func TestBuiltin_get_struct_non_string_key(t *testing.T) {
type anyEnv struct{ Foo any }
env := anyEnv{Foo: mock.Foo{Value: "a"}}

program, err := expr.Compile(`get(Foo, 1)`, expr.Env(env))
require.NoError(t, err)

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

func TestBuiltin_works_with_any(t *testing.T) {
config := map[string]struct {
arity int
Expand Down Expand Up @@ -874,10 +886,10 @@ func TestAbs_UnsignedIntegers(t *testing.T) {
// Test that abs() correctly handles unsigned integers
// Unsigned integers are always non-negative, so abs() should return them unchanged
tests := []struct {
name string
env map[string]any
expr string
want any
name string
env map[string]any
expr string
want any
}{
{"uint", map[string]any{"x": uint(42)}, "abs(x)", uint(42)},
{"uint8", map[string]any{"x": uint8(42)}, "abs(x)", uint8(42)},
Expand Down
5 changes: 4 additions & 1 deletion builtin/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ func get(params ...any) (out any, err error) {
}

case reflect.Struct:
fieldName := i.(string)
fieldName, ok := i.(string)
if !ok {
break
}
t := v.Type()
field, ok := t.FieldByNameFunc(func(name string) bool {
f, _ := t.FieldByName(name)
Expand Down
5 changes: 4 additions & 1 deletion vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func Fetch(from, i any) any {
}

case reflect.Struct:
fieldName := i.(string)
fieldName, ok := i.(string)
if !ok {
break
}
t := v.Type()
key := fieldCacheKey{
t: t,
Expand Down
16 changes: 16 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ func TestRun_MethodWithError(t *testing.T) {
require.Equal(t, "error", selfErr.Error())
}

func TestRun_FetchStructNonStringKey(t *testing.T) {
type Foo struct{ Value string }
type anyEnv struct {
Foo any
I any
}
env := anyEnv{Foo: Foo{Value: "a"}, I: 1}

program, err := expr.Compile(`Foo[I]`, expr.Env(env))
require.NoError(t, err)

_, err = expr.Run(program, env)
require.Error(t, err)
require.Contains(t, err.Error(), "cannot fetch")
}

func TestRun_FastMethods(t *testing.T) {
input := `hello() + world()`

Expand Down