-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.go
290 lines (234 loc) · 5.41 KB
/
processor.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
package main
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/draxil/json2nd/internal/json"
"github.com/draxil/json2nd/internal/options"
)
type processor struct {
in io.Reader
out io.Writer
options options.Set
buffered bool
}
// TODO: detect where not an object more tidily in path mode
// TODO: no error on bad path mode?
// TODO: builds?
// TODO: how should default be for formatting
// TODO: opt for un-formatted (OR THE OTHER WAY AROUND?)
// TODO: path to value?
func (p processor) run() error {
if p.in == nil {
return errNilInput()
}
js := json.New(p.in)
if p.options.Path != "" {
return p.handlePath(js)
}
c, err := js.Next()
if err != nil && err != io.EOF {
return peekErr(err)
}
if err == io.EOF {
return errNoJSON()
}
if c != '[' || p.options.PreserveArray {
return p.handleNonArray(js, c, true)
}
return p.handleArray(js)
}
func (p processor) handlePath(scan *json.JSON) error {
nodes := strings.Split(p.options.Path, ".")
return p.handlePathNodes(nodes, scan)
}
func (p processor) handlePathNodes(nodes []string, scan *json.JSON) error {
// shouldn't be possible? But never say never.
if len(nodes) == 0 {
return fmt.Errorf("novel error 1: please report")
}
next, nodes := nodes[0], nodes[1:]
if next == "" {
return errBlankPath()
}
found, err := scan.ScanForKeyValue(next)
if err != nil {
return err
}
if !found {
return errBadPath(next)
}
if len(nodes) == 0 {
clue, err := scan.Next()
if err != nil {
// TODO:
return err
}
if clue == '[' {
return p.handleArray(scan)
}
return p.handleNonArray(scan, clue, false)
}
return p.handlePathNodes(nodes, scan)
}
func (p processor) prepOut() (w io.Writer, finishOut func() error) {
if p.buffered {
bw := bufio.NewWriter(p.out)
return bw, bw.Flush
}
return p.out, func() error { return nil }
}
func (p processor) handleArray(js *json.JSON) error {
// shift the cursor from the start of the array:
js.MoveOff()
out, finishOut := p.prepOut()
arrayIDX := 0
for {
c, err := js.Next()
if err != nil {
return arrayNextError(arrayIDX, err)
}
if !json.SaneValueStart(c) {
return errBadArrayValueStart(c, arrayIDX)
}
n, err := js.WriteCurrentTo(out, true)
if err != nil {
return arrayJSONErr(err)
}
if n > 0 {
_, err := out.Write([]byte("\n"))
if err != nil {
return arrayJSONErr(err)
}
}
if n == 0 {
break
}
c, err = js.Next()
if err != nil {
return arrayNextError(arrayIDX, err)
}
if c == ']' {
break
}
if c == ',' {
js.MoveOff()
} else {
return fmt.Errorf("unexpected character: %c", c)
}
arrayIDX++
}
return finishOut()
}
func (p processor) handleNonArray(j *json.JSON, clue byte, topLevel bool) error {
out, finishOut := p.prepOut()
if p.options.ExpectArray {
return errNotArrayWas(guessJSONType(j.Peek()))
}
if !json.SaneValueStart(clue) {
return errPathLeadToBadValue(clue, p.options.Path)
}
for {
n, err := j.WriteCurrentTo(out, true)
if err != nil {
if err == io.EOF {
return errNonArrayEOF(guessJSONType(clue))
}
return err
}
if n > 0 {
_, err := out.Write([]byte("\n"))
if err != nil {
return err
}
}
// we don't continue if we scanned down to this level
if !topLevel {
break
}
// okay now we're in some kind of JSON stream like
// NDJSON, so look for the next thing
clue, err := j.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if clue == '[' && !p.options.PreserveArray {
return errArrayInStream()
}
}
return finishOut()
}
func guessJSONType(clue byte) string {
switch clue {
case '{':
return "object"
case '"':
return "string"
case '[':
return "array"
case 't':
return "boolean"
case 'f':
return "boolean"
case 'n':
return "null"
}
if (clue >= '0' && clue <= '9') || clue == '-' {
return "number"
}
return ""
}
func errNilInput() error {
return fmt.Errorf("nil input")
}
func errNotArrayWas(t string) error {
return fmt.Errorf("expected structure to be an array but found: %s", t)
}
func errNonArrayEOF(t string) error {
if t == "" {
return fmt.Errorf("JSON data ended prematurely")
}
return fmt.Errorf("file ended before the end of value (%s)", t)
}
func errBadPath(chunk string) error {
return fmt.Errorf("path node did not exist: %s", chunk)
}
func errBlankPath() error {
return fmt.Errorf("bad blank path node, did you have a double dot?")
}
func errPathLeadToBadValue(start byte, path string) error {
t := guessJSONType(start)
if t != "" {
return fmt.Errorf("path (%s) lead to %s not an object", path, t)
}
return fmt.Errorf("path (%s) lead to bad value start: %c", path, start)
}
func errBadArrayValueStart(start byte, index int) error {
return fmt.Errorf("at array index %d found something which doesn't look like a JSON value, starts with: %c", index, start)
}
func arrayNextError(index int, e error) error {
if e == io.EOF {
return errArrayEOF(index)
}
// TODO: process this?
return e
}
func errArrayEOF(index int) error {
return fmt.Errorf("at array index %d we ran out of data", index)
}
func errNoJSON() error {
return fmt.Errorf("no JSON data found")
}
func arrayJSONErr(e error) error {
return fmt.Errorf("array JSON decode error: %w", e)
}
func peekErr(e error) error {
return fmt.Errorf("error looking for JSON: %w", e)
}
func errArrayInStream() error {
return fmt.Errorf("Found an array in what seemed to be a JSON stream. Consider using -%s", options.OptPreserveArray)
}