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