-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
341 lines (325 loc) · 7.97 KB
/
schema.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
package chioas
import (
"errors"
"fmt"
"github.com/go-andiamo/chioas/yaml"
"reflect"
"strings"
)
type Schemas []Schema
func (ss Schemas) writeYaml(w yaml.Writer) {
if len(ss) > 0 {
// check for duplicates...
m := map[string]struct{}{}
for _, s := range ss {
if s.Name != "" {
if _, ok := m[s.Name]; ok {
w.SetError(fmt.Errorf("duplicate schema name '%s' in components", s.Name))
return
}
m[s.Name] = struct{}{}
}
}
w.WriteTagStart(tagNameSchemas)
for _, s := range ss {
s.writeYaml(true, w)
}
w.WriteTagEnd()
}
}
// SchemaConverter is an interface that can be implemented by anything to convert it to a schema
type SchemaConverter interface {
ToSchema() *Schema
}
// SchemaWriter is an interface that can be implemented by anything to write a schema for that item
type SchemaWriter interface {
WriteSchema(w yaml.Writer)
}
// Schema represents the OAS definition of a schema
type Schema struct {
// Name is the OAS name of the schema
Name string
// Description is the OAS description
Description string
// Type is the OAS type
//
// Should be one of "string", "object", "array", "boolean", "integer", "number" or "null"
Type string
// Format is the OAS format
Format string
// RequiredProperties is the ordered collection of required properties
//
// If any of the items in Properties is also denoted as Property.Required, these are
// automatically added to RequiredProperties
RequiredProperties []string
// Properties is the ordered collection of properties
Properties Properties
// Default is the OAS default
Default any
// Example is the OAS example for the schema
Example any
// Enum is the OAS enum
Enum []any
// Extensions is extension OAS yaml properties
Extensions Extensions
// Additional is any additional OAS spec yaml to be written
Additional Additional
// Comment is any comment(s) to appear in the OAS spec yaml
Comment string
// SchemaRef is the OAS schema reference
//
// Only used if value is a non-empty string
//
// If the value does not contain a path (i.e. does not contain any "/") then the ref
// path will be the value prefixed with components schemas path. For example, specifying "foo"
// will result in a schema ref:
// schema:
// $ref: "#/components/schemas/foo"
SchemaRef string
// Discriminator is the optional OAS discriminator for the schema
Discriminator *Discriminator
// Ofs is the optional OAS ofs (oneOf, anyOf or allOf) for the schema
Ofs *Ofs
}
func (s *Schema) Ref() string {
panic("*Schema does not contain Ref() string")
}
func (s *Schema) Schema() *Schema {
return s
}
func (s *Schema) IsRef() bool {
return false
}
func (s *Schema) writeYaml(withName bool, w yaml.Writer) {
if withName {
if s.Name == "" {
w.SetError(errors.New("schema without name"))
return
}
w.WriteTagStart("\"" + s.Name + "\"")
}
w.WriteComments(s.Comment)
if s.SchemaRef != "" {
writeSchemaRef(s.SchemaRef, s.Type == tagValueTypeArray, w)
} else {
w.WriteTagValue(tagNameDescription, s.Description).
WriteTagValue(tagNameType, defValue(s.Type, tagValueTypeObject)).
WriteTagValue(tagNameFormat, s.Format)
if s.Ofs != nil {
s.Ofs.writeYaml(w)
}
if s.Discriminator != nil {
s.Discriminator.writeYaml(w)
}
if reqs, has := s.getRequiredProperties(); has {
w.WriteTagStart(tagNameRequired)
for _, rp := range reqs {
w.WriteItem(rp)
}
w.WriteTagEnd()
}
if len(s.Properties) > 0 {
w.WriteTagStart(tagNameProperties)
for _, p := range s.Properties {
p.writeYaml(w, true)
}
w.WriteTagEnd()
}
w.WriteTagValue(tagNameDefault, s.Default).
WriteTagValue(tagNameExample, s.Example)
if len(s.Enum) > 0 {
w.WriteTagStart(tagNameEnum)
for _, e := range s.Enum {
w.WriteItem(e)
}
w.WriteTagEnd()
}
writeExtensions(s.Extensions, w)
writeAdditional(s.Additional, s, w)
}
if withName {
w.WriteTagEnd()
}
}
func (s *Schema) writeOfYaml(w yaml.Writer) {
w.WriteTagValue(tagNameDescription, s.Description).
WriteItemStart(tagNameType, defValue(s.Type, tagValueTypeObject)).
WriteTagValue(tagNameFormat, s.Format)
if reqs, has := s.getRequiredProperties(); has {
w.WriteTagStart(tagNameRequired)
for _, rp := range reqs {
w.WriteItem(rp)
}
w.WriteTagEnd()
}
if len(s.Properties) > 0 {
w.WriteTagStart(tagNameProperties)
for _, p := range s.Properties {
p.writeYaml(w, true)
}
w.WriteTagEnd()
}
w.WriteTagValue(tagNameDefault, s.Default).
WriteTagValue(tagNameExample, s.Example)
if len(s.Enum) > 0 {
w.WriteTagStart(tagNameEnum)
for _, e := range s.Enum {
w.WriteItem(e)
}
w.WriteTagEnd()
}
writeExtensions(s.Extensions, w)
writeAdditional(s.Additional, s, w)
w.WriteTagEnd()
}
func (s *Schema) getRequiredProperties() ([]string, bool) {
result := make([]string, 0, len(s.RequiredProperties))
m := map[string]bool{}
for _, v := range s.RequiredProperties {
if !m[v] {
result = append(result, v)
m[v] = true
}
}
for _, pty := range s.Properties {
if pty.Required && !m[pty.Name] {
result = append(result, pty.Name)
m[pty.Name] = true
}
}
return result, len(result) > 0
}
func writeSchema(schema any, isArray bool, w yaml.Writer) {
actual, writer := isActualSchema(schema)
if actual == nil && writer == nil {
actual, isArray = extractSchema(schema, isArray)
}
if actual != nil {
if isArray {
w.WriteTagValue(tagNameType, tagValueTypeArray).
WriteTagStart(tagNameItems)
}
actual.writeYaml(false, w)
if isArray {
w.WriteTagEnd()
}
} else if writer != nil {
if isArray {
w.WriteTagValue(tagNameType, tagValueTypeArray).
WriteTagStart(tagNameItems)
}
writer.WriteSchema(w)
if isArray {
w.WriteTagEnd()
}
} else {
w.WriteTagValue(tagNameType, tagValueTypeNull)
}
}
func isActualSchema(schema any) (*Schema, SchemaWriter) {
if conv, ok := schema.(SchemaConverter); ok {
return conv.ToSchema(), nil
} else if writer, ok := schema.(SchemaWriter); ok {
return nil, writer
}
switch ts := schema.(type) {
case Schema:
return &ts, nil
case *Schema:
return ts, nil
}
return nil, nil
}
func extractSchema(example any, isArray bool) (*Schema, bool) {
arrayDetect := isArray
st := reflect.TypeOf(example)
sv := reflect.ValueOf(example)
hasValue := true
if st.Kind() == reflect.Pointer {
st = st.Elem()
sv = sv.Elem()
}
if st.Kind() == reflect.Slice {
arrayDetect = true
st = st.Elem()
if sv.Len() > 0 {
sv = sv.Index(0)
} else {
hasValue = false
}
}
if st.Kind() == reflect.Struct {
ptys := make([]Property, 0)
reqds := make([]string, 0)
for i := 0; i < st.NumField(); i++ {
fld := st.Field(i)
if fld.IsExported() {
var fv *reflect.Value
if hasValue {
fvv := sv.FieldByName(fld.Name)
fv = &fvv
}
pty, reqd := extractSchemaField(fld, fv)
ptys = append(ptys, pty)
if reqd {
reqds = append(reqds, pty.Name)
}
}
}
return &Schema{
RequiredProperties: reqds,
Properties: ptys,
}, arrayDetect
}
return nil, false
}
func extractSchemaField(fld reflect.StructField, value *reflect.Value) (Property, bool) {
reqd := true
name := fld.Name
if tgv, ok := fld.Tag.Lookup("json"); ok {
if tgs := strings.Split(tgv, ","); len(tgs) > 1 {
name = tgs[0]
reqd = tgs[1] != "omitempty"
} else {
name = tgv
}
}
k := fld.Type.Kind()
if k == reflect.Pointer {
reqd = false
k = fld.Type.Elem().Kind()
}
ft := "null"
var eg any
switch k {
case reflect.String:
ft = tagValueTypeString
if value != nil {
eg = value.Interface()
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ft = tagValueTypeInteger
if value != nil {
eg = value.Interface()
}
case reflect.Float32, reflect.Float64:
ft = tagValueTypeNumber
if value != nil {
eg = value.Interface()
}
case reflect.Bool:
ft = tagValueTypeBoolean
if value != nil {
eg = value.Interface()
}
case reflect.Slice:
ft = tagValueTypeArray
case reflect.Map, reflect.Struct:
ft = tagValueTypeObject
}
return Property{
Name: name,
Type: ft,
Example: eg,
}, reqd
}