Skip to content

Commit fb34ea4

Browse files
committed
Fix work with field funcs
1 parent 4439fd8 commit fb34ea4

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

eval_test.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,35 @@ func TestEval_panic(t *testing.T) {
585585
}
586586
}
587587

588+
func TestEval_func(t *testing.T) {
589+
type testEnv struct {
590+
Func func() string
591+
}
592+
593+
env := &testEnv{
594+
Func: func() string {
595+
return "func"
596+
},
597+
}
598+
599+
input := `Func()`
600+
601+
node, err := expr.Parse(input, expr.Env(&testEnv{}))
602+
if err != nil {
603+
t.Fatal(err)
604+
}
605+
606+
actual, err := expr.Run(node, env)
607+
if err != nil {
608+
t.Fatal(err)
609+
}
610+
611+
expected := "func"
612+
if !reflect.DeepEqual(actual, expected) {
613+
t.Fatalf("TestEval_method:\ngot\n\t%#v\nexpected:\n\t%#v", actual, expected)
614+
}
615+
}
616+
588617
func TestEval_method(t *testing.T) {
589618
env := &testEnv{
590619
Hello: "hello",
@@ -596,10 +625,9 @@ func TestEval_method(t *testing.T) {
596625
},
597626
}
598627

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

601630
node, err := expr.Parse(input, expr.Env(&testEnv{}))
602-
fmt.Printf("%#v\n", node)
603631
if err != nil {
604632
t.Fatal(err)
605633
}
@@ -641,6 +669,6 @@ func (e *testEnv) Title(s string) string {
641669
return strings.Title(s)
642670
}
643671

644-
func (e *testEnv) Empty() string {
672+
func (e *testEnv) Space() string {
645673
return " "
646674
}

runtime.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ func getFunc(val interface{}, i interface{}) (interface{}, bool) {
120120
if method.IsValid() && method.CanInterface() {
121121
return method.Interface(), true
122122
}
123-
value := v.FieldByName(name)
123+
124+
// If struct has not method, maybe it has func field.
125+
// To access this field we need dereferenced value.
126+
value := d.FieldByName(name)
124127
if value.IsValid() && value.CanInterface() {
125128
return value.Interface(), true
126129
}

0 commit comments

Comments
 (0)