forked from orus-io/json-schema-generate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
519 lines (488 loc) · 13.4 KB
/
generator.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package generate
import (
"bytes"
"errors"
"fmt"
"strings"
"unicode"
"github.com/shopspring/decimal"
)
// Generator will produce structs from the JSON schema.
type Generator struct {
schemas []*Schema
resolver *RefResolver
Structs map[string]Struct
Aliases map[string]Field
OneOfs map[string]OneOf
// cache for reference types; k=url v=type
refs map[string]string
anonCount int
}
// New creates an instance of a generator which will produce structs.
func New(schemas ...*Schema) *Generator {
return &Generator{
schemas: schemas,
resolver: NewRefResolver(schemas),
Structs: make(map[string]Struct),
Aliases: make(map[string]Field),
OneOfs: make(map[string]OneOf),
refs: make(map[string]string),
}
}
// CreateTypes creates types from the JSON schemas, keyed by the golang name.
func (g *Generator) CreateTypes() (err error) {
if err := g.resolver.Init(); err != nil {
return err
}
// extract the types
for _, schema := range g.schemas {
name := g.getSchemaName("", schema)
rootType, err := g.processSchema(name, schema)
if err != nil {
return err
}
// ugh: if it was anything but a struct the type will not be the name...
if rootType != "*"+name {
a := Field{
Name: name,
JSONName: "",
Type: rootType,
Required: false,
Description: schema.Description,
}
g.Aliases[a.Name] = a
}
}
return
}
// process a block of definitions
func (g *Generator) processDefinitions(schema *Schema) error {
for key, subSchema := range schema.Definitions {
if _, err := g.processSchema(getGolangName(key), subSchema); err != nil {
return err
}
}
return nil
}
// process a reference string
func (g *Generator) processReference(schema *Schema) (string, error) {
schemaPath := g.resolver.GetPath(schema)
if schema.Reference == "" {
return "", errors.New("processReference empty reference: " + schemaPath)
}
refSchema, err := g.resolver.GetSchemaByReference(schema)
if err != nil {
return "", errors.New("processReference: reference \"" + schema.Reference + "\" not found at \"" + schemaPath + "\"")
}
if refSchema.GeneratedType == "" {
// reference is not resolved yet. Do that now.
refSchemaName := g.getSchemaName("", refSchema)
typeName, err := g.processSchema(refSchemaName, refSchema)
if err != nil {
return "", err
}
return typeName, nil
}
return refSchema.GeneratedType, nil
}
// returns the type refered to by schema after resolving all dependencies
func (g *Generator) processSchema(schemaName string, schema *Schema) (typ string, err error) {
if len(schema.Definitions) > 0 {
g.processDefinitions(schema)
}
schema.FixMissingTypeValue()
// if we have multiple schema types, the golang type will be interface{}
typ = "interface{}"
types, isMultiType := schema.MultiType()
if len(types) > 0 {
for _, schemaType := range types {
name := schemaName
if isMultiType {
name = name + "_" + schemaType
}
switch schemaType {
case "object":
rv, err := g.processObject(name, schema)
if err != nil {
return "", err
}
if !isMultiType {
return rv, nil
}
case "array":
rv, err := g.processArray(name, schema)
if err != nil {
return "", err
}
if !isMultiType {
return rv, nil
}
case "number":
if !schema.MultipleOf.IsZero() {
exp := schema.MultipleOf.Exponent()
if exp < 0 && schema.MultipleOf.Equal(decimal.New(1, exp)) {
if !isMultiType {
return "decimal.Decimal", nil
}
}
}
if !isMultiType {
return "float64", nil
}
default:
rv, err := getPrimitiveTypeName(schemaType, "", false)
if err != nil {
return "", err
}
if !isMultiType {
return rv, nil
}
}
}
} else if schema.Reference != "" {
return g.processReference(schema)
} else if len(schema.OneOf) != 0 {
return g.processOneOf(schemaName, schema)
}
return // return interface{}
}
func getOneOfTypeNull(typ string) string {
switch typ {
case "string":
return "OneOfStringNull"
case "float64":
return "OneOfNumberNull"
case "bool":
return "OneOfBoolNull"
default:
if !strings.HasPrefix(typ, "*") {
typ = "*" + typ
}
return typ
}
}
func (g *Generator) generateOneOf(schemaName string, schema *Schema) (string, error) {
var oneOf = OneOf{
Name: g.getSchemaName(schemaName, schema) + "Type",
Description: schema.Description,
}
for _, subSchema := range schema.OneOf {
typ, err := g.processSchema(schemaName+getGolangName(subSchema.Title), subSchema)
if err != nil {
return "", err
}
jsonType, _ := subSchema.Type()
shortType := typ
if subSchema.Title != "" {
shortType = getGolangName(subSchema.Title)
}
if strings.HasPrefix(shortType, "*") {
shortType = shortType[1:]
}
shortType = strings.ToUpper(shortType[:1]) + shortType[1:]
oneOf.Types = append(oneOf.Types, OneOfType{
ShortType: shortType,
Type: typ,
JSONType: jsonType,
})
}
g.OneOfs[oneOf.Name] = oneOf
return oneOf.Name, nil
}
func (g *Generator) processOneOf(schemaName string, schema *Schema) (typ string, err error) {
if len(schema.OneOf) == 2 {
type1, err := g.processSchema(schemaName, schema.OneOf[0])
if err != nil {
return "", err
}
if type1 == "interface{}" {
return type1, nil
}
type2, err := g.processSchema(schemaName, schema.OneOf[1])
if err != nil {
return "", err
}
if type2 == "interface{}" {
return type2, nil
}
if type1 == "nil" {
return getOneOfTypeNull(type2), nil
} else if type2 == "nil" {
return getOneOfTypeNull(type1), nil
}
}
return g.generateOneOf(schemaName, schema)
}
// name: name of this array, usually the js key
// schema: items element
func (g *Generator) processArray(name string, schema *Schema) (typeStr string, err error) {
if schema.Items != nil {
// subType: fallback name in case this array contains inline object without a title
subName := g.getSchemaName(name+"Items", schema.Items)
subTyp, err := g.processSchema(subName, schema.Items)
if err != nil {
return "", err
}
finalType, err := getPrimitiveTypeName("array", subTyp, true)
if err != nil {
return "", err
}
// only alias root arrays
if schema.Parent == nil {
array := Field{
Name: name,
JSONName: "",
Type: finalType,
Required: contains(schema.Required, name),
Description: schema.Description,
}
g.Aliases[array.Name] = array
}
return finalType, nil
}
return "[]interface{}", nil
}
// name: name of the struct (calculated by caller)
// schema: detail incl properties & child objects
// returns: generated type
func (g *Generator) processObject(name string, schema *Schema) (typ string, err error) {
strct := Struct{
ID: schema.ID(),
Name: name,
Description: schema.Description,
Fields: make(map[string]Field, len(schema.Properties)),
}
// cache the object name in case any sub-schemas recursively reference it
schema.GeneratedType = "*" + name
// regular properties
for propKey, prop := range schema.Properties {
fieldName := getGolangName(propKey)
// calculate sub-schema name here, may not actually be used depending on type of schema!
subSchemaName := g.getSchemaName(fieldName, prop)
fieldType, err := g.processSchema(subSchemaName, prop)
if err != nil {
return "", err
}
f := Field{
Name: fieldName,
JSONName: propKey,
Type: fieldType,
Required: contains(schema.Required, propKey),
Description: prop.Description,
}
for _, v := range prop.Enum {
f.Enum = append(f.Enum, string(v))
}
if f.Required {
strct.GenerateCode = true
}
strct.Fields[f.Name] = f
}
// additionalProperties with typed sub-schema
if schema.AdditionalProperties != nil && schema.AdditionalProperties.AdditionalPropertiesBool == nil {
ap := (*Schema)(schema.AdditionalProperties)
apName := g.getSchemaName("", ap)
subTyp, err := g.processSchema(apName, ap)
if err != nil {
return "", err
}
mapTyp := "map[string]" + subTyp
// If this object is inline property for another object, and only contains additional properties, we can
// collapse the structure down to a map.
//
// If this object is a definition and only contains additional properties, we can't do that or we end up with
// no struct
isDefinitionObject := strings.HasPrefix(schema.PathElement, "definitions")
if len(schema.Properties) == 0 && !isDefinitionObject {
// since there are no regular properties, we don't need to emit a struct for this object - return the
// additionalProperties map type.
return mapTyp, nil
}
// this struct will have both regular and additional properties
f := Field{
Name: "AdditionalProperties",
JSONName: "-",
Type: mapTyp,
Required: false,
Description: "",
}
strct.Fields[f.Name] = f
// setting this will cause marshal code to be emitted in Output()
strct.GenerateCode = true
strct.AdditionalType = subTyp
}
// additionalProperties as either true (everything) or false (nothing)
if schema.AdditionalProperties != nil && schema.AdditionalProperties.AdditionalPropertiesBool != nil {
if *schema.AdditionalProperties.AdditionalPropertiesBool == true {
// everything is valid additional
subTyp := "map[string]interface{}"
f := Field{
Name: "AdditionalProperties",
JSONName: "-",
Type: subTyp,
Required: false,
Description: "",
}
strct.Fields[f.Name] = f
// setting this will cause marshal code to be emitted in Output()
strct.GenerateCode = true
strct.AdditionalType = "interface{}"
} else {
// nothing
strct.GenerateCode = true
strct.AdditionalType = "false"
}
}
g.Structs[strct.Name] = strct
// objects are always a pointer
return getPrimitiveTypeName("object", name, true)
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func getPrimitiveTypeName(schemaType string, subType string, pointer bool) (name string, err error) {
switch schemaType {
case "array":
if subType == "" {
return "error_creating_array", errors.New("can't create an array of an empty subtype")
}
return "[]" + subType, nil
case "boolean":
return "bool", nil
case "integer":
return "int", nil
case "number":
return "float64", nil
case "null":
return "nil", nil
case "object":
if subType == "" {
return "error_creating_object", errors.New("can't create an object of an empty subtype")
}
if pointer {
return "*" + subType, nil
}
return subType, nil
case "string":
return "string", nil
}
return "undefined", fmt.Errorf("failed to get a primitive type for schemaType %s and subtype %s",
schemaType, subType)
}
// return a name for this (sub-)schema.
func (g *Generator) getSchemaName(keyName string, schema *Schema) string {
if len(schema.Title) > 0 {
return getGolangName(schema.Title)
}
if keyName != "" {
return getGolangName(keyName)
}
if schema.Parent == nil {
return "Root"
}
if schema.JSONKey != "" {
return getGolangName(schema.JSONKey)
}
if schema.Parent != nil && schema.Parent.JSONKey != "" {
return getGolangName(schema.Parent.JSONKey + "Item")
}
g.anonCount++
return fmt.Sprintf("Anonymous%d", g.anonCount)
}
// getGolangName strips invalid characters out of golang struct or field names.
func getGolangName(s string) string {
if s == "__type__" {
return "DataType"
}
buf := bytes.NewBuffer([]byte{})
for i, v := range splitOnAll(s, isNotAGoNameCharacter) {
if i == 0 && strings.IndexAny(v, "0123456789") == 0 {
// Go types are not allowed to start with a number, lets prefix with an underscore.
buf.WriteRune('_')
}
buf.WriteString(capitaliseFirstLetter(v))
}
return buf.String()
}
func splitOnAll(s string, shouldSplit func(r rune) bool) []string {
rv := []string{}
buf := bytes.NewBuffer([]byte{})
for _, c := range s {
if shouldSplit(c) {
rv = append(rv, buf.String())
buf.Reset()
} else {
buf.WriteRune(c)
}
}
if buf.Len() > 0 {
rv = append(rv, buf.String())
}
return rv
}
func isNotAGoNameCharacter(r rune) bool {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return false
}
return true
}
func capitaliseFirstLetter(s string) string {
if s == "" {
return s
}
prefix := s[0:1]
suffix := s[1:]
return strings.ToUpper(prefix) + suffix
}
// Struct defines the data required to generate a struct in Go.
type Struct struct {
// The ID within the JSON schema, e.g. #/definitions/address
ID string
// The golang name, e.g. "Address"
Name string
// Description of the struct
Description string
Fields map[string]Field
GenerateCode bool
AdditionalType string
}
// Field defines the data required to generate a field in Go.
type Field struct {
// The golang name, e.g. "Address1"
Name string
// The JSON name, e.g. "address1"
JSONName string
// The golang type of the field, e.g. a built-in type like "string" or the name of a struct generated
// from the JSON schema.
Type string
Enum []string
// Required is set to true when the field is required.
Required bool
Description string
}
// OneOfType is a type in a OneOf
type OneOfType struct {
ShortType string
Type string
JSONType string
}
// OneOf is a generated type for holding a oneOf field data
type OneOf struct {
Name string
Description string
Types []OneOfType
}
// GetByJSONType returns the type matching the given json type
func (o OneOf) GetByJSONType(t string) OneOfType {
for _, ot := range o.Types {
if ot.JSONType == t {
return ot
}
}
return OneOfType{}
}