github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/mapper/exprmapper/ref/outputref.go (about) 1 package ref 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json/field" 8 9 "github.com/TIBCOSoftware/flogo-lib/core/data" 10 ) 11 12 func GetValueFromOutputScope(mapfield *field.MappingField, outputtscope data.Scope) (interface{}, error) { 13 fieldName, err := GetMapToAttrName(mapfield) 14 if err != nil { 15 return nil, err 16 } 17 log.Debugf("GetValueFromOutputScope field name %s", fieldName) 18 19 attribute, exist := outputtscope.GetAttr(fieldName) 20 log.Debugf("GetValueFromOutputScope field name %s and exist %t ", fieldName, exist) 21 22 if exist { 23 switch attribute.Type() { 24 case data.TypeComplexObject: 25 complexObject := attribute.Value().(*data.ComplexObject) 26 object := complexObject.Value 27 //Convert the object to exist struct. 28 //TODO return interface rather than string 29 if object == nil { 30 return "{}", nil 31 } 32 return object, nil 33 default: 34 return attribute.Value(), nil 35 } 36 37 } 38 return nil, fmt.Errorf("Cannot found attribute %s", fieldName) 39 } 40 41 func GetMapToAttrName(field *field.MappingField) (string, error) { 42 fields := field.Getfields() 43 return getFieldName(fields[0]), nil 44 } 45 46 func GetMapToPathFields(mapField *field.MappingField) (*field.MappingField, error) { 47 fields := mapField.Getfields() 48 49 if len(fields) == 1 && !HasArray(fields[0]) { 50 return field.NewMappingField([]string{}), nil 51 } else if HasArray(fields[0]) { 52 arrayIndexPart := getArrayIndexPart(fields[0]) 53 fields[0] = arrayIndexPart 54 return field.NewMappingField(fields), nil 55 } else if len(fields) > 1 { 56 if strings.HasSuffix(fields[0], "]") { 57 //Root element is an array 58 arrayIndexPart := getArrayIndexPart(fields[0]) 59 fields[0] = arrayIndexPart 60 return field.NewMappingField(fields), nil 61 } else { 62 return field.NewMappingField(mapField.Getfields()[1:]), nil 63 } 64 } else { 65 //Only attribute name no field name 66 return field.NewMappingField([]string{}), nil 67 } 68 } 69 70 func getFieldName(fieldname string) string { 71 if strings.Index(fieldname, "[") > 0 && strings.Index(fieldname, "]") > 0 { 72 return fieldname[:strings.Index(fieldname, "[")] 73 } 74 return fieldname 75 } 76 77 func HasArray(fieldname string) bool { 78 if strings.Index(fieldname, "[") > 0 && strings.Index(fieldname, "]") > 0 { 79 return true 80 } 81 return false 82 } 83 84 //getArrayIndexPart get array part of the string. such as name[0] return [0] 85 func getArrayIndexPart(fieldName string) string { 86 if strings.Index(fieldName, "[") >= 0 { 87 return fieldName[strings.Index(fieldName, "[") : strings.Index(fieldName, "]")+1] 88 } 89 return "" 90 }