-
Notifications
You must be signed in to change notification settings - Fork 30
/
jsonschema.go
198 lines (167 loc) · 4.36 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
/*
Basic json-schema generator based on Go types, for easy interchange of Go
structures between diferent languages.
*/
package jsonschema
import (
"encoding/json"
"reflect"
"strings"
)
const DEFAULT_SCHEMA = "http://json-schema.org/schema#"
type Document struct {
Schema string `json:"$schema,omitempty"`
property
}
// Reads the variable structure into the JSON-Schema Document
func (d *Document) Read(variable interface{}) {
d.setDefaultSchema()
value := reflect.ValueOf(variable)
d.read(value.Type(), tagOptions(""))
}
func (d *Document) setDefaultSchema() {
if d.Schema == "" {
d.Schema = DEFAULT_SCHEMA
}
}
// Marshal returns the JSON encoding of the Document
func (d *Document) Marshal() ([]byte, error) {
return json.MarshalIndent(d, "", " ")
}
// String return the JSON encoding of the Document as a string
func (d *Document) String() string {
json, _ := d.Marshal()
return string(json)
}
type property struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Items *property `json:"items,omitempty"`
Properties map[string]*property `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
AdditionalProperties bool `json:"additionalProperties,omitempty"`
}
func (p *property) read(t reflect.Type, opts tagOptions) {
jsType, format, kind := getTypeFromMapping(t)
if jsType != "" {
p.Type = jsType
}
if format != "" {
p.Format = format
}
switch kind {
case reflect.Slice:
p.readFromSlice(t)
case reflect.Map:
p.readFromMap(t)
case reflect.Struct:
p.readFromStruct(t)
case reflect.Ptr:
p.read(t.Elem(), opts)
}
}
func (p *property) readFromSlice(t reflect.Type) {
jsType, _, kind := getTypeFromMapping(t.Elem())
if kind == reflect.Uint8 {
p.Type = "string"
} else if jsType != "" {
p.Items = &property{}
p.Items.read(t.Elem(), tagOptions(""))
}
}
func (p *property) readFromMap(t reflect.Type) {
jsType, format, _ := getTypeFromMapping(t.Elem())
if jsType != "" {
p.Properties = make(map[string]*property, 0)
p.Properties[".*"] = &property{Type: jsType, Format: format}
} else {
p.AdditionalProperties = true
}
}
func (p *property) readFromStruct(t reflect.Type) {
p.Type = "object"
p.Properties = make(map[string]*property, 0)
p.AdditionalProperties = false
count := t.NumField()
for i := 0; i < count; i++ {
field := t.Field(i)
tag := field.Tag.Get("json")
name, opts := parseTag(tag)
if name == "" {
name = field.Name
}
if name == "-" {
continue
}
if field.Anonymous {
embeddedProperty := &property{}
embeddedProperty.read(field.Type, opts)
for name, property := range embeddedProperty.Properties {
p.Properties[name] = property
}
p.Required = append(p.Required, embeddedProperty.Required...)
continue
}
p.Properties[name] = &property{}
p.Properties[name].read(field.Type, opts)
if !opts.Contains("omitempty") {
p.Required = append(p.Required, name)
}
}
}
var formatMapping = map[string][]string{
"time.Time": []string{"string", "date-time"},
}
var kindMapping = map[reflect.Kind]string{
reflect.Bool: "boolean",
reflect.Int: "integer",
reflect.Int8: "integer",
reflect.Int16: "integer",
reflect.Int32: "integer",
reflect.Int64: "integer",
reflect.Uint: "integer",
reflect.Uint8: "integer",
reflect.Uint16: "integer",
reflect.Uint32: "integer",
reflect.Uint64: "integer",
reflect.Float32: "number",
reflect.Float64: "number",
reflect.String: "string",
reflect.Slice: "array",
reflect.Struct: "object",
reflect.Map: "object",
}
func getTypeFromMapping(t reflect.Type) (string, string, reflect.Kind) {
if v, ok := formatMapping[t.String()]; ok {
return v[0], v[1], reflect.String
}
if v, ok := kindMapping[t.Kind()]; ok {
return v, "", t.Kind()
}
return "", "", t.Kind()
}
type tagOptions string
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, ",")
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}