-
Notifications
You must be signed in to change notification settings - Fork 6
/
transform.go
296 lines (256 loc) · 7.98 KB
/
transform.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package transform
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/PaesslerAG/jsonpath"
"github.com/antchfx/xmlquery"
)
type transformMethod int32
const (
first transformMethod = iota
last
concatenate
)
// transformOperation defines the interface for operations that are implemented within the transform schema.
type transformOperation interface {
init(args map[string]string) error
transform(in interface{}) (interface{}, error)
}
type transformOperationJSON struct {
Name string `json:"type"`
Args map[string]string `json:"args"`
}
// transformInstruction defines a jsonPath and xmlPath for a transform and an optional set of operations to be performed on the
// data from that path.
type transformInstruction struct {
// For jsonPath format see http://goessner.net/articles/JsonPath/
jsonPath string
// For XPath format see https://devhints.io/xpath
xmlPath string
Operations []transformOperation `json:"operations"`
}
type transformInstructionJSON struct {
JSONPath string `json:"jsonPath"`
XMLPath string `json:"xmlPath"`
Operations []transformOperationJSON `json:"operations"`
}
// UnmarshalJSON implements the json.Unmarshaler interface, this function exists to properly map the transformOperation.
func (ti *transformInstruction) UnmarshalJSON(data []byte) error {
var jti transformInstructionJSON
if err := json.Unmarshal(data, &jti); err != nil {
return fmt.Errorf("failed to extract transform from JSON: %v", err)
}
ti.jsonPath = jti.JSONPath
ti.xmlPath = jti.XMLPath
ti.Operations = []transformOperation{}
for _, toj := range jti.Operations {
var op transformOperation
switch toj.Name {
case "changeCase":
op = &changeCase{}
case "currentTime":
op = ¤tTime{}
case "duration":
op = &duration{}
case "inverse":
op = &inverse{}
case "max":
op = &max{}
case "replace":
op = &replace{}
case "split":
op = &split{}
case "timeParse":
op = &timeParse{}
case "toCamelCase":
op = &toCamelCase{}
default:
return fmt.Errorf("unsupported operation %q", toj.Name)
}
if err := op.init(toj.Args); err != nil {
return fmt.Errorf("failed initializing transform operation: %v", err)
}
ti.Operations = append(ti.Operations, op)
}
return nil
}
func (ti *transformInstruction) xmlTransform(in interface{}, fieldType string, modifier pathModifier) (interface{}, error) {
path := ti.xmlPath
if modifier != nil {
path = modifier(path)
}
node, ok := in.(*xmlquery.Node)
if !ok {
return nil, errors.New("Error converting input to *xmlquery.Node")
}
xmlNode := xmlquery.Find(node, path)
if xmlNode == nil {
return nil, nil
}
var (
value interface{}
err error
)
//num elements that have a child element
numElementsWithChild := len(xmlquery.Find(node, path+"[*]"))
//num elements without child
numElementsWithoutChild := len(xmlquery.Find(node, path+"[not(*)]"))
//if only numElementsWithoutChild has results then the nodes are leaf nodes and can extract value
if numElementsWithChild == 0 && numElementsWithoutChild == 1 {
value, err = convert(xmlNode[0].InnerText(), fieldType)
} else {
switch fieldType {
case "array", "object":
value = xmlNode
case "string":
values := make([]string, len(xmlNode))
for i, node := range xmlNode {
values[i] = node.InnerText()
}
value = strings.Join(values, " ")
default:
value, err = convert(xmlNode[0].InnerText(), fieldType)
if err != nil {
value = xmlNode
}
}
}
if value == nil {
return nil, nil
}
for _, op := range ti.Operations {
value, err = op.transform(value)
if err != nil {
return nil, fmt.Errorf("failed operation on value from xmlPath %q: %v", path, err)
}
}
return value, nil
}
func (ti *transformInstruction) jsonTransform(in interface{}, fieldType string, modifier pathModifier) (interface{}, error) {
path := ti.jsonPath
if modifier != nil {
path = modifier(path)
}
rawValue, err := jsonpath.Get(path, in)
if err != nil {
return nil, nil
}
if rawValue == nil {
return nil, nil
}
value, err := convert(rawValue, fieldType)
if err != nil {
// In some cases the conversion is helpful but in others like before a max operation it isn't
value = rawValue
}
if value == nil {
return nil, nil
}
for _, op := range ti.Operations {
value, err = op.transform(value)
if err != nil {
return nil, fmt.Errorf("failed operation on value from jsonPath %q: %v", path, err)
}
}
return value, nil
}
// transform runs the instructions in this object returning the new transformed value or an error if unable to.
// It handles the logic for finding the value to be transformed and chaining the Operations.
// It will not error if the value is not found, rather it returns nil for the value.
// If a conversion or operation fails an error is returned.
func (ti *transformInstruction) transform(in interface{}, fieldType string, modifier pathModifier, format inputFormat) (interface{}, error) {
if format == xmlInput {
return ti.xmlTransform(in, fieldType, modifier)
}
if format == jsonInput {
return ti.jsonTransform(in, fieldType, modifier)
}
return nil, errors.New("no path type specified for transform")
}
// trransformInstructions defines a set of instructions and a method for combining their results.
// The default method is to take the first non-nil result.
type transformInstructions struct {
From []*transformInstruction `json:"from"`
Method transformMethod `json:"method"`
MethodOptions methodOptions `json:"methodOptions"`
}
type transformInstructionsJSON struct {
From []*transformInstruction `json:"from"`
Method string `json:"method"`
MethodOptions methodOptions `json:"methodOptions"`
}
type methodOptions struct {
ConcatenateDelimiter string `json:"concatenateDelimiter"`
}
// UnmarshalJSON implements the json.Unmarshaler interface, this function exists to properly map the method.
func (tis *transformInstructions) UnmarshalJSON(data []byte) error {
var jtis transformInstructionsJSON
if err := json.Unmarshal(data, &jtis); err != nil {
return fmt.Errorf("failed to extract transform from JSON: %v", err)
}
tis.From = jtis.From
tis.MethodOptions = jtis.MethodOptions
switch jtis.Method {
case "":
tis.Method = 0
case "first":
tis.Method = first
case "last":
tis.Method = last
case "concatenate":
tis.Method = concatenate
default:
return fmt.Errorf("unknown method %q", jtis.Method)
}
return nil
}
// transform runs the instructions in this object returning the new transformed value or nil if none is found.
// It handles the logic for concatenation, first or last methods.
func (tis *transformInstructions) transform(in interface{}, fieldType string, modifier pathModifier, format inputFormat) (interface{}, error) {
var concatResult bool
switch tis.Method {
case last:
var newFrom []*transformInstruction
for i := len(tis.From) - 1; i >= 0; i-- {
newFrom = append(newFrom, tis.From[i])
}
tis.From = newFrom
case concatenate:
concatResult = true
}
var result interface{}
for _, from := range tis.From {
value, err := from.transform(in, fieldType, modifier, format)
if err != nil {
return nil, err
}
if concatResult {
delimiter := tis.MethodOptions.ConcatenateDelimiter
result, err = concat(result, value, delimiter)
if err != nil {
return nil, fmt.Errorf("failed to concat values: %v", err)
}
continue
}
if value != nil {
result = value
break
}
}
return result, nil
}
// replaceJSONPathPrefix will switch old for new in the path of the transform instructions if the path starts with
// old.
func (tis *transformInstructions) replaceJSONPathPrefix(old, new string) {
for _, instruction := range tis.From {
if strings.HasPrefix(instruction.jsonPath, old) {
instruction.jsonPath = strings.Replace(instruction.jsonPath, old, new, 1)
}
if strings.HasPrefix(instruction.xmlPath, old) {
instruction.xmlPath = strings.Replace(instruction.xmlPath, old, new, 1)
}
}
}
type transform map[string]transformInstructions