/
extractor.go
167 lines (135 loc) · 3.83 KB
/
extractor.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
package jsonconfig
import (
"encoding/json"
"fmt"
"regexp"
"github.com/efritz/api-test/config"
"github.com/ghodss/yaml"
"github.com/xeipuuv/gojsonschema"
)
type (
TypeEnvelope struct {
Type string `json:"type"`
}
JQValueExtractor struct {
Expr string `json:"expr"`
IsList bool `json:"list"`
Assert json.RawMessage `json:"assert"`
Header string `json:"header"`
}
RegexValueExtractor struct {
Pattern string `json:"pattern"`
Assert json.RawMessage `json:"assert"`
Header string `json:"header"`
}
RegexValueAssertion struct {
Pattern string `json:"pattern"`
}
JSONSchemaValueAssertion struct {
Schema string `json:"schema"`
}
)
var (
unmarshalExtractFuncs = map[string]func(json.RawMessage) (*config.ValueExtractor, error){
"jq": unmarshalJQValueExtractor,
"regex": unmarshalRegexValueExtractor,
}
unmarshalAssertionFuncs = map[string]func(json.RawMessage) (*config.ValueAssertion, error){
"regex": unmarshalRegexValueAssertion,
"jsonschema": unmarshalJSONSchemaValueAssertion,
}
)
//
// Extractors
func unmarshalValueExtractor(payload json.RawMessage) (*config.ValueExtractor, error) {
envelope := &TypeEnvelope{}
if err := json.Unmarshal(payload, &envelope); err != nil {
return nil, err
}
fn, ok := unmarshalExtractFuncs[envelope.Type]
if !ok {
return nil, fmt.Errorf("unknown extractor type '%s'", envelope.Type)
}
return fn(payload)
}
func unmarshalJQValueExtractor(payload json.RawMessage) (*config.ValueExtractor, error) {
properties := &JQValueExtractor{}
if err := json.Unmarshal(payload, &properties); err != nil {
return nil, err
}
assertion, err := unmarshalValueAssertion(properties.Assert)
if err != nil {
return nil, err
}
return &config.ValueExtractor{
JQ: properties.Expr,
IsList: properties.IsList,
Assert: assertion,
Header: properties.Header,
}, nil
}
func unmarshalRegexValueExtractor(payload json.RawMessage) (*config.ValueExtractor, error) {
properties := &RegexValueExtractor{}
if err := json.Unmarshal(payload, &properties); err != nil {
return nil, err
}
pattern, err := regexp.Compile(properties.Pattern)
if err != nil {
return nil, fmt.Errorf("illegal extractor regex")
}
assertion, err := unmarshalValueAssertion(properties.Assert)
if err != nil {
return nil, err
}
return &config.ValueExtractor{
Pattern: pattern,
Assert: assertion,
Header: properties.Header,
}, nil
}
//
// Assertions
func unmarshalValueAssertion(payload json.RawMessage) (*config.ValueAssertion, error) {
if payload == nil {
return nil, nil
}
envelope := &TypeEnvelope{}
if err := json.Unmarshal(payload, &envelope); err != nil {
return nil, err
}
fn, ok := unmarshalAssertionFuncs[envelope.Type]
if !ok {
return nil, fmt.Errorf("unknown assertion type '%s'", envelope.Type)
}
return fn(payload)
}
func unmarshalRegexValueAssertion(payload json.RawMessage) (*config.ValueAssertion, error) {
properties := &RegexValueAssertion{}
if err := json.Unmarshal(payload, &properties); err != nil {
return nil, err
}
pattern, err := regexp.Compile(properties.Pattern)
if err != nil {
return nil, fmt.Errorf("illegal assertion regex")
}
return &config.ValueAssertion{
Pattern: pattern,
}, nil
}
func unmarshalJSONSchemaValueAssertion(payload json.RawMessage) (*config.ValueAssertion, error) {
properties := &JSONSchemaValueAssertion{}
if err := json.Unmarshal(payload, &properties); err != nil {
return nil, err
}
rawSchema, err := yaml.YAMLToJSON([]byte(properties.Schema))
if err != nil {
return nil, fmt.Errorf("failed to convert schema to JSON (%s)", err.Error())
}
schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader(rawSchema))
if err != nil {
return nil, fmt.Errorf("Failed to load schema (%s)", err.Error())
}
return &config.ValueAssertion{
Schema: schema,
}, nil
}