-
Notifications
You must be signed in to change notification settings - Fork 6
/
instance.go
563 lines (490 loc) · 16.2 KB
/
instance.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package transform
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/PaesslerAG/jsonpath"
"github.com/antchfx/xmlquery"
"github.com/buger/jsonparser"
)
// pathModifier is used to modify the JSON path of an instance to indicate
type pathModifier func(string) string
func pathReplace(old, new string, modifier pathModifier) pathModifier {
return func(path string) string {
if modifier != nil {
path = modifier(path)
}
path = strings.Replace(path, old, new, 1)
return path
}
}
// instanceTransformer represents a JSON schema instance with transform details in it.
// The primary function it performs is to transform (or not) data given as input.
// In some cases instances contain other instances, in such a case the children transformers are called as needed to
// build up each value depth first.
type instanceTransformer interface {
addChild(instanceTransformer) error
child() instanceTransformer // Arrays return a child object all others nil
path() string
selectChild(string) instanceTransformer // This returns nil for everything except objects
transform(interface{}, pathModifier) (interface{}, error)
}
// arrayTransformer represents a JSON instance type array in the case of a JSON transform or an array of xmlquery.Node in the case of an XML transform.
// in both cases the output will be JSON
type arrayTransformer struct {
childTransformer instanceTransformer
defaultValue []interface{}
jsonPath string
format inputFormat
transforms *transformInstructions
}
func newArrayTransformer(path, transformIdentifier string, raw json.RawMessage, format inputFormat) (*arrayTransformer, error) {
at := &arrayTransformer{
jsonPath: path,
format: format,
}
var err error
at.transforms, err = extractTransformInstructions(raw, transformIdentifier, path)
if err != nil {
return nil, err
}
rawDefault, err := schemaDefault(raw)
if err != nil {
return nil, err
}
if rawDefault != nil {
var ok bool
at.defaultValue, ok = rawDefault.([]interface{})
if !ok {
return nil, fmt.Errorf("default value for path %q is not an array", path)
}
}
return at, nil
}
func (at *arrayTransformer) addChild(child instanceTransformer) error {
at.childTransformer = child
return nil
}
func (at *arrayTransformer) baseValueJSON(in interface{}, path string, modifier pathModifier) ([]interface{}, bool, error) {
// 1. Use a transform if it exists
if at.transforms != nil {
rawValue, err := at.transforms.transform(in, "array", modifier, at.format)
if err != nil {
return nil, false, err
}
if rawValue != nil {
newValue, ok := rawValue.([]interface{})
if !ok {
newValue = []interface{}{rawValue}
}
return newValue, true, nil
}
}
// 2. Look for the same jsonPath in the input and use directly if possible.
rawValue, err := jsonpath.Get(path, in)
if err == nil && rawValue != nil {
newValue, ok := rawValue.([]interface{})
if !ok {
newValue = []interface{}{rawValue}
}
return newValue, false, nil
}
// 3. Fall back to the JSON Schema default value.
if at.defaultValue != nil {
return at.defaultValue, true, nil
}
return nil, false, nil
}
func (at *arrayTransformer) baseValueXML(in interface{}, path string, modifier pathModifier) ([]interface{}, bool, error) {
// 1. Use a transform if it exists
if at.transforms != nil {
rawValue, err := at.transforms.transform(in, "array", modifier, at.format)
if err != nil {
return nil, false, err
}
//if rawValue is an array of xml nodes we need to append them to newValue for return as []interface{}
xmlNodeArray, ok := rawValue.([]*xmlquery.Node)
if ok {
newValue := make([]interface{}, len(xmlNodeArray))
for i, item := range xmlNodeArray {
newValue[i] = item
}
return newValue, false, nil
}
if rawValue != nil {
newValue, ok := rawValue.([]interface{})
if !ok {
newValue = []interface{}{rawValue}
}
return newValue, true, nil
}
}
// 2. Fall back to the JSON Schema default value.
if at.defaultValue != nil {
return at.defaultValue, true, nil
}
return nil, false, nil
}
// baseValue routes to the correct arrayTransformer.baseValue format
func (at *arrayTransformer) baseValue(in interface{}, path string, modifier pathModifier) ([]interface{}, bool, error) {
if at.format == jsonInput {
return at.baseValueJSON(in, path, modifier)
}
if at.format == xmlInput {
return at.baseValueXML(in, path, modifier)
}
return nil, false, errors.New("unknown transform type in arrayTransformer baseValue")
}
func (at *arrayTransformer) child() instanceTransformer { return at.childTransformer }
func (at *arrayTransformer) path() string { return at.jsonPath }
func (at *arrayTransformer) selectChild(key string) instanceTransformer { return nil }
// arrayTransformJSON retrieves the value for this object by building the value for the base object and then adding in any
// transforms for all defined child fields.
func (at *arrayTransformer) arrayTransformJSON(in interface{}, modifier pathModifier) (interface{}, error) {
path := at.jsonPath
if modifier != nil {
path = modifier(path)
}
base, changed, err := at.baseValue(in, path, modifier)
if err != nil {
return nil, err
}
if changed {
// save the array base to in as children will use the value from this for their transforms
if path == "$" {
in = base
} else {
inMap, ok := in.(map[string]interface{})
if !ok {
return nil, errors.New("input is neither a JSON array nor object")
}
if err := saveInTree(inMap, path, base); err != nil {
return nil, fmt.Errorf("failed to save array transform to input data: %v", err)
}
}
}
if at.childTransformer == nil {
return base, nil
}
oldPath := path + "[*]"
newArray := make([]interface{}, 0, len(base))
for i := range base {
currentPath := path + fmt.Sprintf("[%d]", i)
childValue, err := at.childTransformer.transform(in, pathReplace(oldPath, currentPath, modifier))
if err != nil {
return nil, err
}
if childValue != nil {
newArray = append(newArray, childValue)
}
}
if len(newArray) == 0 {
return nil, nil
}
return newArray, nil
}
// arrayTransformXML retrieves the value for this object by building the value for the base object and then adding in any
// transforms for all defined child fields.
func (at *arrayTransformer) arrayTransformXML(in interface{}, modifier pathModifier) (interface{}, error) {
path := at.jsonPath
if modifier != nil {
path = modifier(path)
}
base, _, err := at.baseValue(in, path, modifier)
if err != nil {
return nil, err
}
if at.childTransformer == nil {
return base, nil
}
oldPath := path + "[*]"
newArray := make([]interface{}, 0, len(base))
for i := range base {
currentPath := path + fmt.Sprintf("[%d]", i)
childValue := base[i]
if _, ok := childValue.(*xmlquery.Node); ok {
childValue, err = at.childTransformer.transform(childValue, pathReplace(oldPath, currentPath, modifier))
if err != nil {
return nil, err
}
}
if childValue != nil {
newArray = append(newArray, childValue)
}
}
if len(newArray) == 0 {
return nil, nil
}
return newArray, nil
}
// transform routes to the correct array transform type
func (at *arrayTransformer) transform(in interface{}, modifier pathModifier) (interface{}, error) {
if at.format == jsonInput {
return at.arrayTransformJSON(in, modifier)
}
if at.format == xmlInput {
return at.arrayTransformXML(in, modifier)
}
return nil, fmt.Errorf("Unrecognized transform type %s in arraytransformer transform, must be 'JSON' or 'XML' ", at.format)
}
// objectTransformer represents a JSON instance of type object and associated transforms.
type objectTransformer struct {
children map[string]instanceTransformer
defaultValue map[string]interface{}
jsonPath string
format inputFormat
transforms *transformInstructions
}
func newObjectTransformer(path, transformIdentifier string, raw json.RawMessage, format inputFormat) (*objectTransformer, error) {
ot := &objectTransformer{
children: make(map[string]instanceTransformer),
jsonPath: path,
format: format,
}
var err error
ot.transforms, err = extractTransformInstructions(raw, transformIdentifier, path)
if err != nil {
return nil, err
}
rawDefault, err := schemaDefault(raw)
if err != nil {
return nil, err
}
if rawDefault != nil {
var ok bool
ot.defaultValue, ok = rawDefault.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid default for an object `%v`", rawDefault)
}
}
return ot, nil
}
func (ot *objectTransformer) addChild(child instanceTransformer) error {
pathSplits := strings.Split(child.path(), ".")
name := pathSplits[len(pathSplits)-1]
parentPath := strings.Join(pathSplits[:len(pathSplits)-1], ".")
if parentPath != ot.jsonPath {
return fmt.Errorf("path %q is not a child of %q", child.path(), ot.jsonPath)
}
ot.children[name] = child
return nil
}
func (ot *objectTransformer) child() instanceTransformer { return nil }
func (ot *objectTransformer) path() string { return ot.jsonPath }
func (ot *objectTransformer) selectChild(key string) instanceTransformer { return ot.children[key] }
// objectTransformJSON retrieves the value for this object by building the value for the base object and then adding in any
// transforms for all defined child fields.
func (ot *objectTransformer) objectTransformJSON(in interface{}, modifier pathModifier) (interface{}, error) {
path := ot.jsonPath
if modifier != nil {
path = modifier(path)
}
var newValue map[string]interface{}
// For the object use a transform if it exists or the default or an empty map
if ot.transforms != nil {
rawValue, err := ot.transforms.transform(in, "object", modifier, ot.format)
if err != nil {
return nil, err
}
if rawValue != nil {
var ok bool
newValue, ok = rawValue.(map[string]interface{})
if !ok {
return nil, errors.New("transform returned non-object value")
}
}
}
if newValue == nil {
if ot.defaultValue == nil {
newValue = make(map[string]interface{})
} else {
newValue = ot.defaultValue
}
}
// Add each child value to the paren
for _, child := range ot.children {
childValue, err := child.transform(in, modifier)
if err != nil {
return nil, err
}
savePath := strings.Replace(child.path(), ot.jsonPath, "$", 1)
if err := saveInTree(newValue, savePath, childValue); err != nil {
return nil, fmt.Errorf("path %q failed save: %v", child.path(), err)
}
}
if len(newValue) == 0 {
return nil, nil
}
return newValue, nil
}
// objectTransformXML retrieves the value for this object by building the value for the base object and then adding in any
// transforms for all defined child fields. If a transform is provided it transforms the children relative to the
// passed in node. If a transform is provided and not found the children of the object are skipped.
func (ot *objectTransformer) objectTransformXML(in interface{}, modifier pathModifier) (interface{}, error) {
path := ot.jsonPath
if modifier != nil {
path = modifier(path)
}
// For the object use a transform if it exists, if the transform does not find a node it will return nil unless a
// default value is specified in which case the default value will be returned
if ot.transforms != nil {
rawValue, err := ot.transforms.transform(in, "object", modifier, ot.format)
if err != nil {
return nil, err
}
if rawValue == nil {
if ot.defaultValue == nil {
return nil, nil
} else {
return ot.defaultValue, nil
}
}
switch v := rawValue.(type) {
case *xmlquery.Node:
in = v
case []*xmlquery.Node:
if len(v) > 0 {
in = v[0]
}
default:
return nil, errors.New("non xml node returned from object transform")
}
}
var newValue map[string]interface{}
if ot.defaultValue == nil {
newValue = make(map[string]interface{})
} else {
newValue = ot.defaultValue
}
// Add each child value to the parent if there is no object transform or if the object transform node is found
for _, child := range ot.children {
childValue, err := child.transform(in, modifier)
if err != nil {
return nil, err
}
savePath := strings.Replace(child.path(), ot.jsonPath, "$", 1)
if err := saveInTree(newValue, savePath, childValue); err != nil {
return nil, fmt.Errorf("path %q failed save: %v", child.path(), err)
}
}
if len(newValue) == 0 {
return nil, nil
}
return newValue, nil
}
// transform routes to the correct object transform type
func (ot *objectTransformer) transform(in interface{}, modifier pathModifier) (interface{}, error) {
if ot.format == jsonInput {
return ot.objectTransformJSON(in, modifier)
}
if ot.format == xmlInput {
return ot.objectTransformXML(in, modifier)
}
return nil, fmt.Errorf("Unrecognized transform type %s in objecttransformer transform, must be 'JSON' or 'XML' ", ot.format)
}
// scalarTransformer represents a JSON instance for a scalar type.
type scalarTransformer struct {
defaultValue interface{}
jsonType string
jsonPath string
format inputFormat
transforms *transformInstructions
}
func newScalarTransformer(path, transformIdentifier string, raw json.RawMessage, instanceType string, format inputFormat) (*scalarTransformer, error) {
st := &scalarTransformer{
jsonType: instanceType,
jsonPath: path,
format: format,
}
if instanceType == "string" {
instanceFormat, err := jsonparser.GetString(raw, "format")
if err != nil && err != jsonparser.KeyPathNotFoundError {
return nil, fmt.Errorf("failed to extract instance format: %v", err)
}
if instanceFormat == "date-time" {
st.jsonType = "date-time"
}
}
var err error
st.transforms, err = extractTransformInstructions(raw, transformIdentifier, path)
if err != nil {
return nil, err
}
st.defaultValue, err = schemaDefault(raw)
if err != nil {
return nil, err
}
return st, nil
}
func (st *scalarTransformer) addChild(instanceTransformer) error { return nil }
func (st *scalarTransformer) child() instanceTransformer { return nil }
func (st *scalarTransformer) path() string { return st.jsonPath }
func (st *scalarTransformer) selectChild(string) instanceTransformer { return nil }
// transformScalarJSON retrieves the value for a scalar instance following this process:
//
// 1. Use a Transform if it exists.
//
// 2. Look for the same jsonPath in the input and use directly if possible.
//
// 3. Fall back to the JSON Schema default value.
func (st *scalarTransformer) transformScalarJSON(in interface{}, modifier pathModifier) (interface{}, error) {
path := st.jsonPath
if modifier != nil {
path = modifier(path)
}
// 1. Use a transform if it exists
if st.transforms != nil {
newValue, err := st.transforms.transform(in, st.jsonType, modifier, st.format)
if err != nil {
return nil, err
}
if newValue != nil {
return newValue, nil
}
}
// 2. Look for the same jsonPath in the input and use directly if possible.
rawValue, err := jsonpath.Get(path, in)
if err == nil {
newValue, err := convert(rawValue, st.jsonType)
// if there is a conversion error fall through to the default
if newValue != nil {
return newValue, err
}
}
// 3. Fall back to the JSON Schema default value.
return st.defaultValue, nil
}
// transformScalarXML retrieves the value for a scalar instance following this process:
//
// 1. Use a Transform if it exists.
//
// 2. If transform does not exist or returns no value send back default
func (st *scalarTransformer) transformScalarXML(in interface{}, modifier pathModifier) (interface{}, error) {
path := st.jsonPath
if modifier != nil {
path = modifier(path)
}
// 1. Use a Transform if it exists.
if st.transforms != nil {
newValue, err := st.transforms.transform(in, st.jsonType, modifier, st.format)
if err != nil {
return nil, err
}
if newValue != nil {
return newValue, nil
}
}
// 2. If transform does not exist or returns no value send back default
return st.defaultValue, nil
}
// transform routes to the correct scalar transform type
func (st *scalarTransformer) transform(in interface{}, modifier pathModifier) (interface{}, error) {
if st.format == jsonInput {
return st.transformScalarJSON(in, modifier)
}
if st.format == xmlInput {
return st.transformScalarXML(in, modifier)
}
return nil, fmt.Errorf("Unrecognized transform type %s in scalartransformer transform, must be 'JSON' or 'XML' ", st.format)
}