-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefBox.go
156 lines (131 loc) · 4.41 KB
/
defBox.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gopcp
import (
"errors"
"fmt"
)
var DefBox = &Sandbox{map[string]*BoxFunc{
// (if, conditionExp, successExp, failExp)
"if": ToLazySandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
if len(args) < 2 || len(args) > 3 {
return nil, errors.New("if grammer error. if must have at least 2 params, at most 3 params. eg: [\"if\", true, 1, 0], [\"if\", true, 1]")
}
conditionExp := args[0]
successExp := args[1]
var failExp interface{} = nil
if len(args) > 2 {
failExp = args[2]
}
// condition
conditionRet, cerr := pcpServer.ExecuteAst(conditionExp, attachment)
if cerr != nil {
return nil, cerr
}
if conditionRet == false || conditionRet == 0.0 || conditionRet == nil {
return pcpServer.ExecuteAst(failExp, attachment)
}
return pcpServer.ExecuteAst(successExp, attachment)
}),
// basic data structure: List
"List": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
return args, nil
}),
// basic data structure: Map[string]interface{}
"Map": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
l := len(args)
if l%2 == 1 {
return nil, errors.New("Map grammer error. eg: [\"Map\", \"age\", 3, \"weight\", 45]")
}
m := make(map[string]interface{})
for i := 0; i < l; i += 2 {
key, ok := args[i].(string)
if !ok {
return nil, errors.New("Map grammer error. eg: [\"Map\", \"age\", 3, \"weight\", 45]")
}
m[key] = args[i+1]
}
return m, nil
}),
// get property from map[string]interface{} structure
"prop": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
if len(args) != 2 {
return nil, errors.New("Prop grammer error. eg: [\"Prop\", {\"a\": 1}, \"a\"]")
}
obj, ok1 := args[0].(map[string]interface{})
propName, ok2 := args[1].(string)
if !ok1 || !ok2 {
return nil, fmt.Errorf("error types of function prop")
}
return obj[propName], nil
}),
"error": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
l := len(args)
if l%2 < 1 {
return nil, errors.New("Must specify error message. eg: [\"error\", \"Exception!\"]")
}
msg, ok := args[0].(string)
if !ok {
return nil, errors.New("Must specify error message (string). eg: [\"error\", \"Exception!\"]")
}
return nil, errors.New(msg)
}),
// math operations
"+": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
var res float64 = 0
for _, arg := range args {
if val, ok := arg.(float64); !ok {
return nil, errors.New("args of \"+\" should be float64")
} else {
res += val
}
}
return res, nil
}),
"*": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
var res float64 = 1
for _, arg := range args {
if val, ok := arg.(float64); !ok {
return nil, errors.New("args of \"+\" should be float64")
} else {
res *= val
}
}
return res, nil
}),
"-": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
if len(args) != 2 {
return nil, errors.New("- must have two arguments")
}
v1, ok1 := args[0].(float64)
v2, ok2 := args[1].(float64)
if !ok1 || !ok2 {
return nil, fmt.Errorf("args of \"-\" should be float64, but got v1=%v, v2=%v", v1, v2)
}
return v1 - v2, nil
}),
"/": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
if len(args) != 2 {
return nil, errors.New("- must have two arguments")
}
v1, ok1 := args[0].(float64)
v2, ok2 := args[1].(float64)
if !ok1 || !ok2 {
return nil, fmt.Errorf("args of \"/\" should be float64, but got v1=%v, v2=%v", v1, v2)
}
if v2 == 0 {
return nil, errors.New("divisor can not be 0")
}
return v1 / v2, nil
}),
"==": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
if len(args) != 2 {
return nil, errors.New("== must have two arguments")
}
return args[0] == args[1], nil
}),
"!=": ToSandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
if len(args) != 2 {
return nil, errors.New("== must have two arguments")
}
return args[0] != args[1], nil
}),
}}