forked from project-flogo/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
40 lines (30 loc) · 805 Bytes
/
function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package function
import (
"fmt"
"github.com/JCorpse96/core/data"
"github.com/JCorpse96/core/data/coerce"
)
type Function interface {
Name() string
Sig() (paramTypes []data.Type, isVariadic bool)
Eval(params ...interface{}) (interface{}, error)
}
func Eval(f Function, params ...interface{}) (interface{}, error) {
paramTypes, isVariadic := f.Sig()
paramValues := make([]interface{}, len(params))
typeIdx := 0
if !isVariadic && len(params) != len(paramTypes) {
return 0, fmt.Errorf("%s function should have %d arguments", f.Name(), len(paramTypes))
}
for idx, param := range params {
if !isVariadic {
typeIdx = idx
}
val, err := coerce.ToType(param, paramTypes[typeIdx])
if err != nil {
return nil, err
}
paramValues[idx] = val
}
return f.Eval(paramValues...)
}