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
26 changes: 26 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ type evalErrorTest struct {
err string
}

type evalParams map[string]interface{}

func (p evalParams) Max(a, b float64) float64 {
if a < b {
return b
}
return a
}

func (p evalParams) Min(a, b float64) float64 {
if a < b {
return a
}
return b
}

var evalTests = []evalTest{
{
"foo",
Expand Down Expand Up @@ -339,6 +355,16 @@ var evalTests = []evalTest{
map[string]interface{}{"foo": func(in string) string { return "hello " + in }},
"hello world",
},
{
"Max(a, b)",
evalParams{"a": 1.23, "b": 3.21},
3.21,
},
{
"Min(a, b)",
evalParams{"a": 1.23, "b": 3.21},
1.23,
},
}

var evalErrorTests = []evalErrorTest{
Expand Down
12 changes: 9 additions & 3 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func extract(val interface{}, i interface{}) (interface{}, bool) {
return nil, false
}

func getFunc(val interface{}, i interface{}) (interface{}, bool) {
func getFunc(val interface{}, name string) (interface{}, bool) {
v := reflect.ValueOf(val)
d := v
if v.Kind() == reflect.Ptr {
Expand All @@ -92,12 +92,18 @@ func getFunc(val interface{}, i interface{}) (interface{}, bool) {

switch d.Kind() {
case reflect.Map:
value := d.MapIndex(reflect.ValueOf(i))
value := d.MapIndex(reflect.ValueOf(name))
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
// A map may have method too.
if v.NumMethod() > 0 {
method := v.MethodByName(name)
if method.IsValid() && method.CanInterface() {
return method.Interface(), true
}
}
case reflect.Struct:
name := reflect.ValueOf(i).String()
method := v.MethodByName(name)
if method.IsValid() && method.CanInterface() {
return method.Interface(), true
Expand Down