forked from a-h/generate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
452 lines (374 loc) · 10.6 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
// Package generate creates Go structs from JSON schemas.
package generate
import (
"bytes"
"errors"
"fmt"
"net/url"
"path"
"sort"
"strings"
"unicode"
"github.com/a-h/generate/jsonschema"
)
// Generator will produce structs from the JSON schema.
type Generator struct {
schemas []*jsonschema.Schema
}
// New creates an instance of a generator which will produce structs.
func New(schemas ...*jsonschema.Schema) *Generator {
return &Generator{
schemas: schemas,
}
}
// CreateTypes creates types from the JSON schemas, keyed by the golang name.
func (g *Generator) CreateTypes() (structs map[string]Struct, aliases map[string]Field, err error) {
schemaIDs := make([]*url.URL, len(g.schemas))
for i, schema := range g.schemas {
if schema.ID() != "" {
schemaIDs[i], err = url.Parse(schema.ID())
if err != nil {
return
}
}
}
// Extract nested and complex types from the JSON schemas.
types := map[string]*jsonschema.Schema{}
for i, schema := range g.schemas {
for name, typ := range schema.ExtractTypes() {
if schemaIDs[i] != nil {
name = schemaIDs[i].ResolveReference(&url.URL{Fragment: name[1:]}).String()
}
if typ.Reference == "" {
types[name] = typ
}
}
}
structs = make(map[string]Struct)
aliases = make(map[string]Field)
errs := []error{}
for _, typeKey := range getOrderedKeyNamesFromSchemaMap(types) {
v := types[typeKey]
if (v.TypeValue == "object" || v.TypeValue == nil) && len(v.Properties) > 0 {
s, errtype := createStruct(typeKey, v, types)
if errtype != nil {
errs = append(errs, errtype...)
}
if _, ok := structs[s.Name]; ok {
errs = append(errs, errors.New("Duplicate struct name : "+s.Name))
}
structs[s.Name] = s
} else {
a, errtype := createAlias(typeKey, v, types)
if errtype != nil {
errs = append(errs, errtype...)
}
aliases[a.Name] = a
}
}
if len(errs) > 0 {
err = errors.New(joinErrors(errs))
}
return
}
// createStruct creates a struct type from the JSON schema.
func createStruct(typeKey string, schema *jsonschema.Schema, types map[string]*jsonschema.Schema) (s Struct, errs []error) {
typeKeyURI, err := url.Parse(typeKey)
if err != nil {
errs = append(errs, err)
}
fields, err := getFields(typeKeyURI, schema.Properties, types, schema.Required)
if err != nil {
errs = append(errs, err)
}
structName := getTypeName(typeKeyURI, schema, 1)
if err != nil {
errs = append(errs, err)
}
s = Struct{
ID: typeKey,
Name: structName,
Description: schema.Description,
Fields: fields,
}
return
}
// createAlias creates a simple alias type from the JSON schema.
func createAlias(typeKey string, schema *jsonschema.Schema, types map[string]*jsonschema.Schema) (a Field, errs []error) {
typeKeyURI, err := url.Parse(typeKey)
if err != nil {
errs = append(errs, err)
}
aliasName := getTypeName(typeKeyURI, schema, 1)
if err != nil {
errs = append(errs, err)
}
tn, err := getTypeForField(typeKeyURI, typeKey, aliasName, schema, types, true)
if err != nil {
errs = append(errs, err)
}
a = Field{
Name: aliasName,
JSONName: "",
Type: tn,
Required: false,
}
return
}
func joinErrors(errs []error) string {
var buffer bytes.Buffer
for idx, err := range errs {
buffer.WriteString(err.Error())
if idx+1 < len(errs) {
buffer.WriteString(", ")
}
}
return buffer.String()
}
func getOrderedKeyNamesFromSchemaMap(m map[string]*jsonschema.Schema) []string {
keys := make([]string, len(m))
idx := 0
for k := range m {
keys[idx] = k
idx++
}
sort.Strings(keys)
return keys
}
func getFields(parentTypeKey *url.URL, properties map[string]*jsonschema.Schema,
types map[string]*jsonschema.Schema, requiredFields []string) (field map[string]Field, err error) {
fields := map[string]Field{}
missingTypes := []string{}
errors := []error{}
for _, fieldName := range getOrderedKeyNamesFromSchemaMap(properties) {
v := properties[fieldName]
golangName := getGolangName(fieldName)
tn, err := getTypeForField(parentTypeKey, fieldName, golangName, v, types, true)
if err != nil {
missingTypes = append(missingTypes, golangName)
errors = append(errors, err)
}
f := Field{
Name: golangName,
JSONName: fieldName,
// Look up the types, try references first, then drop to the built-in types.
Type: tn,
Required: contains(requiredFields, fieldName),
}
fields[f.Name] = f
}
if len(missingTypes) > 0 {
return fields, fmt.Errorf("missing types for %s with errors %s",
strings.Join(missingTypes, ","), joinErrors(errors))
}
return fields, nil
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func getTypeForField(parentTypeKey *url.URL, fieldName string, fieldGoName string,
fieldSchema *jsonschema.Schema, types map[string]*jsonschema.Schema, pointer bool) (typeName string, err error) {
// If there's no schema, or the field can be more than one type, we have to use interface{} and allow the caller to use type assertions to determine
// the actual underlying type.
if fieldSchema == nil {
return "interface{}", nil
}
majorType, multiple := fieldSchema.Type()
if multiple {
return "interface{}", nil
}
var subType string
// Look up by named reference.
if fieldSchema.Reference != "" {
// Resolve reference URI relative to schema's ID (URI).
ref, err := url.Parse(fieldSchema.Reference)
if err != nil {
return "", err
}
ref = parentTypeKey.ResolveReference(ref)
if t, ok := types[ref.String()]; ok {
sn := getTypeName(ref, t, 1)
majorType = "object"
subType = sn
} else {
return "", fmt.Errorf("failed to resolve the reference %s", ref)
}
}
// Look up any embedded types.
if subType == "" && (majorType == "object" || majorType == "") {
if len(fieldSchema.Properties) == 0 && len(fieldSchema.AdditionalProperties) > 0 {
if len(fieldSchema.AdditionalProperties) == 1 {
sn, _ := getTypeForField(parentTypeKey, fieldName, fieldGoName,
fieldSchema.AdditionalProperties[0], types, pointer)
subType = "map[string]" + sn
pointer = false
} else {
subType = "map[string]interface{}"
pointer = false
}
} else {
ref := joinURLFragmentPath(parentTypeKey, "properties/"+fieldName)
// Root schema without properties, try array item instead
if _, ok := types[ref.String()]; !ok && isRootSchemaKey(parentTypeKey) {
ref = joinURLFragmentPath(parentTypeKey, "arrayitems")
}
if parentType, ok := types[ref.String()]; ok {
sn := getTypeName(ref, parentType, 1)
subType = sn
} else {
subType = "undefined"
}
}
}
// Find named array references.
if majorType == "array" {
s, _ := getTypeForField(parentTypeKey, fieldName, fieldGoName, fieldSchema.Items, types, true)
subType = s
}
name, err := getPrimitiveTypeName(majorType, subType, pointer)
if err != nil {
return name, fmt.Errorf("failed to get the type for %s with error %s",
fieldGoName,
err.Error())
}
return name, nil
}
// isRootSchemaKey returns whether a given type key references the root schema.
func isRootSchemaKey(typeKey *url.URL) bool {
return typeKey.Fragment == ""
}
// joinURLFragmentPath joins elem onto u.Fragment, adding a separating slash.
func joinURLFragmentPath(base *url.URL, elem string) *url.URL {
url := *base
if url.Fragment == "" {
url.Fragment = "/"
}
url.Fragment = path.Join(url.Fragment, elem)
return &url
}
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 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)
}
// getTypeName makes a golang type name from an input reference in the form of #/definitions/address
// The parts refers to the number of segments from the end to take as the name.
func getTypeName(reference *url.URL, structType *jsonschema.Schema, n int) string {
if len(structType.Title) > 0 {
return getGolangName(structType.Title)
}
if isRootSchemaKey(reference) {
rootName := structType.Title
if rootName == "" {
rootName = structType.Description
}
if rootName == "" {
rootName = "Root"
}
return getGolangName(rootName)
}
parts := strings.Split(reference.Fragment, "/")
partsToUse := parts[len(parts)-n:]
sb := bytes.Buffer{}
for _, p := range partsToUse {
sb.WriteString(getGolangName(p))
}
result := sb.String()
if result == "" {
return "Root"
}
if structType.NameCount > 1 {
result = fmt.Sprintf("%v%v", result, structType.NameCount)
}
return result
}
// getGolangName strips invalid characters out of golang struct or field names.
func getGolangName(s string) string {
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
}
// 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
// Required is set to true when the field is required.
Required bool
}