forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.jsonparse.go
105 lines (93 loc) · 2.74 KB
/
task.jsonparse.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
package pipeline
import (
"context"
"encoding/json"
"math/big"
"strings"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
//
// Return types:
// float64
// string
// bool
// map[string]interface{}
// []interface{}
// nil
//
type JSONParseTask struct {
BaseTask `mapstructure:",squash"`
Path string `json:"path"`
Data string `json:"data"`
// Lax when disabled will return an error if the path does not exist
// Lax when enabled will return nil with no error if the path does not exist
Lax string
}
var _ Task = (*JSONParseTask)(nil)
func (t *JSONParseTask) Type() TaskType {
return TaskTypeJSONParse
}
func (t *JSONParseTask) Run(_ context.Context, vars Vars, inputs []Result) (result Result, runInfo RunInfo) {
_, err := CheckInputs(inputs, 0, 1, 0)
if err != nil {
return Result{Error: errors.Wrap(err, "task inputs")}, runInfo
}
var (
path JSONPathParam
data StringParam
lax BoolParam
)
err = multierr.Combine(
errors.Wrap(ResolveParam(&path, From(VarExpr(t.Path, vars), t.Path)), "path"),
errors.Wrap(ResolveParam(&data, From(VarExpr(t.Data, vars), Input(inputs, 0))), "data"),
errors.Wrap(ResolveParam(&lax, From(NonemptyString(t.Lax), false)), "lax"),
)
if err != nil {
return Result{Error: err}, runInfo
}
var decoded interface{}
err = json.Unmarshal([]byte(data), &decoded)
if err != nil {
return Result{Error: err}, runInfo
}
for _, part := range path {
switch d := decoded.(type) {
case map[string]interface{}:
var exists bool
decoded, exists = d[part]
if !exists && bool(lax) {
decoded = nil
break
} else if !exists {
return Result{Error: errors.Wrapf(ErrKeypathNotFound, `could not resolve path ["%v"] in %s`, strings.Join(path, `","`), data)}, runInfo
}
case []interface{}:
bigindex, ok := big.NewInt(0).SetString(part, 10)
if !ok {
return Result{Error: errors.Wrapf(ErrKeypathNotFound, "JSONParse task error: %v is not a valid array index", part)}, runInfo
} else if !bigindex.IsInt64() {
if bool(lax) {
decoded = nil
break
}
return Result{Error: errors.Wrapf(ErrKeypathNotFound, `could not resolve path ["%v"] in %s`, strings.Join(path, `","`), data)}, runInfo
}
index := int(bigindex.Int64())
if index < 0 {
index = len(d) + index
}
exists := index >= 0 && index < len(d)
if !exists && bool(lax) {
decoded = nil
break
} else if !exists {
return Result{Error: errors.Wrapf(ErrKeypathNotFound, `could not resolve path ["%v"] in %s`, strings.Join(path, `","`), data)}, runInfo
}
decoded = d[index]
default:
return Result{Error: errors.Wrapf(ErrKeypathNotFound, `could not resolve path ["%v"] in %s`, strings.Join(path, `","`), data)}, runInfo
}
}
return Result{Value: decoded}, runInfo
}