-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
json.go
348 lines (294 loc) · 9.03 KB
/
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
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
package gofakeit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
)
// JSONOptions defines values needed for json generation
type JSONOptions struct {
Type string `json:"type" xml:"type" fake:"{randomstring:[array,object]}"` // array or object
RowCount int `json:"row_count" xml:"row_count" fake:"{number:1,10}"`
Indent bool `json:"indent" xml:"indent"`
Fields []Field `json:"fields" xml:"fields" fake:"{fields}"`
}
type jsonKeyVal struct {
Key string
Value any
}
type jsonOrderedKeyVal []*jsonKeyVal
func (okv jsonOrderedKeyVal) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("{")
for i, kv := range okv {
// Add comma to all except last one
if i != 0 {
buf.WriteString(",")
}
// Marshal key and write
key, err := json.Marshal(kv.Key)
if err != nil {
return nil, err
}
buf.Write(key)
// Write colon separator
buf.WriteString(":")
// Marshal value and write
val, err := json.Marshal(kv.Value)
if err != nil {
return nil, err
}
buf.Write(val)
}
buf.WriteString("}")
return buf.Bytes(), nil
}
// JSON generates an object or an array of objects in json format.
// A nil JSONOptions returns a randomly structured JSON.
func JSON(jo *JSONOptions) ([]byte, error) { return jsonFunc(GlobalFaker, jo) }
// JSON generates an object or an array of objects in json format.
// A nil JSONOptions returns a randomly structured JSON.
func (f *Faker) JSON(jo *JSONOptions) ([]byte, error) { return jsonFunc(f, jo) }
// JSON generates an object or an array of objects in json format
func jsonFunc(f *Faker, jo *JSONOptions) ([]byte, error) {
if jo == nil {
// We didn't get a JSONOptions, so create a new random one
err := f.Struct(&jo)
if err != nil {
return nil, err
}
}
// Check to make sure they passed in a type
if jo.Type != "array" && jo.Type != "object" {
return nil, errors.New("invalid type, must be array or object")
}
if jo.Fields == nil || len(jo.Fields) <= 0 {
return nil, errors.New("must pass fields in order to build json object(s)")
}
if jo.Type == "object" {
v := make(jsonOrderedKeyVal, len(jo.Fields))
// Loop through fields and add to them to map[string]any
for i, field := range jo.Fields {
if field.Function == "autoincrement" {
// Object only has one
v[i] = &jsonKeyVal{Key: field.Name, Value: 1}
continue
}
// Get function info
funcInfo := GetFuncLookup(field.Function)
if funcInfo == nil {
return nil, errors.New("invalid function, " + field.Function + " does not exist")
}
// Call function value
value, err := funcInfo.Generate(f, &field.Params, funcInfo)
if err != nil {
return nil, err
}
if _, ok := value.([]byte); ok {
// If it's a slice, unmarshal it into an interface
var val any
err := json.Unmarshal(value.([]byte), &val)
if err != nil {
return nil, err
}
value = val
}
v[i] = &jsonKeyVal{Key: field.Name, Value: value}
}
// Marshal into bytes
if jo.Indent {
j, _ := json.MarshalIndent(v, "", " ")
return j, nil
}
j, _ := json.Marshal(v)
return j, nil
}
if jo.Type == "array" {
// Make sure you set a row count
if jo.RowCount <= 0 {
return nil, errors.New("must have row count")
}
v := make([]jsonOrderedKeyVal, jo.RowCount)
for i := 0; i < int(jo.RowCount); i++ {
vr := make(jsonOrderedKeyVal, len(jo.Fields))
// Loop through fields and add to them to map[string]any
for ii, field := range jo.Fields {
if field.Function == "autoincrement" {
vr[ii] = &jsonKeyVal{Key: field.Name, Value: i + 1} // +1 because index starts with 0
continue
}
// Get function info
funcInfo := GetFuncLookup(field.Function)
if funcInfo == nil {
return nil, errors.New("invalid function, " + field.Function + " does not exist")
}
// Call function value
value, err := funcInfo.Generate(f, &field.Params, funcInfo)
if err != nil {
return nil, err
}
if _, ok := value.([]byte); ok {
// If it's a slice, unmarshal it into an interface
var val any
err := json.Unmarshal(value.([]byte), &val)
if err != nil {
return nil, err
}
value = val
}
vr[ii] = &jsonKeyVal{Key: field.Name, Value: value}
}
v[i] = vr
}
// Marshal into bytes
if jo.Indent {
j, _ := json.MarshalIndent(v, "", " ")
return j, nil
}
j, _ := json.Marshal(v)
return j, nil
}
return nil, errors.New("invalid type, must be array or object")
}
func addFileJSONLookup() {
AddFuncLookup("json", Info{
Display: "JSON",
Category: "file",
Description: "Format for structured data interchange used in programming, returns an object or an array of objects",
Example: `[
{ "first_name": "Markus", "last_name": "Moen", "password": "Dc0VYXjkWABx" },
{ "first_name": "Osborne", "last_name": "Hilll", "password": "XPJ9OVNbs5lm" },
{ "first_name": "Mertie", "last_name": "Halvorson", "password": "eyl3bhwfV8wA" }
]`,
Output: "[]byte",
ContentType: "application/json",
Params: []Param{
{Field: "type", Display: "Type", Type: "string", Default: "object", Options: []string{"object", "array"}, Description: "Type of JSON, object or array"},
{Field: "rowcount", Display: "Row Count", Type: "int", Default: "100", Description: "Number of rows in JSON array"},
{Field: "indent", Display: "Indent", Type: "bool", Default: "false", Description: "Whether or not to add indents and newlines"},
{Field: "fields", Display: "Fields", Type: "[]Field", Description: "Fields containing key name and function to run in json format"},
},
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) {
jo := JSONOptions{}
typ, err := info.GetString(m, "type")
if err != nil {
return nil, err
}
jo.Type = typ
rowcount, err := info.GetInt(m, "rowcount")
if err != nil {
return nil, err
}
jo.RowCount = rowcount
indent, err := info.GetBool(m, "indent")
if err != nil {
return nil, err
}
jo.Indent = indent
fieldsStr, err := info.GetStringArray(m, "fields")
if err != nil {
return nil, err
}
// Check to make sure fields has length
if len(fieldsStr) > 0 {
jo.Fields = make([]Field, len(fieldsStr))
for i, f := range fieldsStr {
// Unmarshal fields string into fields array
err = json.Unmarshal([]byte(f), &jo.Fields[i])
if err != nil {
return nil, err
}
}
}
return jsonFunc(f, &jo)
},
})
}
// encoding/json.RawMessage is a special case of []byte
// it cannot be handled as a reflect.Array/reflect.Slice
// because it needs additional structure in the output
func rJsonRawMessage(f *Faker, v reflect.Value, tag string) error {
if tag != "" {
err := rCustom(f, v, tag)
if err == nil {
jsonData := v.Bytes()
if !json.Valid(jsonData) {
fName, _ := parseNameAndParamsFromTag(tag)
return errors.New("custom function " + fName + " returned invalid json data: " + string(jsonData))
}
}
return err
}
b, err := f.JSON(nil)
if err != nil {
return err
}
v.SetBytes(b)
return nil
}
// encoding/json.Number is a special case of string
// that represents a JSON number literal.
// It cannot be handled as a string because it needs to
// represent an integer or a floating-point number.
func rJsonNumber(f *Faker, v reflect.Value, tag string) error {
var ret json.Number
var numberType string
if tag == "" {
numberType = f.RandomString([]string{"int", "float"})
switch numberType {
case "int":
retInt := f.Int16()
ret = json.Number(strconv.Itoa(int(retInt)))
case "float":
retFloat := f.Float64()
ret = json.Number(strconv.FormatFloat(retFloat, 'f', -1, 64))
}
} else {
fName, fParams := parseNameAndParamsFromTag(tag)
info := GetFuncLookup(fName)
if info == nil {
return fmt.Errorf("invalid function, %s does not exist", fName)
}
// Parse map params
mapParams, err := parseMapParams(info, fParams)
if err != nil {
return err
}
valueIface, err := info.Generate(f, mapParams, info)
if err != nil {
return err
}
switch value := valueIface.(type) {
case int:
ret = json.Number(strconv.FormatInt(int64(value), 10))
case int8:
ret = json.Number(strconv.FormatInt(int64(value), 10))
case int16:
ret = json.Number(strconv.FormatInt(int64(value), 10))
case int32:
ret = json.Number(strconv.FormatInt(int64(value), 10))
case int64:
ret = json.Number(strconv.FormatInt(int64(value), 10))
case uint:
ret = json.Number(strconv.FormatUint(uint64(value), 10))
case uint8:
ret = json.Number(strconv.FormatUint(uint64(value), 10))
case uint16:
ret = json.Number(strconv.FormatUint(uint64(value), 10))
case uint32:
ret = json.Number(strconv.FormatUint(uint64(value), 10))
case uint64:
ret = json.Number(strconv.FormatUint(uint64(value), 10))
case float32:
ret = json.Number(strconv.FormatFloat(float64(value), 'f', -1, 64))
case float64:
ret = json.Number(strconv.FormatFloat(float64(value), 'f', -1, 64))
default:
return fmt.Errorf("invalid type, %s is not a valid type for json.Number", reflect.TypeOf(value))
}
}
v.Set(reflect.ValueOf(ret))
return nil
}