-
Notifications
You must be signed in to change notification settings - Fork 64
/
corece.go
49 lines (37 loc) · 960 Bytes
/
corece.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
41
42
43
44
45
46
47
48
49
package coerce
import (
"fmt"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
)
func init() {
_ = function.Register(&fnToType{})
}
type baseFn struct {
}
func (*baseFn) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeAny}, false
}
type fnToType struct {
}
func (*fnToType) Name() string {
return "toType"
}
func (*fnToType) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeAny, data.TypeString}, false
}
func (*fnToType) Eval(params ...interface{}) (interface{}, error) {
if len(params) < 2 {
return nil, fmt.Errorf("missing params, signature is toType(any,string)")
}
typeStr, ok := params[1].(string)
if !ok {
return nil, fmt.Errorf("senond param must be a string")
}
t, err := data.ToTypeEnum(typeStr)
if err != nil {
return nil, err
}
return coerce.ToType(params[0], t)
}