github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/mapper/assign/assign.go (about)

     1  package assign
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     7  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json"
     8  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json/field"
     9  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/ref"
    10  	"github.com/TIBCOSoftware/flogo-lib/logger"
    11  	"strings"
    12  )
    13  
    14  var log = logger.GetLogger("assign-mapper")
    15  
    16  func MapAssign(mapping *data.MappingDef, inputScope, outputScope data.Scope, resolver data.Resolver) error {
    17  	mappingValue, err := GetMappingValue(mapping.Value, inputScope, resolver)
    18  	if err != nil {
    19  		return err
    20  	}
    21  	err = SetValueToOutputScope(mapping.MapTo, outputScope, mappingValue)
    22  	if err != nil {
    23  		err = fmt.Errorf("Set value %+v to output [%s] error - %s", mappingValue, mapping.MapTo, err.Error())
    24  		log.Error(err)
    25  		return err
    26  	}
    27  	log.Debugf("Set value %+v to %s Done", mappingValue, mapping.MapTo)
    28  	return nil
    29  }
    30  
    31  func GetMappingValue(mappingV interface{}, inputScope data.Scope, resolver data.Resolver) (interface{}, error) {
    32  	mappingValue, ok := mappingV.(string)
    33  	if !ok {
    34  		return mappingV, nil
    35  	}
    36  	if !isMappingRef(mappingValue) {
    37  		log.Debug("Mapping value is literal set directly to field")
    38  		log.Debugf("Mapping ref %s and value %+v", mappingValue, mappingValue)
    39  		return mappingValue, nil
    40  	} else {
    41  		mappingref := ref.NewMappingRef(mappingValue)
    42  		mappingValue, err := mappingref.GetValue(inputScope, resolver)
    43  		if err != nil {
    44  			return nil, fmt.Errorf("Get value from ref [%s] error - %s", mappingref.GetRef(), err.Error())
    45  
    46  		}
    47  		log.Debugf("Mapping ref %s and value %+v", mappingValue, mappingValue)
    48  		return mappingValue, nil
    49  	}
    50  	return nil, nil
    51  }
    52  
    53  func isMappingRef(mappingref string) bool {
    54  	if mappingref == "" || !strings.HasPrefix(mappingref, "$") {
    55  		return false
    56  	}
    57  	return true
    58  }
    59  
    60  func SetValueToOutputScope(mapTo string, outputScope data.Scope, value interface{}) error {
    61  	mapField, err := field.ParseMappingField(mapTo)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	actRootField, err := ref.GetMapToAttrName(mapField)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	fields := mapField.Getfields()
    72  	if len(fields) == 1 && !ref.HasArray(fields[0]) {
    73  		//No complex mapping exist
    74  		return SetAttribute(actRootField, value, outputScope)
    75  	} else if ref.HasArray(fields[0]) || len(fields) > 1 {
    76  		//Complex mapping
    77  		return settValue(mapField, actRootField, outputScope, value)
    78  	} else {
    79  		return fmt.Errorf("No field name found for mapTo [%s]", mapTo)
    80  	}
    81  
    82  }
    83  
    84  func settValue(mapField *field.MappingField, fieldName string, outputScope data.Scope, value interface{}) error {
    85  	existValue, err := ref.GetValueFromOutputScope(mapField, outputScope)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	newValue, err2 := SetPathValue(existValue, mapField, value)
    90  	if err2 != nil {
    91  		return err2
    92  	}
    93  
    94  	return SetAttribute(fieldName, newValue, outputScope)
    95  }
    96  
    97  func SetPathValue(value interface{}, mapField *field.MappingField, attrvalue interface{}) (interface{}, error) {
    98  	pathfields, err := ref.GetMapToPathFields(mapField)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	log.Debugf("Set value %+v to fields %s", value, pathfields)
   104  	value, err = json.SetFieldValue(attrvalue, value, pathfields)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	return value, nil
   109  }
   110  
   111  func SetAttribute(fieldName string, value interface{}, outputScope data.Scope) error {
   112  	//Set Attribute value back to attribute
   113  	attribute, exist := outputScope.GetAttr(fieldName)
   114  	if exist {
   115  		switch attribute.Type() {
   116  		case data.TypeComplexObject:
   117  			complexObject := attribute.Value().(*data.ComplexObject)
   118  			newComplexObject := &data.ComplexObject{Metadata: complexObject.Metadata, Value: value}
   119  			outputScope.SetAttrValue(fieldName, newComplexObject)
   120  		default:
   121  			outputScope.SetAttrValue(fieldName, value)
   122  		}
   123  
   124  	} else {
   125  		return errors.New("Cannot found attribute " + fieldName + " at output scope")
   126  	}
   127  	return nil
   128  }