Skip to content

Commit

Permalink
Merge pull request #244 from TIBCOSoftware/expr-mapper-log
Browse files Browse the repository at this point in the history
Remove bad log and make debug log more cleary for expr mapper
  • Loading branch information
lixingwang authored Oct 23, 2018
2 parents e9a7ef1 + 099bca7 commit c1d22d5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 56 deletions.
19 changes: 4 additions & 15 deletions core/mapper/exprmapper/expression/direction/direction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package direction

import (
"errors"
"github.com/TIBCOSoftware/flogo-lib/logger"
"reflect"
"strconv"
"strings"
Expand All @@ -13,7 +14,6 @@ import (
"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"
"github.com/TIBCOSoftware/flogo-lib/logger"
)

var log = logger.GetLogger("expression-direction")
Expand All @@ -22,7 +22,6 @@ type Attribute interface{}

func NewDoubleQuoteStringLit(lit interface{}) (string, error) {
str := strings.TrimSpace(string(lit.(*token.Token).Lit))
log.Debugf("New double qutoes [%s]", str)

if str != "" && len(str) > 0 {
str = RemoveQuote(str)
Expand All @@ -32,14 +31,11 @@ func NewDoubleQuoteStringLit(lit interface{}) (string, error) {
str = strings.Replace(str, "\\\"", "\"", -1)
}

log.Debugf("Final double qutoes [%s]", str)

return str, nil
}

func NewSingleQuoteStringLit(lit interface{}) (string, error) {
str := strings.TrimSpace(string(lit.(*token.Token).Lit))
log.Debugf("New single qutoe [%s]", str)

if str != "" && len(str) > 0 {
str = RemoveQuote(str)
Expand All @@ -50,8 +46,6 @@ func NewSingleQuoteStringLit(lit interface{}) (string, error) {
str = strings.Replace(str, "\\'", "'", -1)
}

log.Debugf("Final single qutoe [%s]", str)

return str, nil
}

Expand Down Expand Up @@ -82,7 +76,6 @@ func NewNilLit(lit interface{}) (*NIL, error) {

func NewMappingRef(lit interface{}) (interface{}, error) {
s := strings.TrimSpace(string(lit.(*token.Token).Lit))
log.Debugf("New mapping ref and value [%s]", s)
if strings.HasPrefix(s, "$.") || strings.HasPrefix(s, "$$") {
m := ref.NewArrayRef(s)
return m, nil
Expand All @@ -93,8 +86,6 @@ func NewMappingRef(lit interface{}) (interface{}, error) {
}

func NewFunction(name Attribute, parameters Attribute) (interface{}, error) {
log.Debugf("New function name type [%s] and parameter type [%s]:", reflect.TypeOf(name), reflect.TypeOf(parameters))

f_func := &function.FunctionExp{}
to := name.(*token.Token)
f_func.Name = string(to.Lit)
Expand All @@ -109,11 +100,12 @@ func NewFunction(name Attribute, parameters Attribute) (interface{}, error) {
}
}
}

log.Debug(f_func.Tostring())
return f_func, nil
}

func NewArgument(a Attribute) (interface{}, error) {
log.Debugf("New Argument and type [%s]", reflect.TypeOf(a))
param := &function.Parameter{}
parameters := []*function.Parameter{}
switch t := a.(type) {
Expand Down Expand Up @@ -177,7 +169,6 @@ func exprFieldToArgument(ex *expr.Expression, param *function.Parameter) {
}

func NewArguments(as ...Attribute) (interface{}, error) {
log.Debugf("New Arguments and type [%s]", reflect.TypeOf(as))
parameters := []*function.Parameter{}
for _, a := range as {
param := &function.Parameter{}
Expand Down Expand Up @@ -220,14 +211,11 @@ func NewArguments(as ...Attribute) (interface{}, error) {
}

func NewExpressionField(a Attribute) (interface{}, error) {
log.Debugf("New Expression field [%+v] and type [%s]", a, reflect.TypeOf(a))
expression := getExpression(a)
return expression, nil
}

func NewExpression(left Attribute, op Attribute, right Attribute) (interface{}, error) {
log.Debugf("New Expression and operator [%s]", string(op.(*token.Token).Lit))

expression := expr.NewExpression()
operator, found := expr.ToOperator(strings.TrimSpace(string(op.(*token.Token).Lit)))
if found {
Expand All @@ -239,6 +227,7 @@ func NewExpression(left Attribute, op Attribute, right Attribute) (interface{},
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
}

Expand Down
35 changes: 14 additions & 21 deletions core/mapper/exprmapper/expression/expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,26 @@ func (e *Expression) IsFunction() bool {
}

func (f *Expression) Eval() (interface{}, error) {
log.Debug("Expression eval method....")
return f.evaluate(nil, nil, nil)
}

func (f *Expression) EvalWithScope(inputScope data.Scope, resolver data.Resolver) (interface{}, error) {
log.Debug("Expression eval method....")
return f.evaluate(nil, inputScope, resolver)
}

func (f *Expression) EvalWithData(data interface{}, inputScope data.Scope, resolver data.Resolver) (interface{}, error) {
log.Debug("Expression eval method....")
return f.evaluate(data, inputScope, resolver)
}

func (f *Expression) evaluate(data interface{}, inputScope data.Scope, resolver data.Resolver) (interface{}, error) {
log.Debug("Expression evaluate method....")
//Left
leftResultChan := make(chan interface{}, 1)
rightResultChan := make(chan interface{}, 1)
if f.IsNil() {
log.Debugf("Expression right and left are nil, return value directly")
return f.Value, nil
}

go f.Left.do(data, inputScope, resolver, leftResultChan)
go f.Right.do(data, inputScope, resolver, rightResultChan)

Expand All @@ -281,9 +278,6 @@ func (f *Expression) evaluate(data interface{}, inputScope data.Scope, resolver
case error:
return nil, rightValue.(error)
}

log.Debugf("Left value ", leftValue, " Type ", reflect.TypeOf(leftValue))
log.Debugf("Right value ", rightValue, " Type ", reflect.TypeOf(rightValue))
//Operator
operator := f.Operator

Expand All @@ -294,7 +288,6 @@ func (f *Expression) do(edata interface{}, inputScope data.Scope, resolver data.
if f == nil {
resultChan <- nil
}
log.Debug("Do left and expression ", f)
var leftValue interface{}
if f.IsFunction() {
funct := f.Value.(*function.FunctionExp)
Expand Down Expand Up @@ -377,7 +370,7 @@ func (f *Expression) run(left interface{}, op OPERATIOR, right interface{}) (int
}

func equals(left interface{}, right interface{}) (bool, error) {
log.Debugf("Left expression value %+v, right expression value %+v", left, right)
log.Debugf("Equals condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return true, nil
} else if left == nil && right != nil {
Expand All @@ -391,7 +384,7 @@ func equals(left interface{}, right interface{}) (bool, error) {
return false, err
}

log.Debugf("Right expression value [%s]", rightValue)
log.Debugf("Equals condition -> right expression value [%s]", rightValue)

return leftValue == rightValue, nil
}
Expand Down Expand Up @@ -455,7 +448,7 @@ func ConvertToSameType(left interface{}, right interface{}) (interface{}, interf

func notEquals(left interface{}, right interface{}) (bool, error) {

log.Debugf("Left expression value %+v, right expression value %+v", left, right)
log.Debugf("Not equals condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand All @@ -469,15 +462,15 @@ func notEquals(left interface{}, right interface{}) (bool, error) {
return false, err
}

log.Debugf("Right expression value [%s]", rightValue)
log.Debugf("Not equals condition -> right expression value [%s]", rightValue)

return leftValue != rightValue, nil

}

func gt(left interface{}, right interface{}, includeEquals bool) (bool, error) {

log.Debugf("Left expression value %+v, right expression value %+v", left, right)
log.Debugf("Greater than condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand All @@ -486,7 +479,7 @@ func gt(left interface{}, right interface{}, includeEquals bool) (bool, error) {
return false, nil
}

log.Debugf("Left value [%+v] and Right value: [%+v]", left, right)
log.Debugf("Greater than condition -> right value [%+v] and Right value: [%+v]", left, right)
rightType := getType(right)
switch le := left.(type) {
case int:
Expand Down Expand Up @@ -551,7 +544,7 @@ func gt(left interface{}, right interface{}, includeEquals bool) (bool, error) {

func lt(left interface{}, right interface{}, includeEquals bool) (bool, error) {

log.Debugf("Left expression value %+v, right expression value %+v", left, right)
log.Debugf("Less than condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand Down Expand Up @@ -621,7 +614,7 @@ func lt(left interface{}, right interface{}, includeEquals bool) (bool, error) {

func add(left interface{}, right interface{}) (bool, error) {

log.Infof("Add operator, Left expression value %+v, right expression value %+v", left, right)
log.Infof("Add condition -> left expression value %+v, right expression value %+v", left, right)

switch le := left.(type) {
case bool:
Expand All @@ -639,7 +632,7 @@ func add(left interface{}, right interface{}) (bool, error) {

func or(left interface{}, right interface{}) (bool, error) {

log.Infof("Add operator, Left expression value %+v, right expression value %+v", left, right)
log.Infof("Or condition -> left expression value %+v, right expression value %+v", left, right)
switch le := left.(type) {
case bool:
rightValue, err := data.CoerceToBoolean(right)
Expand All @@ -656,7 +649,7 @@ func or(left interface{}, right interface{}) (bool, error) {

func additon(left interface{}, right interface{}) (interface{}, error) {

log.Infof("Left expression value %+v, right expression value %+v", left, right)
log.Infof("Addition condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand Down Expand Up @@ -708,7 +701,7 @@ func additon(left interface{}, right interface{}) (interface{}, error) {

func sub(left interface{}, right interface{}) (interface{}, error) {

log.Debugf("Left expression value %+v, right expression value %+v", left, right)
log.Debugf("Sub condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand Down Expand Up @@ -760,7 +753,7 @@ func sub(left interface{}, right interface{}) (interface{}, error) {

func multiplication(left interface{}, right interface{}) (interface{}, error) {

log.Infof("Left expression value %+v, right expression value %+v", left, right)
log.Infof("Multiplication condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand Down Expand Up @@ -812,7 +805,7 @@ func multiplication(left interface{}, right interface{}) (interface{}, error) {

func div(left interface{}, right interface{}) (interface{}, error) {

log.Debugf("Left expression value %+v, right expression value %+v", left, right)
log.Debugf("Div condition -> left expression value %+v, right expression value %+v", left, right)
if left == nil && right == nil {
return false, nil
} else if left == nil && right != nil {
Expand Down
30 changes: 15 additions & 15 deletions core/mapper/exprmapper/expression/function/function.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package function

import (
"bytes"
"encoding/json"
"errors"
"reflect"
Expand Down Expand Up @@ -72,10 +73,7 @@ func (p *Parameter) IsEmtpy() bool {
}

func (p *Parameter) IsFunction() bool {
if funcexprtype.FUNCTION == p.Type {
return true
}
return false
return funcexprtype.FUNCTION == p.Type
}

func (f *FunctionExp) Eval() (interface{}, error) {
Expand Down Expand Up @@ -142,10 +140,7 @@ func (f *FunctionExp) getMethod() (reflect.Value, error) {
}

method := value.MethodByName("Eval")
if method.IsValid() {
logrus.Debug("valid")
} else {
logrus.Debug("invalid")
if !method.IsValid() {
method = ptr.MethodByName("Eval")
if !method.IsValid() {
logrus.Debug("invalid also, ", f.Name)
Expand All @@ -157,11 +152,16 @@ func (f *FunctionExp) getMethod() (reflect.Value, error) {
return method, nil
}

func convertToFunctionName(name string) string {
if name != "" {
return strings.Title(name)
func (f *FunctionExp) Tostring() string {
var buffer bytes.Buffer

buffer.WriteString(fmt.Sprintf("function name [%s] ", f.Name))
for i, param := range f.Params {
if param != nil {
buffer.WriteString(fmt.Sprintf(" parameter [%d]'s type [%s] and value [%+v]", i, param.Type, param.Value))
}
}
return name
return buffer.String()
}

func (f *FunctionExp) callFunction(fdata interface{}, inputScope data.Scope, resolver data.Resolver) (results reflect.Value, err error) {
Expand All @@ -184,19 +184,18 @@ func (f *FunctionExp) callFunction(fdata interface{}, inputScope data.Scope, res
if err != nil {
return reflect.Value{}, err
}
logrus.Debugf("function [%s] [%d]'s argument type [%s] and value [%+v]", f.Name, i, p.Type, result)
inputs = append(inputs, result)
} else {
if !p.IsEmtpy() {
if p.Type == funcexprtype.REF {
logrus.Debug("Mapping ref field should done before eval function.")
var field *ref.MappingRef
switch p.Value.(type) {
case string:
field = ref.NewMappingRef(p.Value.(string))
case *ref.MappingRef:
field = p.Value.(*ref.MappingRef)
}
//TODO
if inputScope == nil {
p.Value = field.GetRef()
} else {
Expand All @@ -209,7 +208,6 @@ func (f *FunctionExp) callFunction(fdata interface{}, inputScope data.Scope, res
}

} else if p.Type == funcexprtype.ARRAYREF {
logrus.Debug("Mapping ref field should done before eval function.")
var field *ref.ArrayRef
switch p.Value.(type) {
case string:
Expand Down Expand Up @@ -241,6 +239,8 @@ func (f *FunctionExp) callFunction(fdata interface{}, inputScope data.Scope, res

}
}

logrus.Debugf("function [%s] [%d]'s argument type [%s] and value [%+v]", f.Name, i, p.Type, p.Value)
if p.Value != nil {
inputs = append(inputs, reflect.ValueOf(p.Value))
} else {
Expand Down
2 changes: 2 additions & 0 deletions core/mapper/exprmapper/exprmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ func GetExpresssionValue(mappingV interface{}, inputScope data.Scope, resolver d
exp, err := expression.ParseExpression(mappingValue)
if err == nil {
//flogo expression
log.Debugf("[%s] is an valid expression", mappingValue)
expValue, err := exp.EvalWithScope(inputScope, resolver)
if err != nil {
return nil, fmt.Errorf("Execution failed for mapping [%s] due to error - %s", mappingValue, err.Error())
}
return expValue, nil
} else {
log.Debugf("[%s] is not an expression, take it as assign", mappingValue)
return assign.GetMappingValue(mappingV, inputScope, resolver)
}
}
2 changes: 0 additions & 2 deletions core/mapper/exprmapper/json/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func GetFieldValue(data interface{}, mappingField *field.MappingField) (interfac
}

func handleGetValue(jsonData *JSONData, fields []string) (interface{}, error) {

log.Debugf("All fields %+v", fields)
jsonData.rw.Lock()
defer jsonData.rw.Unlock()

Expand Down
Loading

0 comments on commit c1d22d5

Please sign in to comment.