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
166 lines (157 loc) · 4.71 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
package model
import (
"fmt"
"log"
"net/url"
"strconv"
"strings"
"github.com/taskcluster/jsonschema2go"
"github.com/taskcluster/taskcluster-client-java/codegenerator/utils"
)
type JsonSubSchema jsonschema2go.JsonSubSchema
func (jsonSubSchema *JsonSubSchema) TypeDefinition(level int, extraPackages map[string]bool) (classDefinition, comment, typ string) {
if d := jsonSubSchema.Description; d != nil {
if desc := *d; desc != "" {
comment += desc
}
}
if comment != "" && comment[len(comment)-1:] != "\n" {
comment += "\n"
}
// Create comments for metadata in a single paragraph. Only start new
// paragraph if we discover after inspecting all possible metadata, that
// something has been specified. If there is no metadata, no need to create
// a new paragraph.
var metadata string
if enum := jsonSubSchema.Enum; enum != nil {
metadata += "Possible values:\n"
for _, i := range enum {
switch i.(type) {
case float64:
metadata += fmt.Sprintf(" * %v\n", i)
default:
metadata += fmt.Sprintf(" * %q\n", i)
}
}
}
if def := jsonSubSchema.Default; def != nil {
var value string
switch (*def).(type) {
case bool:
value = strconv.FormatBool((*def).(bool))
case float64:
value = strconv.FormatFloat((*def).(float64), 'g', -1, 64)
default:
value = fmt.Sprintf("%q", *def)
}
metadata += "Default: " + value + "\n"
}
if regex := jsonSubSchema.Pattern; regex != nil {
metadata += "Syntax: " + *regex + "\n"
}
if minItems := jsonSubSchema.MinLength; minItems != nil {
metadata += "Min length: " + strconv.Itoa(*minItems) + "\n"
}
if maxItems := jsonSubSchema.MaxLength; maxItems != nil {
metadata += "Max length: " + strconv.Itoa(*maxItems) + "\n"
}
if minimum := jsonSubSchema.Minimum; minimum != nil {
metadata += "Mininum: " + strconv.Itoa(*minimum) + "\n"
}
if maximum := jsonSubSchema.Maximum; maximum != nil {
metadata += "Maximum: " + strconv.Itoa(*maximum) + "\n"
}
// Here we check if metadata was specified, and only create new
// paragraph (`*\n`) if something was.
if len(metadata) > 0 {
comment += "\n" + metadata
}
if URL := jsonSubSchema.SourceURL; URL != "" {
u, err := url.Parse(URL)
if err == nil && u.Scheme != "file" {
if len(comment) > 1 {
comment += "\n"
}
comment += "See " + URL + "\n"
}
}
comment = utils.Comment(comment, "")
typ = "Object"
if p := jsonSubSchema.Type; p != nil {
typ = *p
}
if p := jsonSubSchema.RefSubSchema; p != nil {
j := JsonSubSchema(*jsonSubSchema.RefSubSchema)
_, _, possSimpleType := (&j).TypeDefinition(0, make(map[string]bool))
switch possSimpleType {
case "":
typ = p.TypeName
default:
typ = possSimpleType
}
}
switch typ {
case "array":
if jsonSubSchema.Items != nil {
if jsonSubSchema.Items.Type != nil {
j := JsonSubSchema(*jsonSubSchema.Items)
subClassDefinition, _, subType := (&j).TypeDefinition(level, extraPackages)
if subType == "" {
log.Fatalf("%v returns an empty Type", jsonSubSchema.Items.SourceURL)
}
typ = subType + "[]"
classDefinition = subClassDefinition
} else {
if refSubSchema := jsonSubSchema.Items.RefSubSchema; refSubSchema != nil {
if refSubSchema.TypeName == "" {
log.Fatalf("%v has not set TypeName", refSubSchema.SourceURL)
}
typ = refSubSchema.TypeName + "[]"
}
}
} else {
typ = "Object[]"
}
case "object":
if s := jsonSubSchema.Properties; s != nil {
typ = jsonSubSchema.TypeName
classDefinition = "\n" + strings.Repeat(" ", level) + "public class " + typ + " {"
for _, j := range s.SortedPropertyNames {
// recursive call to build go types inside structs
var subType string
c := JsonSubSchema(*s.Properties[j])
subMember := s.MemberNames[j]
subClassDefinition, subComment, subType := (&c).TypeDefinition(level+1, extraPackages)
classDefinition += "\n"
classDefinition += subClassDefinition
classDefinition += "\n"
classDefinition += utils.Indent(subComment, strings.Repeat(" ", level+1), false)
classDefinition += strings.Repeat(" ", level+1) + "public " + subType + " " + subMember + ";"
}
classDefinition += "\n"
classDefinition += strings.Repeat(" ", level) + "}"
classDefinition += "\n"
} 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
}
return classDefinition, comment, typ
}