forked from PaesslerAG/gval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow arbitrary functions as language function
- Loading branch information
JanErik Keller
committed
Oct 12, 2017
1 parent
b957afc
commit e7b2bbc
Showing
5 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package gval | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
type function func(arguments ...interface{}) (interface{}, error) | ||
|
||
func toFunc(f interface{}) function { | ||
if f, ok := f.(func(arguments ...interface{}) (interface{}, error)); ok { | ||
return function(f) | ||
} | ||
return func(args ...interface{}) (interface{}, error) { | ||
fun := reflect.ValueOf(f) | ||
t := fun.Type() | ||
|
||
variadic := t.IsVariadic() | ||
numIn := t.NumIn() | ||
|
||
if (!variadic && len(args) != numIn) || (variadic && len(args) < numIn-1) { | ||
return nil, fmt.Errorf("invalid number of parameters") | ||
} | ||
|
||
in := make([]reflect.Value, len(args)) | ||
var inType reflect.Type | ||
for i, arg := range args { | ||
if !variadic || i < numIn-1 { | ||
inType = t.In(i) | ||
} else if i == numIn-1 { | ||
inType = t.In(numIn - 1).Elem() | ||
} | ||
argVal := reflect.ValueOf(arg) | ||
if arg == nil || !argVal.Type().AssignableTo(inType) { | ||
return nil, fmt.Errorf("expected type %s for parameter %d but got %T", | ||
inType.String(), i, arg) | ||
} | ||
in[i] = argVal | ||
} | ||
|
||
out := fun.Call(in) | ||
|
||
r := make([]interface{}, len(out)) | ||
for i, e := range out { | ||
r[i] = e.Interface() | ||
} | ||
|
||
err := error(nil) | ||
errorInterface := reflect.TypeOf((*error)(nil)).Elem() | ||
if len(r) > 0 && fun.Type().Out(len(r)-1).Implements(errorInterface) { | ||
if r[len(r)-1] != nil { | ||
err = r[len(r)-1].(error) | ||
} | ||
r = r[0 : len(r)-1] | ||
} | ||
|
||
switch len(r) { | ||
case 0: | ||
return nil, err | ||
case 1: | ||
return r[0], err | ||
default: | ||
return r, err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package gval | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_toFunc(t *testing.T) { | ||
myError := fmt.Errorf("my error") | ||
tests := []struct { | ||
name string | ||
function interface{} | ||
arguments []interface{} | ||
want interface{} | ||
wantErr error | ||
wantAnyErr bool | ||
}{ | ||
{ | ||
name: "empty", | ||
function: func() {}, | ||
}, | ||
{ | ||
name: "one arg", | ||
function: func(a interface{}) { | ||
if a != true { | ||
panic("fail") | ||
} | ||
}, | ||
arguments: []interface{}{true}, | ||
}, | ||
{ | ||
name: "three args", | ||
function: func(a, b, c interface{}) { | ||
if a != 1 || b != 2 || c != 3 { | ||
panic("fail") | ||
} | ||
}, | ||
arguments: []interface{}{1, 2, 3}, | ||
}, | ||
{ | ||
name: "input types", | ||
function: func(a int, b string, c bool) { | ||
if a != 1 || b != "2" || !c { | ||
panic("fail") | ||
} | ||
}, | ||
arguments: []interface{}{1, "2", true}, | ||
}, | ||
{ | ||
name: "wronge input type int", | ||
function: func(a int, b string, c bool) {}, | ||
arguments: []interface{}{"1", "2", true}, | ||
wantAnyErr: true, | ||
}, | ||
{ | ||
name: "wronge input type string", | ||
function: func(a int, b string, c bool) {}, | ||
arguments: []interface{}{1, 2, true}, | ||
wantAnyErr: true, | ||
}, | ||
{ | ||
name: "wronge input type bool", | ||
function: func(a int, b string, c bool) {}, | ||
arguments: []interface{}{1, "2", "true"}, | ||
wantAnyErr: true, | ||
}, | ||
{ | ||
name: "wronge input number", | ||
function: func(a int, b string, c bool) {}, | ||
arguments: []interface{}{1, "2"}, | ||
wantAnyErr: true, | ||
}, | ||
{ | ||
name: "one return", | ||
function: func() bool { | ||
return true | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "three returns", | ||
function: func() (bool, string, int) { | ||
return true, "2", 3 | ||
}, | ||
want: []interface{}{true, "2", 3}, | ||
}, | ||
{ | ||
name: "error", | ||
function: func() error { | ||
return myError | ||
}, | ||
wantErr: myError, | ||
}, | ||
{ | ||
name: "none error", | ||
function: func() error { | ||
return nil | ||
}, | ||
}, | ||
{ | ||
name: "one return with error", | ||
function: func() (bool, error) { | ||
return false, myError | ||
}, | ||
want: false, | ||
wantErr: myError, | ||
}, | ||
{ | ||
name: "three returns with error", | ||
function: func() (bool, string, int, error) { | ||
return false, "", 0, myError | ||
}, | ||
want: []interface{}{false, "", 0}, | ||
wantErr: myError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := toFunc(tt.function)(tt.arguments...) | ||
|
||
if tt.wantAnyErr { | ||
if err != nil { | ||
return | ||
} | ||
t.Fatalf("toFunc()(args...) = error(nil), but wantAnyErr") | ||
} | ||
if err != tt.wantErr { | ||
t.Fatalf("toFunc()(args...) = error(%v), wantErr (%v)", err, tt.wantErr) | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("toFunc()(args...) = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters