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

     1  package ref
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json/field"
     6  	"strings"
     7  
     8  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     9  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json"
    10  )
    11  
    12  type ArrayRef struct {
    13  	ref string
    14  }
    15  
    16  func (m *ArrayRef) GetRef() string {
    17  	return m.ref
    18  }
    19  
    20  func NewArrayRef(ref string) *ArrayRef {
    21  	return &ArrayRef{ref: ref}
    22  }
    23  
    24  func (m *ArrayRef) EvalFromData(data interface{}) (interface{}, error) {
    25  	log.Debugf("Eval mapping field and ref: %s", m.ref)
    26  	//
    27  	value, err := m.getValueFromRef(data, m.ref)
    28  	if err != nil {
    29  		log.Errorf("Get From from ref error %+v", err)
    30  	}
    31  
    32  	log.Debugf("Mapping ref eval result: %p", value)
    33  	return value, err
    34  }
    35  
    36  func (m *ArrayRef) Eval(inputScope, outputScope data.Scope) (interface{}, error) {
    37  
    38  	return nil, fmt.Errorf("Array ref not support eval")
    39  
    40  }
    41  
    42  func (m *ArrayRef) getValueFromRef(object interface{}, ref string) (interface{}, error) {
    43  	reference := GetFieldNameFromArrayRef(ref)
    44  	mapField, err := field.ParseMappingField(reference)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return json.GetFieldValue(object, mapField)
    49  }
    50  
    51  func GetFieldNameFromArrayRef(arrayRef string) string {
    52  	if arrayRef != "" {
    53  		if IsArrayMapping(arrayRef) {
    54  			return arrayRef[2:]
    55  		}
    56  	}
    57  	return arrayRef
    58  }
    59  
    60  func IsArrayMapping(ref string) bool {
    61  	if ref != "" {
    62  		return strings.HasPrefix(ref, "$.") || strings.HasPrefix(ref, "$$")
    63  	}
    64  	return false
    65  }