-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
input_json.go
274 lines (247 loc) · 5.69 KB
/
input_json.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
package trdsql
// Convert JSON to a table.
// Supports the following JSON container types.
// * Array ([{c1: 1}, {c1: 2}, {c1: 3}])
// * Multiple JSON ({c1: 1}\n {c1: 2}\n {c1: 3}\n)
// Make a table from json
// or make the result of json filter by jq.
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"github.com/itchyny/gojq"
)
// JSONReader provides methods of the Reader interface.
type JSONReader struct {
reader *json.Decoder
query *gojq.Query
already map[string]bool
inNULL string
preRead []map[string]any
names []string
types []string
limitRead bool
needNULL bool
}
// NewJSONReader returns JSONReader and error.
func NewJSONReader(reader io.Reader, opts *ReadOpts) (*JSONReader, error) {
r := &JSONReader{}
r.reader = json.NewDecoder(reader)
r.reader.UseNumber()
r.already = make(map[string]bool)
var top any
if opts.InJQuery != "" {
str := trimQuoteAll(opts.InJQuery)
query, err := gojq.Parse(str)
if err != nil {
return nil, fmt.Errorf("%w gojq:(%s)", err, opts.InJQuery)
}
r.query = query
}
r.limitRead = opts.InLimitRead
r.needNULL = opts.InNeedNULL
r.inNULL = opts.InNULL
for i := 0; i < opts.InPreRead; i++ {
if err := r.reader.Decode(&top); err != nil {
if !errors.Is(err, io.EOF) {
return r, fmt.Errorf("%w: %s", ErrInvalidJSON, err)
}
debug.Printf(err.Error())
return r, nil
}
if r.query != nil {
if err := r.jqueryRun(top); err != nil {
return nil, err
}
return r, nil
}
if err := r.readAhead(top); err != nil {
return nil, err
}
}
return r, nil
}
// Names returns column names.
func (r *JSONReader) Names() ([]string, error) {
return r.names, nil
}
// Types returns column types.
// All JSON types return the DefaultDBType.
func (r *JSONReader) Types() ([]string, error) {
r.types = make([]string, len(r.names))
for i := 0; i < len(r.names); i++ {
r.types[i] = DefaultDBType
}
return r.types, nil
}
// readAhead parses the top level of the JSON and stores it in preRead.
func (r *JSONReader) readAhead(top any) error {
switch m := top.(type) {
case []any:
// []
r.preRead = make([]map[string]any, 0, len(m))
if r.reader.More() {
pre, names, err := r.etcRow(m)
if err != nil {
return err
}
r.appendNames(names)
r.preRead = append(r.preRead, pre)
return nil
}
for _, v := range m {
pre, names, err := r.topLevel(v)
if err != nil {
return err
}
r.appendNames(names)
r.preRead = append(r.preRead, pre)
}
return nil
default:
pre, names, err := r.topLevel(m)
if err != nil {
return err
}
r.appendNames(names)
r.preRead = append(r.preRead, pre)
}
return nil
}
// appendNames adds multiple names for the argument to be unique.
func (r *JSONReader) appendNames(names []string) {
for _, name := range names {
if !r.already[name] {
r.already[name] = true
r.names = append(r.names, name)
}
}
}
func (r *JSONReader) topLevel(top any) (map[string]any, []string, error) {
switch obj := top.(type) {
case map[string]any:
return r.objectRow(obj)
default:
return r.etcRow(obj)
}
}
// PreReadRow is returns only columns that store preRead rows.
// One json (not jsonl) returns all rows with preRead.
func (r *JSONReader) PreReadRow() [][]any {
rows := make([][]any, len(r.preRead))
for n, v := range r.preRead {
rows[n] = make([]any, len(r.names))
for i := range r.names {
rows[n][i] = v[r.names[i]]
if r.needNULL {
rows[n][i] = replaceNULL(r.inNULL, rows[n][i])
}
}
}
return rows
}
// ReadRow is read the rest of the row.
// Only jsonl requires ReadRow in json.
func (r *JSONReader) ReadRow(row []any) ([]any, error) {
if r.limitRead {
return nil, io.EOF
}
var data any
if err := r.reader.Decode(&data); err != nil {
return nil, err
}
if r.query != nil {
return r.jqueryRunJsonl(row, data)
}
return r.rowParse(row, data), nil
}
func (r *JSONReader) rowParse(row []any, jsonRow any) []any {
switch m := jsonRow.(type) {
case map[string]any:
for i := range r.names {
row[i] = r.jsonString(m[r.names[i]])
}
default:
for i := range r.names {
row[i] = nil
}
row[0] = r.jsonString(jsonRow)
}
return row
}
func (r *JSONReader) objectRow(obj map[string]any) (map[string]any, []string, error) {
// {"a":"b"} object
names := make([]string, 0, len(obj))
row := make(map[string]any)
for k, v := range obj {
names = append(names, k)
row[k] = r.jsonString(v)
}
return row, names, nil
}
func (r *JSONReader) etcRow(val any) (map[string]any, []string, error) {
// ex. array array
// [["a"],
// ["b"]]
var names []string
k := "c1"
names = append(names, k)
row := make(map[string]any)
row[k] = r.jsonString(val)
return row, names, nil
}
// jqueryRun is a gojq.Run for json.
func (r *JSONReader) jqueryRun(top any) error {
iter := r.query.Run(top)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return fmt.Errorf("%w gojq:(%s) ", err, r.query)
}
if err := r.readAhead(v); err != nil {
return err
}
}
return nil
}
// jqueryRunJsonl gojq.Run for rows of jsonl.
func (r *JSONReader) jqueryRunJsonl(row []any, jsonRow any) ([]any, error) {
iter := r.query.Run(jsonRow)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
debug.Printf("%s gojq: %s", err.Error(), r.query)
continue
}
row = r.rowParse(row, v)
}
return row, nil
}
// jsonString returns the string of the argument.
func (r *JSONReader) jsonString(val any) any {
var str string
switch val.(type) {
case nil:
return nil
case map[string]any, []any:
b, err := json.Marshal(val)
if err != nil {
log.Printf("ERROR: jsonString:%s", err)
}
str = ValString(b)
default:
str = ValString(val)
}
if r.needNULL {
return replaceNULL(r.inNULL, str)
}
return str
}