This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jsonschema.go
315 lines (293 loc) · 10.5 KB
/
jsonschema.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
package model
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"github.com/taskcluster/taskcluster-client-java/codegenerator/utils"
)
type (
// Note that all members are backed by pointers, so that nil value can signify non-existence.
// Otherwise we could not differentiate whether a zero value is non-existence or actually the
// zero value. For example, if a bool is false, we don't know if it was explictly set to false
// in the json we read, or whether it was not given. Unmarshaling into a pointer means pointer
// will be nil pointer if it wasn't read, or a pointer to true/false if it was read from json.
JsonSubSchema struct {
AdditionalItems *bool `json:"additionalItems"`
AdditionalProperties *AdditionalProperties `json:"additionalProperties"`
AllOf Items `json:"allOf"`
AnyOf Items `json:"anyOf"`
Default *interface{} `json:"default"`
Description *string `json:"description"`
Enum interface{} `json:"enum"`
Format *string `json:"format"`
ID *string `json:"id"`
Items *JsonSubSchema `json:"items"`
Maximum *int `json:"maximum"`
MaxLength *int `json:"maxLength"`
Minimum *int `json:"minimum"`
MinLength *int `json:"minLength"`
OneOf Items `json:"oneOf"`
Pattern *string `json:"pattern"`
Properties *Properties `json:"properties"`
Ref *string `json:"$ref"`
Required []string `json:"required"`
Schema *string `json:"$schema"`
Title *string `json:"title"`
Type *string `json:"type"`
// non-json fields used for sorting/tracking
TypeName string
IsInputSchema bool
IsOutputSchema bool
SourceURL string
RefSubSchema *JsonSubSchema
APIDefinition *APIDefinition
}
Items []JsonSubSchema
Properties struct {
Properties map[string]*JsonSubSchema
SortedPropertyNames []string
}
AdditionalProperties struct {
Boolean *bool
Properties *JsonSubSchema
}
)
func (subSchema JsonSubSchema) String() string {
result := ""
result += describe("Additional Items", subSchema.AdditionalItems)
result += describe("Additional Properties", subSchema.AdditionalProperties)
result += describe("All Of", subSchema.AllOf)
result += describe("Any Of", subSchema.AnyOf)
result += describe("Default", subSchema.Default)
result += describe("Description", subSchema.Description)
result += describe("Enum", subSchema.Enum)
result += describe("Format", subSchema.Format)
result += describe("ID", subSchema.ID)
result += describeList("Items", subSchema.Items)
result += describe("Maximum", subSchema.Maximum)
result += describe("MaxLength", subSchema.MaxLength)
result += describe("Minimum", subSchema.Minimum)
result += describe("MinLength", subSchema.MinLength)
result += describeList("OneOf", subSchema.OneOf)
result += describe("Pattern", subSchema.Pattern)
result += describeList("Properties", subSchema.Properties)
result += describe("Ref", subSchema.Ref)
result += describe("Required", subSchema.Required)
result += describe("Schema", subSchema.Schema)
result += describe("Title", subSchema.Title)
result += describe("Type", subSchema.Type)
result += describe("TypeName", &subSchema.TypeName)
result += describe("IsInputSchema", &subSchema.IsInputSchema)
result += describe("IsOutputSchema", &subSchema.IsOutputSchema)
result += describe("SourceURL", &subSchema.SourceURL)
return result
}
func (jsonSubSchema *JsonSubSchema) TypeDefinition(level int, fromArray bool, extraPackages map[string]bool) (string, map[string]bool, string) {
content := ""
if level == 0 && !fromArray {
content += "/**\n"
if d := jsonSubSchema.Description; d != nil {
if desc := *d; desc != "" {
content += utils.Indent(desc, "* ")
}
if content[len(content)-1:] != "\n" {
content += "\n"
}
}
if url := jsonSubSchema.SourceURL; url != "" {
content += "*\n* See " + url + "\n"
}
content += "*/\n"
}
typ := "Object"
if p := jsonSubSchema.Type; p != nil {
typ = *p
}
if p := jsonSubSchema.RefSubSchema; p != nil {
_, _, possSimpleType := p.TypeDefinition(1, true, make(map[string]bool))
switch possSimpleType {
case "":
typ = p.TypeName
default:
typ = possSimpleType
}
}
switch typ {
case "array":
if jsonType := jsonSubSchema.Items.Type; jsonType != nil {
var newType string
newType, extraPackages, typ = jsonSubSchema.Items.TypeDefinition(level, true, extraPackages)
if level == 0 {
if typ == "" {
content += newType
}
} else {
typ = newType + "[]"
}
} else {
if refSubSchema := jsonSubSchema.Items.RefSubSchema; refSubSchema != nil {
typ = refSubSchema.TypeName
}
}
case "object":
if s := jsonSubSchema.Properties; s != nil {
typ = "" // strings.Title(jsonSubSchema.TypeName)
def := fmt.Sprintf("class " + strings.Title(jsonSubSchema.TypeName) + " {\n")
for _, j := range s.SortedPropertyNames {
// recursive call to build go types inside structs
var subType string
subType, extraPackages, _ = s.Properties[j].TypeDefinition(level+1, false, extraPackages)
// comment the struct member with the description from the json
if d := s.Properties[j].Description; d != nil {
def += "\n" + utils.Comment(*d, strings.Repeat(" ", level+1))
}
// struct member name and type, as part of struct definition
def += fmt.Sprintf(strings.Repeat(" ", level+1)+"public %v %v;\n", subType, s.Properties[j].TypeName)
}
def += strings.Repeat(" ", level) + "}"
if level == 0 {
def = "public " + def
} else {
def += "\n\n" + strings.Repeat(" ", level) + "public " + strings.Title(jsonSubSchema.TypeName)
}
content += def
} else {
typ = "Object"
}
case "number":
typ = "int"
case "integer":
typ = "int"
case "boolean":
typ = "boolean"
// json type string maps to go type string, so only need to test case of when
// string is a json date-time, so we can convert to go type time.Time...
case "string":
if f := jsonSubSchema.Format; f != nil && *f == "date-time" {
typ = "Date"
} else {
typ = "String"
}
}
switch typ {
case "Date":
extraPackages["java.util.Date"] = true
}
content += typ
return content, extraPackages, typ
}
func (p Properties) String() string {
result := ""
for _, i := range p.SortedPropertyNames {
result += "Property '" + i + "' =\n" + utils.Indent(p.Properties[i].String(), " ")
}
return result
}
func (p *Properties) postPopulate(apiDef *APIDefinition) {
// now all data should be loaded, let's sort the p.Properties
if p.Properties != nil {
p.SortedPropertyNames = make([]string, 0, len(p.Properties))
for propertyName := range p.Properties {
p.SortedPropertyNames = append(p.SortedPropertyNames, propertyName)
}
sort.Strings(p.SortedPropertyNames)
members := make(map[string]bool, len(p.SortedPropertyNames))
for _, j := range p.SortedPropertyNames {
p.Properties[j].TypeName = utils.NormaliseLower(j, members)
// subschemas also need to be triggered to postPopulate...
p.Properties[j].postPopulate(apiDef)
}
}
}
func (p *Properties) UnmarshalJSON(bytes []byte) (err error) {
errX := json.Unmarshal(bytes, &p.Properties)
return errX
}
func (aP *AdditionalProperties) UnmarshalJSON(bytes []byte) (err error) {
b, p := new(bool), new(JsonSubSchema)
if err = json.Unmarshal(bytes, b); err == nil {
aP.Boolean = b
return
}
if err = json.Unmarshal(bytes, p); err == nil {
aP.Properties = p
}
return
}
func (aP AdditionalProperties) String() string {
if aP.Boolean != nil {
return strconv.FormatBool(*aP.Boolean)
}
return aP.Properties.String()
}
func (items Items) String() string {
result := ""
for i, j := range items {
result += fmt.Sprintf("Item '%v' =\n", i) + utils.Indent(j.String(), " ")
}
return result
}
func (items Items) postPopulate(apiDef *APIDefinition) {
for i := range items {
items[i].postPopulate(apiDef)
}
}
func describeList(name string, value interface{}) string {
if reflect.ValueOf(value).IsValid() {
if !reflect.ValueOf(value).IsNil() {
return fmt.Sprintf("%v\n", name) + utils.Indent(fmt.Sprintf("%v", reflect.Indirect(reflect.ValueOf(value)).Interface()), " ")
}
}
return ""
}
// If item is not null, then return a description of it. If it is a pointer, dereference it first.
func describe(name string, value interface{}) string {
if reflect.ValueOf(value).IsValid() {
if !reflect.ValueOf(value).IsNil() {
return fmt.Sprintf("%-22v = '%v'\n", name, reflect.Indirect(reflect.ValueOf(value)).Interface())
}
}
return ""
}
type CanPopulate interface {
postPopulate(*APIDefinition)
}
func postPopulateIfNotNil(canPopulate CanPopulate, apiDef *APIDefinition) {
if reflect.ValueOf(canPopulate).IsValid() {
if !reflect.ValueOf(canPopulate).IsNil() {
canPopulate.postPopulate(apiDef)
}
}
}
func (subSchema *JsonSubSchema) postPopulate(apiDef *APIDefinition) {
if subSchema.TypeName == "" {
members := make(map[string]bool, 1)
switch {
case subSchema.Title != nil && *subSchema.Title != "" && len(*subSchema.Title) < 40:
subSchema.TypeName = utils.NormaliseLower(*subSchema.Title, members)
case subSchema.Description != nil && *subSchema.Description != "" && len(*subSchema.Description) < 40:
subSchema.TypeName = utils.NormaliseLower(*subSchema.Description, members)
case subSchema.RefSubSchema != nil && subSchema.RefSubSchema.TypeName != "":
subSchema.TypeName = subSchema.RefSubSchema.TypeName
default:
subSchema.TypeName = "X"
}
}
// Arrays should get their name from their parent subschema. Note we set
// this before calling postPopulate on subSchema.Items, to make sure we get
// there first! If already set, it won't get updated later.
if subSchema.Items != nil {
subSchema.Items.TypeName = subSchema.TypeName
}
postPopulateIfNotNil(subSchema.AllOf, apiDef)
postPopulateIfNotNil(subSchema.AnyOf, apiDef)
postPopulateIfNotNil(subSchema.OneOf, apiDef)
postPopulateIfNotNil(subSchema.Items, apiDef)
postPopulateIfNotNil(subSchema.Properties, apiDef)
// If we have a $ref pointing to another schema, keep a reference so we can
// discover TypeName later when we generate the type definition
subSchema.RefSubSchema = apiDef.cacheJsonSchema(subSchema.Ref)
}