forked from omissis/go-jsonschema-fork
/
model.go
353 lines (288 loc) · 6.07 KB
/
model.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
package codegen
import (
"sort"
"strings"
"github.com/02strich/go-jsonschema/pkg/schemas"
"github.com/sanity-io/litter"
)
type Decl interface {
Generate(out *Emitter)
}
type Named interface {
Decl
GetName() string
}
type File struct {
FileName string
Package Package
}
func (p *File) Generate(out *Emitter) {
out.Comment("Code generated by github.com/02strich/go-jsonschema, DO NOT EDIT.")
out.Newline()
p.Package.Generate(out)
}
// Package is a "package <name>; <body>".
type Package struct {
QualifiedName string
Comment string
Decls []Decl
Imports []Import
}
func (p *Package) AddDecl(t Decl) {
p.Decls = append(p.Decls, t)
}
func (p *Package) AddImport(qualifiedName, alias string) {
if !p.hasImport(qualifiedName) {
p.Imports = append(p.Imports, Import{
QualifiedName: qualifiedName,
Name: alias,
})
}
}
func (p *Package) hasImport(q string) bool {
for _, i := range p.Imports {
if i.QualifiedName == q {
return true
}
}
return false
}
func (p *Package) Name() string {
s := p.QualifiedName
if i := strings.LastIndex(s, "/"); i != -1 && i < len(s)-1 {
return s[i+1:]
}
return s
}
func (p *Package) Generate(out *Emitter) {
out.Comment(p.Comment)
out.Println("package %s", p.Name())
if len(p.Imports) > 0 {
for _, i := range p.Imports {
i.Generate(out)
}
}
out.Newline()
sorted := make([]Decl, len(p.Decls))
copy(sorted, p.Decls)
sort.Slice(sorted, func(i, j int) bool {
if a, ok := sorted[i].(Named); ok {
if b, ok := sorted[j].(Named); ok {
return a.GetName() < b.GetName()
}
}
return false
})
for i, t := range sorted {
if i > 0 {
out.Newline()
}
t.Generate(out)
}
}
// Var is a "var <name> = <value>".
type Var struct {
Type Type
Name string
Value interface{}
}
func (v *Var) GetName() string {
return v.Name
}
func (v *Var) Generate(out *Emitter) {
out.Print("var %s ", v.Name)
if v.Type != nil {
v.Type.Generate(out)
}
out.Print(" = %s", litter.Sdump(v.Value))
}
// Constant is a "const <name> = <value>".
type Constant struct {
Type Type
Name string
Value interface{}
}
func (c *Constant) GetName() string {
return c.Name
}
func (c *Constant) Generate(out *Emitter) {
out.Print("const %s ", c.Name)
if c.Type != nil {
c.Type.Generate(out)
}
out.Print(" = %s", litter.Sdump(c.Value))
}
// Fragment is an arbitary piece of code.
type Fragment func(*Emitter)
func (f Fragment) Generate(out *Emitter) {
f(out)
}
// Method defines a method and how to generate it.
type Method struct {
Impl func(*Emitter)
}
func (m *Method) Generate(out *Emitter) {
out.Newline()
m.Impl(out)
out.Newline()
}
// Import is a "type <name> = <definition>".
type Import struct {
Name string
QualifiedName string
}
func (i *Import) Generate(out *Emitter) {
if i.Name != "" {
out.Println("import %s %q", i.Name, i.QualifiedName)
} else {
out.Println("import %q", i.QualifiedName)
}
}
// TypeDecl is a "type <name> = <definition>".
type TypeDecl struct {
Name string
Type Type
Comment string
}
func (td *TypeDecl) GetName() string {
return td.Name
}
func (td *TypeDecl) Generate(out *Emitter) {
out.Comment(td.Comment)
out.Print("type %s ", td.Name)
td.Type.Generate(out)
out.Newline()
}
type Type interface {
Decl
IsNillable() bool
}
type PointerType struct {
Type Type
}
func (PointerType) IsNillable() bool { return true }
func (p PointerType) Generate(out *Emitter) {
out.Print("*")
p.Type.Generate(out)
}
type ArrayType struct {
Type Type
}
func (ArrayType) IsNillable() bool { return true }
func (a ArrayType) Generate(out *Emitter) {
out.Print("[]")
a.Type.Generate(out)
}
type NamedType struct {
Package *Package
Decl *TypeDecl
}
func (t NamedType) GetName() string {
return t.Decl.Name
}
func (t NamedType) IsNillable() bool {
return t.Decl.Type != nil && t.Decl.Type.IsNillable()
}
func (t NamedType) Generate(out *Emitter) {
if t.Package != nil {
out.Print(t.Package.Name())
out.Print(".")
}
out.Print(t.Decl.Name)
}
type PrimitiveType struct {
Type string
}
func (PrimitiveType) IsNillable() bool { return false }
func (p PrimitiveType) Generate(out *Emitter) {
out.Print(p.Type)
}
type CustomNameType struct {
Type string
}
func (CustomNameType) IsNillable() bool { return false }
func (p CustomNameType) Generate(out *Emitter) {
out.Print(p.Type)
}
type MapType struct {
KeyType, ValueType Type
}
func (MapType) IsNillable() bool { return true }
func (p MapType) Generate(out *Emitter) {
out.Print("map[")
p.KeyType.Generate(out)
out.Print("]")
p.ValueType.Generate(out)
}
type EmptyInterfaceType struct{}
func (EmptyInterfaceType) IsNillable() bool { return true }
func (EmptyInterfaceType) Generate(out *Emitter) {
out.Print("interface{}")
}
type NullType struct{}
func (NullType) IsNillable() bool { return true }
func (NullType) Generate(out *Emitter) {
out.Print("interface{}")
}
type StructType struct {
Parents []Type
Fields []StructField
RequiredJSONFields []string
Variants []Type
}
func (StructType) IsNillable() bool { return false }
func (s *StructType) AddParent(f Type) {
s.Parents = append(s.Parents, f)
}
func (s *StructType) AddField(f StructField) {
s.Fields = append(s.Fields, f)
}
func (s *StructType) AddVariant(f Type) {
s.Variants = append(s.Variants, f)
}
func (s *StructType) Generate(out *Emitter) {
out.Println("struct {")
out.Indent(1)
i := 0
for _, p := range s.Parents {
if i > 0 {
out.Newline()
}
p.Generate(out)
out.Newline()
i++
}
for _, f := range s.Fields {
if i > 0 {
out.Newline()
}
f.Generate(out)
out.Newline()
i++
}
if len(s.Variants) > 0 {
out.Println("union json.RawMessage")
}
out.Indent(-1)
out.Print("}")
}
type StructField struct {
Name string
Type Type
Comment string
Tags string
JSONName string
DefaultValue interface{}
SchemaType *schemas.Type
}
func (f *StructField) GetName() string {
return f.Name
}
func (f *StructField) Generate(out *Emitter) {
out.Comment(f.Comment)
out.Print("%s ", f.Name)
f.Type.Generate(out)
if f.Tags != "" {
out.Print(" `%s`", f.Tags)
}
}