Skip to content
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
8 changes: 8 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,14 @@ func TestExpr_fetch_from_func(t *testing.T) {
assert.Contains(t, err.Error(), "cannot fetch Value from func()")
}

func TestExpr_fetch_field_from_string(t *testing.T) {
// Accessing a named field on a string value (via dynamic map lookup) should
// produce a clear error instead of "invalid operation: int(string)".
_, err := expr.Eval(`let v = {"k": "hello"}; v.k.missing != ""`, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "cannot fetch missing from string")
}

func TestExpr_map_default_values(t *testing.T) {
env := map[string]any{
"foo": map[string]string{},
Expand Down
3 changes: 3 additions & 0 deletions vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Fetch(from, i any) any {

switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
if _, ok := i.(string); ok {
panic(fmt.Sprintf("cannot fetch %v from %T", i, from))
}
index := ToInt(i)
l := v.Len()
if index < 0 {
Expand Down