-
Notifications
You must be signed in to change notification settings - Fork 33
/
value.go
359 lines (349 loc) · 8.55 KB
/
value.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
349
350
351
352
353
354
355
356
357
358
359
package jsonstruct
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/fatih/structtag"
)
// An value describes an observed value.
type value struct {
observations int
empties int
arrays int
bools int
boolStrings int
float64s int
float64Strings int
ints int
intStrings int
nulls int
objects int
strings int
times int // time.Time is an implicit more specific type than string.
arrayElements *value
allObjectProperties *value
objectProperties map[string]*value
}
type generateOptions struct {
exportNameFunc ExportNameFunc
imports map[string]struct{}
intType string
omitEmptyTags OmitEmptyTagsType
skipUnparsableProperties bool
stringTags bool
structTagNames []string
useJSONNumber bool
}
type goType struct {
typeStr string
omitEmpty bool
stringTag bool
}
// observe merges a into v.
func (v *value) observe(a any) *value {
if v == nil {
v = &value{}
}
v.observations++
switch a := a.(type) {
case []any:
v.arrays++
if len(a) == 0 {
v.empties++
}
if v.arrayElements == nil {
v.arrayElements = &value{}
}
for _, e := range a {
v.arrayElements = v.arrayElements.observe(e)
}
case bool:
v.bools++
if !a {
v.empties++
}
case float64:
v.float64s++
if a == 0 {
v.empties++
}
case int:
v.ints++
if a == 0 {
v.empties++
}
case nil:
v.nulls++
case map[string]any:
v.objects++
if len(a) == 0 {
v.empties++
}
if v.objectProperties == nil {
v.objectProperties = make(map[string]*value)
}
for property, value := range a {
v.allObjectProperties = v.allObjectProperties.observe(value)
v.objectProperties[property] = v.objectProperties[property].observe(value)
}
case string:
if a == "" {
v.empties++
}
if err := json.Unmarshal([]byte(a), new(bool)); err == nil {
v.boolStrings++
} else if err := json.Unmarshal([]byte(a), new(int)); err == nil {
v.float64Strings++
v.intStrings++
} else if err := json.Unmarshal([]byte(a), new(float64)); err == nil {
v.float64Strings++
}
if v.times == v.strings {
if _, err := time.Parse(time.RFC3339Nano, a); err == nil {
v.times++
}
}
v.strings++
case json.Number:
if _, err := a.Int64(); err == nil {
v.ints++
} else {
v.float64s++
}
}
return v
}
// goType returns the Go type of v.
func (v *value) goType(observations int, options *generateOptions) goType {
// Determine the number of distinct types observed.
distinctTypes := 0
if v.arrays > 0 {
distinctTypes++
}
if v.bools > 0 {
distinctTypes++
}
if v.float64s > 0 {
distinctTypes++
}
if v.ints > 0 {
distinctTypes++
}
if v.nulls > 0 {
distinctTypes++
}
if v.objects > 0 {
distinctTypes++
}
if v.strings > 0 {
distinctTypes++
}
// Based on the observed distinct types, find the most specific Go type.
switch {
case distinctTypes == 1 && v.arrays > 0:
fallthrough
case distinctTypes == 2 && v.arrays > 0 && v.nulls > 0:
elementGoType := v.arrayElements.goType(0, options)
return goType{
typeStr: "[]" + elementGoType.typeStr,
omitEmpty: v.arrays+v.nulls < observations && v.empties == 0,
}
case distinctTypes == 1 && v.bools > 0:
return goType{
typeStr: "bool",
omitEmpty: v.bools < observations && v.empties == 0,
}
case distinctTypes == 2 && v.bools > 0 && v.nulls > 0:
return goType{
typeStr: "*bool",
}
case distinctTypes == 1 && v.float64s > 0:
return goType{
typeStr: "float64",
omitEmpty: v.float64s < observations && v.empties == 0,
}
case distinctTypes == 2 && v.float64s > 0 && v.nulls > 0:
return goType{
typeStr: "*float64",
}
case distinctTypes == 1 && v.ints > 0:
return goType{
typeStr: options.intType,
omitEmpty: v.ints < observations && v.empties == 0,
}
case distinctTypes == 2 && v.ints > 0 && v.nulls > 0:
return goType{
typeStr: "*" + options.intType,
}
case distinctTypes == 2 && v.float64s > 0 && v.ints > 0:
omitEmpty := v.float64s+v.ints < observations && v.empties == 0
if options.useJSONNumber {
options.imports["encoding/json"] = struct{}{}
return goType{
typeStr: "json.Number",
omitEmpty: omitEmpty,
}
}
return goType{
typeStr: "float64",
omitEmpty: omitEmpty,
}
case distinctTypes == 3 && v.float64s > 0 && v.ints > 0 && v.nulls > 0:
if options.useJSONNumber {
options.imports["encoding/json"] = struct{}{}
return goType{
typeStr: "*json.Number",
}
}
return goType{
typeStr: "*float64",
}
case distinctTypes == 1 && v.objects > 0:
fallthrough
case distinctTypes == 2 && v.objects > 0 && v.nulls > 0:
if len(v.objectProperties) == 0 {
switch {
case observations == 0 && v.nulls == 0:
return goType{
typeStr: "struct{}",
}
case v.nulls > 0:
return goType{
typeStr: "*struct{}",
}
case v.objects == observations:
return goType{
typeStr: "struct{}",
}
default:
return goType{
typeStr: "*struct{}",
omitEmpty: v.objects < observations,
}
}
}
hasUnparsableProperties := false
for k := range v.objectProperties {
if strings.ContainsRune(k, ' ') {
hasUnparsableProperties = true
break
}
}
if hasUnparsableProperties && !options.skipUnparsableProperties {
valueGoType := v.allObjectProperties.goType(0, options)
return goType{
typeStr: "map[string]" + valueGoType.typeStr,
omitEmpty: v.objects+v.nulls < observations,
}
}
b := &bytes.Buffer{}
properties := sortedKeys(v.objectProperties)
fmt.Fprintf(b, "struct {\n")
var unparsableProperties []string
for _, property := range properties {
if isUnparsableProperty(property) {
unparsableProperties = append(unparsableProperties, property)
continue
}
goType := v.objectProperties[property].goType(v.objects, options)
var omitEmpty bool
switch {
case options.omitEmptyTags == OmitEmptyTagsNever:
omitEmpty = false
case options.omitEmptyTags == OmitEmptyTagsAlways:
omitEmpty = true
case options.omitEmptyTags == OmitEmptyTagsAuto:
omitEmpty = goType.omitEmpty
}
tags, _ := structtag.Parse("")
var structTagOptions []string
if omitEmpty {
structTagOptions = append(structTagOptions, "omitempty")
}
if goType.stringTag {
structTagOptions = append(structTagOptions, "string")
}
for _, structTagName := range options.structTagNames {
tag := &structtag.Tag{
Key: structTagName,
Name: property,
Options: structTagOptions,
}
_ = tags.Set(tag)
}
fmt.Fprintf(b, "%s %s `%s`\n", options.exportNameFunc(property), goType.typeStr, tags)
}
for _, property := range unparsableProperties {
fmt.Fprintf(b, "// %q cannot be unmarshalled into a struct field by encoding/json.\n", property)
}
fmt.Fprintf(b, "}")
switch {
case observations == 0:
return goType{
typeStr: b.String(),
}
case v.objects == observations:
return goType{
typeStr: b.String(),
}
case v.objects < observations && v.nulls == 0:
return goType{
typeStr: "*" + b.String(),
omitEmpty: true,
}
default:
return goType{
typeStr: "*" + b.String(),
omitEmpty: v.objects+v.nulls < observations,
}
}
case distinctTypes == 1 && v.strings > 0 && v.times == v.strings:
options.imports["time"] = struct{}{}
return goType{
typeStr: "time.Time",
omitEmpty: v.times < observations,
}
case distinctTypes == 1 && v.strings > 0:
switch {
case options.stringTags && v.strings == v.boolStrings:
return goType{
typeStr: "bool",
stringTag: true,
omitEmpty: v.boolStrings < v.observations,
}
case options.stringTags && v.strings == v.intStrings:
return goType{
typeStr: options.intType,
stringTag: true,
omitEmpty: v.intStrings < v.strings,
}
case options.stringTags && v.strings == v.float64Strings:
return goType{
typeStr: "float64",
stringTag: true,
omitEmpty: v.float64Strings < v.strings,
}
default:
return goType{
typeStr: "string",
omitEmpty: v.strings < observations && v.empties == 0,
}
}
case distinctTypes == 2 && v.strings > 0 && v.nulls > 0 && v.times == v.strings:
options.imports["time"] = struct{}{}
return goType{
typeStr: "*time.Time",
}
case distinctTypes == 2 && v.strings > 0 && v.nulls > 0:
return goType{
typeStr: "*string",
}
default:
return goType{
typeStr: "any",
omitEmpty: v.arrays+v.bools+v.float64s+v.ints+v.nulls+v.objects+v.strings < observations,
}
}
}