-
Notifications
You must be signed in to change notification settings - Fork 15
/
funcs.go
45 lines (38 loc) · 810 Bytes
/
funcs.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
package fmr
import (
"fmt"
"zliu.org/goutil"
)
var builtinFuncs = make(map[string]interface{})
func init() {
builtinFuncs["fmr.list"] = fmrList
builtinFuncs["fmr.entity"] = fmrEntity
}
// Call funcs by name fn and args
func Call(fn string, args ...interface{}) (interface{}, error) {
ret, err := goutil.Call(builtinFuncs, fn, args...)
if err != nil {
return nil, err
}
if len(ret) == 0 {
return nil, nil
}
return ret[0].Interface(), nil
}
func fmrList(items ...interface{}) []interface{} {
return items
}
func fmrEntity(items ...interface{}) map[string]interface{} {
l := len(items)
if l == 0 {
return nil
}
typ := fmt.Sprintf("%v", items[0])
if typ == "" {
return nil
}
if l == 1 {
return map[string]interface{}{typ: nil}
}
return map[string]interface{}{typ: items[1:]}
}