Skip to content

Commit 5613693

Browse files
authored
runtime.Fetch: add string type assertion check before trying to check if the value is a method (#349)
1 parent 742c438 commit 5613693

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

expr_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,32 @@ func TestIssue271(t *testing.T) {
16131613
require.Equal(t, 1.0, output)
16141614
}
16151615

1616+
type Issue346Array []Issue346Type
1617+
1618+
type Issue346Type struct {
1619+
Bar string
1620+
}
1621+
1622+
func (i Issue346Array) Len() int {
1623+
return len(i)
1624+
}
1625+
1626+
func TestIssue346(t *testing.T) {
1627+
code := `Foo[0].Bar`
1628+
1629+
env := map[string]interface{}{
1630+
"Foo": Issue346Array{
1631+
{Bar: "bar"},
1632+
},
1633+
}
1634+
program, err := expr.Compile(code, expr.Env(env))
1635+
require.NoError(t, err)
1636+
1637+
output, err := expr.Run(program, env)
1638+
require.NoError(t, err)
1639+
require.Equal(t, "bar", output)
1640+
}
1641+
16161642
func TestCompile_allow_to_use_interface_to_get_an_element_from_map(t *testing.T) {
16171643
code := `{"value": "ok"}[vars.key]`
16181644
env := map[string]interface{}{

vm/runtime/runtime.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ func Fetch(from, i interface{}) interface{} {
1818

1919
// Methods can be defined on any type.
2020
if v.NumMethod() > 0 {
21-
method := v.MethodByName(i.(string))
22-
if method.IsValid() {
23-
return method.Interface()
21+
if methodName, ok := i.(string); ok {
22+
method := v.MethodByName(methodName)
23+
if method.IsValid() {
24+
return method.Interface()
25+
}
2426
}
2527
}
2628

0 commit comments

Comments
 (0)