github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/mapper/exprmapper/expression/direction/direction.go (about) 1 package direction 2 3 import ( 4 "github.com/TIBCOSoftware/flogo-lib/logger" 5 "reflect" 6 "strconv" 7 "strings" 8 9 "github.com/TIBCOSoftware/flogo-lib/core/data" 10 11 "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/expr" 12 "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function" 13 "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/gocc/token" 14 "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/ref" 15 ) 16 17 var log = logger.GetLogger("expression-direction") 18 19 type Attribute interface{} 20 21 func NewDoubleQuoteStringLit(lit interface{}) (string, error) { 22 str := strings.TrimSpace(string(lit.(*token.Token).Lit)) 23 24 if str != "" && len(str) > 0 { 25 str = RemoveQuote(str) 26 } 27 //Eascap string 28 if strings.Contains(str, "\\\"") { 29 str = strings.Replace(str, "\\\"", "\"", -1) 30 } 31 32 return str, nil 33 } 34 35 func NewSingleQuoteStringLit(lit interface{}) (string, error) { 36 str := strings.TrimSpace(string(lit.(*token.Token).Lit)) 37 38 if str != "" && len(str) > 0 { 39 str = RemoveQuote(str) 40 } 41 42 //Eascap string 43 if strings.Contains(str, "\\'") { 44 str = strings.Replace(str, "\\'", "'", -1) 45 } 46 47 return str, nil 48 } 49 50 func NewIntLit(lit interface{}) (int, error) { 51 str := strings.TrimSpace(string(lit.(*token.Token).Lit)) 52 s, err := data.CoerceToInteger(str) 53 return s, err 54 } 55 56 func NewNagtiveIntLit(lit interface{}) (int, error) { 57 str := strings.TrimSpace(string(lit.(*token.Token).Lit)) 58 s, err := data.CoerceToInteger(str) 59 return -s, err 60 } 61 62 func NewFloatLit(lit interface{}) (float64, error) { 63 str := strings.TrimSpace(string(lit.(*token.Token).Lit)) 64 s, err := data.CoerceToDouble(str) 65 return s, err 66 } 67 68 func NewNagtiveFloatLit(lit interface{}) (float64, error) { 69 str := strings.TrimSpace(string(lit.(*token.Token).Lit)) 70 s, err := data.CoerceToDouble(str) 71 return -s, err 72 } 73 74 func NewBool(lit interface{}) (bool, error) { 75 s := strings.TrimSpace(string(lit.(*token.Token).Lit)) 76 b, err := strconv.ParseBool(s) 77 return b, err 78 } 79 80 type NIL struct { 81 } 82 83 func NewNilLit(lit interface{}) (*NIL, error) { 84 return &NIL{}, nil 85 } 86 87 func NewMappingRef(lit interface{}) (interface{}, error) { 88 s := strings.TrimSpace(string(lit.(*token.Token).Lit)) 89 if strings.HasPrefix(s, "$.") || strings.HasPrefix(s, "$$") { 90 m := ref.NewArrayRef(s) 91 return m, nil 92 } else { 93 m := ref.NewMappingRef(s) 94 return m, nil 95 } 96 } 97 98 func NewFunction(name Attribute, parameters Attribute) (interface{}, error) { 99 f_func := &function.FunctionExp{} 100 to := name.(*token.Token) 101 f_func.Name = string(to.Lit) 102 103 switch parameters.(type) { 104 case *function.Parameter: 105 f_func.Params = append(f_func.Params, parameters.(*function.Parameter)) 106 case []*function.Parameter: 107 for _, p := range parameters.([]*function.Parameter) { 108 f_func.Params = append(f_func.Params, p) 109 } 110 } 111 112 return f_func, nil 113 } 114 115 func NewArgument(a Attribute) (interface{}, error) { 116 parameters := []*function.Parameter{} 117 switch a.(type) { 118 case *NIL: 119 param := &function.Parameter{Value: expr.NewLiteralExpr(nil)} 120 parameters = append(parameters, param) 121 case expr.Expr: 122 param := &function.Parameter{Value: a.(expr.Expr)} 123 parameters = append(parameters, param) 124 case []*function.Parameter: 125 for _, p := range a.([]*function.Parameter) { 126 parameters = append(parameters, p) 127 } 128 case []interface{}: 129 //TODO 130 log.Debug("New Arguments type is []interface{}") 131 case interface{}: 132 //TODO 133 log.Debugf("New Arguments type is interface{}, [%+v]", reflect.TypeOf(a)) 134 } 135 return parameters, nil 136 } 137 138 func NewArguments(as ...Attribute) (interface{}, error) { 139 parameters := []*function.Parameter{} 140 for _, a := range as { 141 switch a.(type) { 142 case *NIL: 143 param := &function.Parameter{Value: expr.NewLiteralExpr(nil)} 144 parameters = append(parameters, param) 145 case expr.Expr: 146 param := &function.Parameter{Value: a.(expr.Expr)} 147 parameters = append(parameters, param) 148 case []*function.Parameter: 149 for _, p := range a.([]*function.Parameter) { 150 parameters = append(parameters, p) 151 } 152 default: 153 param := &function.Parameter{Value: expr.NewLiteralExpr(a)} 154 parameters = append(parameters, param) 155 } 156 } 157 return parameters, nil 158 } 159 160 func NewExpressionField(a Attribute) (interface{}, error) { 161 expression := getExpression(a) 162 return expression, nil 163 } 164 165 func NewLiteralExpr(a Attribute) (interface{}, error) { 166 var literalExpr expr.Expr 167 switch t := a.(type) { 168 case expr.Expr: 169 literalExpr = t 170 case *NIL: 171 literalExpr = expr.NewLiteralExpr(nil) 172 default: 173 literalExpr = expr.NewLiteralExpr(t) 174 } 175 return literalExpr, nil 176 } 177 178 func NewExpression(left Attribute, op Attribute, right Attribute) (interface{}, error) { 179 expression := expr.NewExpression() 180 operator := strings.TrimSpace(string(op.(*token.Token).Lit)) 181 expression.Operator = operator 182 183 expression.Left = getExpression(left) 184 expression.Right = getExpression(right) 185 log.Debugf("New expression left [%+v] right [%s+v and operator [%s]", expression.Left, expression.Right, operator) 186 return expression, nil 187 } 188 189 func getExpression(ex Attribute) *expr.Expression { 190 expression := expr.NewExpression() 191 switch ex.(type) { 192 case expr.Expr: 193 expression.Value = ex.(expr.Expr) 194 default: 195 expression.Value = expr.NewLiteralExpr(ex) 196 } 197 return expression 198 } 199 200 func NewTernaryExpression(first Attribute, second Attribute, third Attribute) (Attribute, error) { 201 log.Debugf("first [%+v] and type [%s]", first, reflect.TypeOf(first)) 202 log.Debugf("second [%+v] and type [%s]", second, reflect.TypeOf(second)) 203 log.Debugf("third [%+v] and type [%s]", third, reflect.TypeOf(third)) 204 var firstExpr, secondExpr, thirdExpr expr.Expr 205 206 switch t := first.(type) { 207 case expr.Expr: 208 firstExpr = t 209 default: 210 firstExpr = expr.NewLiteralExpr(t) 211 } 212 213 switch t := second.(type) { 214 case expr.Expr: 215 secondExpr = t 216 default: 217 secondExpr = expr.NewLiteralExpr(t) 218 } 219 220 switch t := third.(type) { 221 case expr.Expr: 222 thirdExpr = t 223 default: 224 thirdExpr = expr.NewLiteralExpr(t) 225 } 226 227 ternaryExp := &expr.TernaryExpression{First: firstExpr, Second: secondExpr, Third: thirdExpr} 228 return ternaryExp, nil 229 230 } 231 232 func NewTernaryArgument(first Attribute) (Attribute, error) { 233 switch t := first.(type) { 234 case expr.Expr: 235 return t, nil 236 default: 237 return expr.NewLiteralExpr(t), nil 238 } 239 } 240 241 func RemoveQuote(quoteStr string) string { 242 if HasQuote(quoteStr) { 243 if strings.HasPrefix(quoteStr, `"`) || strings.HasPrefix(quoteStr, `'`) { 244 quoteStr = quoteStr[1 : len(quoteStr)-1] 245 } 246 } 247 return quoteStr 248 } 249 250 func HasQuote(quoteStr string) bool { 251 if strings.HasPrefix(quoteStr, `"`) && strings.HasSuffix(quoteStr, `"`) { 252 return true 253 } 254 255 if strings.HasPrefix(quoteStr, `'`) && strings.HasSuffix(quoteStr, `'`) { 256 return true 257 } 258 259 return false 260 }