-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
254 lines (227 loc) · 6.01 KB
/
converter.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
package yajsonschema
import (
"errors"
"io"
"sort"
"strings"
// go-yaml doesn't have custom tag support yet... grabbing random fork.
// see: https://github.com/go-yaml/yaml/issues/191
"github.com/wryun/yaml"
)
const (
optionalSigil = "?"
objectType = "object"
arrayType = "array"
)
// Convert processes either 1 or 2 yajsonschema documents accessible via io.Reader,
// and outputs a json schema.
func Convert(yamlSchemaReader io.Reader) (map[string]interface{}, error) {
yamlDefinitions, yamlSchema, err := unmarshal(yamlSchemaReader)
if err != nil {
return nil, err
}
jsonSchema, err := buildFragment(yamlSchema)
if err != nil {
return nil, err
}
if yamlDefinitions != nil {
typedYamlDefinitions, ok := yamlDefinitions.(map[interface{}]interface{})
if !ok {
return nil, errors.New("first document - definitions - is not an object")
}
jsonSchemaDefinitions := make(map[string]interface{}, len(typedYamlDefinitions))
for untypedKey, value := range typedYamlDefinitions {
key, ok := untypedKey.(string)
if !ok {
return nil, errors.New("first document - definitions - has non-string keys")
}
jsonSchemaDefinitions[key], err = buildFragment(value)
if err != nil {
return nil, err
}
}
jsonSchema["definitions"] = jsonSchemaDefinitions
}
jsonSchema["$schema"] = "http://json-schema.org/draft-04/schema#"
return jsonSchema, nil
}
func unmarshal(yamlSchema io.Reader) (interface{}, interface{}, error) {
dec := yaml.NewDecoder(yamlSchema)
dec.RegisterCustomTagUnmarshaller("!enum", &customTagUnmarshaler{
func(unmarshalYaml func(interface{}) error) (interface{}, error) {
enum := enumTag{}
if err := unmarshalYaml(&enum.Contents); err != nil {
return nil, err
}
return enum, nil
},
})
dec.RegisterCustomTagUnmarshaller("!ref", &customTagUnmarshaler{
func(unmarshalYaml func(interface{}) error) (interface{}, error) {
ref := refTag{}
if err := unmarshalYaml(&ref.Contents); err != nil {
return nil, err
}
return ref, nil
},
})
dec.RegisterCustomTagUnmarshaller("!type", &customTagUnmarshaler{
func(unmarshalYaml func(interface{}) error) (interface{}, error) {
t := typeTag{}
if err := unmarshalYaml(&t.Contents); err != nil {
return nil, err
}
return t, nil
},
})
documents := []interface{}{}
for {
var document interface{}
err := dec.Decode(&document)
if err == io.EOF {
break
}
if err != nil {
return nil, nil, err
}
documents = append(documents, document)
}
var definitions, schema interface{}
switch len(documents) {
case 0:
return nil, nil, errors.New("no yaml documents in schema input")
default:
return nil, nil, errors.New("more than two yaml documents in schema input")
case 1:
schema = documents[0]
case 2:
definitions = documents[0]
schema = documents[1]
}
return definitions, schema, nil
}
type customTagUnmarshaler struct {
doit func(func(interface{}) error) (interface{}, error)
}
func (ctu *customTagUnmarshaler) UnmarshalYAML(yamlUnmarshal func(interface{}) error) (interface{}, error) {
return ctu.doit(yamlUnmarshal)
}
type enumTag struct {
Contents []interface{}
}
type typeTag struct {
Contents interface{}
}
type refTag struct {
Contents string
}
func buildFragment(yamlSchema interface{}) (map[string]interface{}, error) {
switch val := yamlSchema.(type) {
default:
return map[string]interface{}(map[string]interface{}{
"enum": []interface{}{val}, // draft 04 doesn't support const
}), nil
case []interface{}:
return buildArraySchema(val)
case map[interface{}]interface{}:
return buildObjectSchema(val)
case enumTag:
return map[string]interface{}(map[string]interface{}{
"enum": val.Contents,
}), nil
case typeTag:
switch typeContents := val.Contents.(type) {
case map[interface{}]interface{}:
result := make(map[string]interface{}, len(typeContents))
for k, v := range typeContents {
result[k.(string)] = v
}
return result, nil
case string:
return map[string]interface{}{
"type": typeContents,
}, nil
default:
return nil, nil
}
case refTag:
return map[string]interface{}{
"$ref": "#/definitions/" + val.Contents,
}, nil
}
}
func buildArraySchema(arr []interface{}) (map[string]interface{}, error) {
var err error
schema := map[string]interface{}(map[string]interface{}{
"type": arrayType,
})
switch len(arr) {
case 0:
break
case 1:
schema["items"], err = buildFragment(arr[0])
if err != nil {
return nil, err
}
default:
items := map[string][]interface{}{
"anyOf": []interface{}{},
}
schema["items"] = items
for _, yamlSchema := range arr {
anyOfSchema, err := buildFragment(yamlSchema)
if err != nil {
return nil, err
}
items["anyOf"] = append(items["anyOf"], anyOfSchema)
}
}
return schema, nil
}
func buildObjectSchema(obj map[interface{}]interface{}) (map[string]interface{}, error) {
var err error
var properties map[string]interface{}
var required []string
schema := map[string]interface{}(map[string]interface{}{
"type": objectType,
})
for untypedField, yamlSchema := range obj {
field, ok := untypedField.(string)
if !ok {
return nil, errors.New("object has non-string key")
}
if field != "-" {
if strings.HasSuffix(field, optionalSigil) {
field = strings.TrimSuffix(field, optionalSigil)
} else {
required = append(required, field)
}
if properties == nil {
properties = map[string]interface{}{}
schema["properties"] = properties
}
properties[field], err = buildFragment(yamlSchema)
if err != nil {
return nil, err
}
} else {
switch yamlSchema.(type) {
case bool:
// i.e. we can't actually handle the case when the value of the additional
// property must be a boolean constant. Oh well.
schema["additionalProperties"] = yamlSchema
default:
schema["additionalProperties"], err = buildFragment(yamlSchema)
if err != nil {
return nil, err
}
}
}
}
if required != nil {
// So it's stable. Mostly for testing.
sort.Strings(required)
schema["required"] = required
}
return schema, nil
}