forked from bombsimon/jtd-infer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inferred_schema.go
388 lines (319 loc) · 9.67 KB
/
inferred_schema.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package jtdinfer
import (
"reflect"
"strconv"
"time"
jtd "github.com/jsontypedef/json-typedef-go"
)
// SchemaType represents the type of schema element. It will map to the
// available types for JTD.
type SchemaType int8
// Available schema types.
const (
SchemaTypeUnknown SchemaType = iota
SchemaTypeAny
SchemaTypeBoolean
SchemaTypeNumber
SchemaTypeString
SchemaTypeTimestmap
SchemaTypeEnum
SchemaTypeArray
SchemaTypeProperties
SchemaTypeValues
SchemaTypeDiscriminator
SchemaTypeNullable
)
// Properties represents all required and optional properties which is the same as
// JSON objects.
type Properties struct {
Required map[string]*InferredSchema
Optional map[string]*InferredSchema
}
// Discriminator represents discriminators for the schema.
type Discriminator struct {
Discriminator string
Mapping map[string]*InferredSchema
}
// InferredSchema is the schema while being inferred that holds information
// about fields.
type InferredSchema struct {
SchemaType SchemaType
Number *InferredNumber
Enum map[string]struct{}
Array *InferredSchema
Properties Properties
Values *InferredSchema
Discriminator Discriminator
Nullable *InferredSchema
}
// NewInferredSchema will return a new, empty, `InferredSchema`.
func NewInferredSchema() *InferredSchema {
return &InferredSchema{}
}
// Infer will infer the schema by trying to mimic the way it's implemented in
// the Rust implementation at
// https://github.com/jsontypedef/json-typedef-infer/blob/master/src/inferred_schema.rs.
// Since we don't have enums of this kind in Go we're using a struct with
// pointers to a schema instead of wrapping the enums.
func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
if value == nil {
return &InferredSchema{
SchemaType: SchemaTypeNullable,
Nullable: i,
}
}
if i.SchemaType == SchemaTypeNullable {
return &InferredSchema{
SchemaType: SchemaTypeNullable,
Nullable: i.Nullable.Infer(value, hints),
}
}
if _, ok := value.(bool); ok && i.SchemaType == SchemaTypeUnknown {
return &InferredSchema{SchemaType: SchemaTypeBoolean}
}
// In Go all numbers from unmarshalling JSON will be represented as float64
// so this is the only type we need.
if v, ok := value.(float64); ok && i.SchemaType == SchemaTypeUnknown {
return &InferredSchema{
SchemaType: SchemaTypeNumber,
Number: NewNumber().Infer(v),
}
}
if v, ok := value.(string); ok && i.SchemaType == SchemaTypeUnknown {
if hints.IsEnumActive() {
return &InferredSchema{
SchemaType: SchemaTypeEnum,
Enum: map[string]struct{}{v: {}},
}
}
if _, err := time.Parse(time.RFC3339, v); err == nil {
return &InferredSchema{SchemaType: SchemaTypeTimestmap}
}
return &InferredSchema{SchemaType: SchemaTypeString}
}
if i.SchemaType == SchemaTypeUnknown && reflect.TypeOf(value).Kind() == reflect.Slice {
s := reflect.ValueOf(value)
subInfer := &InferredSchema{}
for i := 0; i < s.Len(); i++ {
subInfer = subInfer.Infer(s.Index(i).Interface(), hints.SubHints(strconv.Itoa(i)))
}
return &InferredSchema{
SchemaType: SchemaTypeArray,
Array: subInfer,
}
}
if m, ok := value.(map[string]any); ok && i.SchemaType == SchemaTypeUnknown {
if hints.IsValuesActive() {
subInfer := NewInferredSchema()
for k, v := range m {
subInfer = subInfer.Infer(v, hints.SubHints(k))
}
return &InferredSchema{
SchemaType: SchemaTypeValues,
Values: subInfer,
}
}
if discriminator, ok := hints.PeekActiveDiscriminator(); ok {
if mappingKey, ok := m[discriminator].(string); ok {
delete(m, discriminator)
return &InferredSchema{
SchemaType: SchemaTypeDiscriminator,
Discriminator: Discriminator{
Discriminator: discriminator,
Mapping: map[string]*InferredSchema{
mappingKey: NewInferredSchema().Infer(m, hints),
},
},
}
}
}
properties := make(map[string]*InferredSchema, 0)
for k, v := range m {
properties[k] = NewInferredSchema().Infer(v, hints.SubHints(k))
}
return &InferredSchema{
SchemaType: SchemaTypeProperties,
Properties: Properties{
Required: properties,
},
}
}
if i.SchemaType == SchemaTypeAny {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if _, ok := value.(bool); ok && i.SchemaType == SchemaTypeBoolean {
return &InferredSchema{SchemaType: SchemaTypeBoolean}
}
if i.SchemaType == SchemaTypeBoolean {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if v, ok := value.(float64); ok && i.SchemaType == SchemaTypeNumber {
return &InferredSchema{
SchemaType: SchemaTypeNumber,
Number: i.Number.Infer(v),
}
}
if i.SchemaType == SchemaTypeNumber {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if v, ok := value.(string); ok && i.SchemaType == SchemaTypeTimestmap {
if _, err := time.Parse(time.RFC3339, v); err == nil {
return &InferredSchema{SchemaType: SchemaTypeTimestmap}
}
return &InferredSchema{SchemaType: SchemaTypeString}
}
if i.SchemaType == SchemaTypeTimestmap {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if _, ok := value.(string); ok && i.SchemaType == SchemaTypeString {
return &InferredSchema{SchemaType: SchemaTypeString}
}
if i.SchemaType == SchemaTypeString {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if v, ok := value.(string); ok && i.SchemaType == SchemaTypeEnum {
i.Enum[v] = struct{}{}
return i
}
if i.SchemaType == SchemaTypeEnum {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if i.SchemaType == SchemaTypeArray && reflect.TypeOf(value).Kind() == reflect.Slice {
s := reflect.ValueOf(value)
subInfer := i.Array
for i := 0; i < s.Len(); i++ {
subInfer = subInfer.Infer(s.Index(i).Interface(), hints.SubHints(strconv.Itoa(i)))
}
return &InferredSchema{
SchemaType: SchemaTypeArray,
Array: subInfer,
}
}
if m, ok := value.(map[string]any); ok && i.SchemaType == SchemaTypeProperties {
ensureMap := func(m map[string]*InferredSchema) map[string]*InferredSchema {
if m != nil {
return m
}
return make(map[string]*InferredSchema, 0)
}
missingKeys := []string{}
for k := range i.Properties.Required {
if _, ok := m[k]; !ok {
missingKeys = append(missingKeys, k)
}
}
for _, k := range missingKeys {
subInfer := i.Properties.Required[k]
delete(i.Properties.Required, k)
i.Properties.Optional = ensureMap(i.Properties.Optional)
i.Properties.Optional[k] = subInfer
}
for k, v := range m {
if subInfer, ok := i.Properties.Required[k]; ok {
i.Properties.Required[k] = subInfer.Infer(v, hints.SubHints(k))
} else if subInfer, ok := i.Properties.Optional[k]; ok {
i.Properties.Optional[k] = subInfer.Infer(v, hints.SubHints(k))
} else {
i.Properties.Optional = ensureMap(i.Properties.Optional)
i.Properties.Optional[k] = NewInferredSchema().Infer(v, hints.SubHints(k))
}
}
return i
}
if i.SchemaType == SchemaTypeProperties {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if m, ok := value.(map[string]any); ok && i.SchemaType == SchemaTypeValues {
subInfer := i.Values
for k, v := range m {
subInfer = subInfer.Infer(v, hints.SubHints(k))
}
return &InferredSchema{
SchemaType: SchemaTypeValues,
Values: subInfer,
}
}
if i.SchemaType == SchemaTypeValues {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
if m, ok := value.(map[string]any); ok && i.SchemaType == SchemaTypeDiscriminator {
mappingKey, ok := m[i.Discriminator.Discriminator].(string)
if !ok {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
delete(m, i.Discriminator.Discriminator)
if _, ok := i.Discriminator.Mapping[mappingKey]; !ok {
i.Discriminator.Mapping[mappingKey] = NewInferredSchema()
}
i.Discriminator.Mapping[mappingKey] = i.Discriminator.Mapping[mappingKey].Infer(m, hints)
return i
}
if i.SchemaType == SchemaTypeDiscriminator {
return &InferredSchema{SchemaType: SchemaTypeAny}
}
return &InferredSchema{}
}
// IntoSchema will convert an `InferredSchema` to a final `Schema`.
func (i *InferredSchema) IntoSchema(hints Hints) Schema {
switch i.SchemaType {
case SchemaTypeUnknown, SchemaTypeAny:
return Schema{}
case SchemaTypeBoolean:
return Schema{Type: jtd.TypeBoolean}
case SchemaTypeNumber:
return Schema{
Type: i.Number.IntoType(hints.DefaultNumType),
}
case SchemaTypeString:
return Schema{Type: jtd.TypeString}
case SchemaTypeTimestmap:
return Schema{Type: jtd.TypeTimestamp}
case SchemaTypeEnum:
enum := make([]string, 0, len(i.Enum))
for k := range i.Enum {
enum = append(enum, k)
}
return Schema{Enum: enum}
case SchemaTypeArray:
elements := i.Array.IntoSchema(hints)
return Schema{Elements: &elements}
case SchemaTypeProperties:
var (
required map[string]Schema
optional map[string]Schema
)
if i.Properties.Required != nil {
required = make(map[string]Schema, len(i.Properties.Required))
for k, v := range i.Properties.Required {
required[k] = v.IntoSchema(hints)
}
}
if i.Properties.Optional != nil {
optional = make(map[string]Schema, len(i.Properties.Optional))
for k, v := range i.Properties.Optional {
optional[k] = v.IntoSchema(hints)
}
}
return Schema{
Properties: required,
OptionalProperties: optional,
}
case SchemaTypeValues:
values := i.Values.IntoSchema(hints)
return Schema{Values: &values}
case SchemaTypeDiscriminator:
mappings := map[string]Schema{}
for k, v := range i.Discriminator.Mapping {
mappings[k] = v.IntoSchema(hints)
}
return Schema{
Discriminator: i.Discriminator.Discriminator,
Mapping: mappings,
}
case SchemaTypeNullable:
schema := i.Nullable.IntoSchema(hints)
schema.Nullable = true
return schema
}
return Schema{}
}