Skip to content

Commit

Permalink
Fix work with field funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Oct 10, 2018
1 parent 4439fd8 commit fb34ea4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
34 changes: 31 additions & 3 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,35 @@ func TestEval_panic(t *testing.T) {
}
}

func TestEval_func(t *testing.T) {
type testEnv struct {
Func func() string
}

env := &testEnv{
Func: func() string {
return "func"
},
}

input := `Func()`

node, err := expr.Parse(input, expr.Env(&testEnv{}))
if err != nil {
t.Fatal(err)
}

actual, err := expr.Run(node, env)
if err != nil {
t.Fatal(err)
}

expected := "func"
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("TestEval_method:\ngot\n\t%#v\nexpected:\n\t%#v", actual, expected)
}
}

func TestEval_method(t *testing.T) {
env := &testEnv{
Hello: "hello",
Expand All @@ -596,10 +625,9 @@ func TestEval_method(t *testing.T) {
},
}

input := `Title(Hello) ~ Empty() ~ (CompareVersion(1, 3) ? World.String() : '')`
input := `Title(Hello) ~ Space() ~ (CompareVersion(1, 3) ? World.String() : '')`

node, err := expr.Parse(input, expr.Env(&testEnv{}))
fmt.Printf("%#v\n", node)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -641,6 +669,6 @@ func (e *testEnv) Title(s string) string {
return strings.Title(s)
}

func (e *testEnv) Empty() string {
func (e *testEnv) Space() string {
return " "
}
5 changes: 4 additions & 1 deletion runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func getFunc(val interface{}, i interface{}) (interface{}, bool) {
if method.IsValid() && method.CanInterface() {
return method.Interface(), true
}
value := v.FieldByName(name)

// If struct has not method, maybe it has func field.
// To access this field we need dereferenced value.
value := d.FieldByName(name)
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
Expand Down

0 comments on commit fb34ea4

Please sign in to comment.