/
direction.go
300 lines (267 loc) · 8.03 KB
/
direction.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package direction
import (
"errors"
"github.com/TIBCOSoftware/flogo-lib/logger"
"reflect"
"strconv"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/expr"
"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function"
"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/gocc/token"
"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/funcexprtype"
"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/ref"
)
var log = logger.GetLogger("expression-direction")
type Attribute interface{}
func NewDoubleQuoteStringLit(lit interface{}) (string, error) {
str := strings.TrimSpace(string(lit.(*token.Token).Lit))
if str != "" && len(str) > 0 {
str = RemoveQuote(str)
}
//Eascap string
if strings.Contains(str, "\\\"") {
str = strings.Replace(str, "\\\"", "\"", -1)
}
return str, nil
}
func NewSingleQuoteStringLit(lit interface{}) (string, error) {
str := strings.TrimSpace(string(lit.(*token.Token).Lit))
if str != "" && len(str) > 0 {
str = RemoveQuote(str)
}
//Eascap string
if strings.Contains(str, "\\'") {
str = strings.Replace(str, "\\'", "'", -1)
}
return str, nil
}
func NewIntLit(lit interface{}) (int, error) {
str := strings.TrimSpace(string(lit.(*token.Token).Lit))
s, err := data.CoerceToInteger(str)
return s, err
}
func NewFloatLit(lit interface{}) (float64, error) {
str := strings.TrimSpace(string(lit.(*token.Token).Lit))
s, err := data.CoerceToNumber(str)
return s, err
}
func NewBool(lit interface{}) (bool, error) {
s := strings.TrimSpace(string(lit.(*token.Token).Lit))
b, err := strconv.ParseBool(s)
return b, err
}
type NIL struct {
}
func NewNilLit(lit interface{}) (*NIL, error) {
return &NIL{}, nil
}
func NewMappingRef(lit interface{}) (interface{}, error) {
s := strings.TrimSpace(string(lit.(*token.Token).Lit))
if strings.HasPrefix(s, "$.") || strings.HasPrefix(s, "$$") {
m := ref.NewArrayRef(s)
return m, nil
} else {
m := ref.NewMappingRef(s)
return m, nil
}
}
func NewFunction(name Attribute, parameters Attribute) (interface{}, error) {
f_func := &function.FunctionExp{}
to := name.(*token.Token)
f_func.Name = string(to.Lit)
switch parameters.(type) {
case *function.Parameter:
f_func.Params = append(f_func.Params, parameters.(*function.Parameter))
case []*function.Parameter:
for _, p := range parameters.([]*function.Parameter) {
if !p.IsEmtpy() {
f_func.Params = append(f_func.Params, p)
}
}
}
log.Debug(f_func.Tostring())
return f_func, nil
}
func NewArgument(a Attribute) (interface{}, error) {
param := &function.Parameter{}
parameters := []*function.Parameter{}
switch t := a.(type) {
case *token.Token:
param.Type = funcexprtype.STRING
param.Value = string(a.(*token.Token).Lit)
case int:
param.Type = funcexprtype.INTEGER
param.Value = a
case float64:
param.Type = funcexprtype.FLOAT
param.Value = a
case string:
param.Type = funcexprtype.STRING
param.Value = a.(string)
case bool:
param.Type = funcexprtype.BOOLEAN
param.Value = a.(bool)
case *NIL:
param.Type = funcexprtype.NIL
param.Value = nil
case *function.FunctionExp:
param.Type = funcexprtype.FUNCTION
param.Function = a.(*function.FunctionExp)
case []*function.Parameter:
for _, p := range a.([]*function.Parameter) {
if !p.IsEmtpy() {
parameters = append(parameters, p)
}
}
case *expr.Expression:
exprFieldToArgument(t, param)
case *ref.MappingRef:
param.Type = funcexprtype.REF
param.Value = a
case *ref.ArrayRef:
param.Type = funcexprtype.ARRAYREF
param.Value = a
case []interface{}:
//TODO
log.Debug("New Arguments type is []interface{}")
case interface{}:
//TODO
log.Debugf("New Arguments type is interface{}, [%+v]", reflect.TypeOf(a))
}
parameters = append(parameters, param)
return parameters, nil
}
func exprFieldToArgument(ex *expr.Expression, param *function.Parameter) {
if ex != nil {
switch ex.Type {
case funcexprtype.INTEGER, funcexprtype.ARRAYREF, funcexprtype.BOOLEAN, funcexprtype.FLOAT, funcexprtype.REF, funcexprtype.STRING:
param.Type = ex.Type
param.Value = ex.Value
case funcexprtype.FUNCTION:
param.Type = ex.Type
param.Function = ex.Value.(*function.FunctionExp)
}
}
}
func NewArguments(as ...Attribute) (interface{}, error) {
parameters := []*function.Parameter{}
for _, a := range as {
param := &function.Parameter{}
switch a.(type) {
case *token.Token:
param.Type = funcexprtype.STRING
param.Value = string(a.(*token.Token).Lit)
case int:
param.Type = funcexprtype.INTEGER
param.Value = a
case float64:
param.Type = funcexprtype.FLOAT
param.Value = a
case string:
param.Type = funcexprtype.STRING
param.Value = a.(string)
case *function.FunctionExp:
param.Type = funcexprtype.FUNCTION
param.Function = a.(*function.FunctionExp)
case *ref.MappingRef:
param.Type = funcexprtype.REF
param.Value = a
case *ref.ArrayRef:
param.Type = funcexprtype.ARRAYREF
param.Value = a
case []*function.Parameter:
for _, p := range a.([]*function.Parameter) {
if !p.IsEmtpy() {
parameters = append(parameters, p)
}
}
case []interface{}:
log.Debugf("New Arguments type is []interface{}")
case interface{}:
log.Debugf("New Arguments type is interface{} %+v", a)
}
parameters = append(parameters, param)
}
return parameters, nil
}
func NewExpressionField(a Attribute) (interface{}, error) {
expression := getExpression(a)
return expression, nil
}
func NewExpression(left Attribute, op Attribute, right Attribute) (interface{}, error) {
expression := expr.NewExpression()
operator, found := expr.ToOperator(strings.TrimSpace(string(op.(*token.Token).Lit)))
if found {
expression.Operator = operator
} else {
return nil, errors.New("Unsupport operator " + string(op.(*token.Token).Lit))
}
expression.Left = getExpression(left)
expression.Right = getExpression(right)
expression.Type = funcexprtype.EXPRESSION
log.Debugf("New expression left [%+v] right [%s+v and operator [%s]", expression.Left, expression.Right, expression.Operator)
return expression, nil
}
func getExpression(ex Attribute) *expr.Expression {
expression := expr.NewExpression()
switch ex.(type) {
case int:
expression.Type = funcexprtype.INTEGER
expression.Value = ex.(int)
case float64:
expression.Type = funcexprtype.FLOAT
expression.Value = ex.(float64)
case string:
expression.Type = funcexprtype.STRING
expression.Value = ex.(string)
case bool:
expression.Type = funcexprtype.BOOLEAN
expression.Value = ex.(bool)
case *NIL:
expression.Type = funcexprtype.NIL
expression.Value = nil
case ref.MappingRef:
expression.Type = funcexprtype.REF
ref := ex.(ref.MappingRef)
expression.Value = ref
case *ref.MappingRef:
expression.Type = funcexprtype.REF
expression.Value = ex.(*ref.MappingRef).GetRef()
case *ref.ArrayRef:
expression.Type = funcexprtype.ARRAYREF
expression.Value = ex.(*ref.ArrayRef).GetRef()
case *function.FunctionExp:
expression.Type = funcexprtype.FUNCTION
expression.Value = ex.(*function.FunctionExp)
case *expr.Expression:
expression = ex.(*expr.Expression)
default:
log.Errorf("Unknow expression type [%s]", ex)
}
return expression
}
func NewTernaryExpression(first Attribute, second Attribute, third Attribute) (Attribute, error) {
log.Debugf("first [%+v] and type [%s]", first, reflect.TypeOf(first))
log.Debugf("second [%+v] and type [%s]", second, reflect.TypeOf(second))
log.Debugf("third [%+v] and type [%s]", third, reflect.TypeOf(third))
ternaryExp := &expr.TernaryExpressio{First: first, Second: second, Third: third}
return ternaryExp, nil
}
func RemoveQuote(quoteStr string) string {
if HasQuote(quoteStr) {
if strings.HasPrefix(quoteStr, `"`) || strings.HasPrefix(quoteStr, `'`) {
quoteStr = quoteStr[1 : len(quoteStr)-1]
}
}
return quoteStr
}
func HasQuote(quoteStr string) bool {
if strings.HasPrefix(quoteStr, `"`) && strings.HasSuffix(quoteStr, `"`) {
return true
}
if strings.HasPrefix(quoteStr, `'`) && strings.HasSuffix(quoteStr, `'`) {
return true
}
return false
}