-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparser.go
More file actions
319 lines (297 loc) · 9.06 KB
/
parser.go
File metadata and controls
319 lines (297 loc) · 9.06 KB
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
package openapi
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
type parseOptions struct {
DefaultEncoding string // default encoding for the schema, used for []byte
}
type ParseOption func(*parseOptions)
// WithDefaultEncoding sets the default encoding for the schema for []byte fields.
// The default encoding is Base64Encoding.
func WithDefaultEncoding(v string) ParseOption {
return func(opts *parseOptions) {
opts.DefaultEncoding = v
}
}
const is64Bit = uint64(^uintptr(0)) == ^uint64(0)
// ParseObject parses the object and returns the schema or the reference to the schema.
//
// The object can be a struct, pointer to struct, map, slice, pointer to map or slice, or any other type.
// The object can contain fields with `json`, `yaml` or `openapi` tags.
//
// `openapi:"<name>[,ref:<ref> || any other tags]"` tag:
// - <name> is the name of the field in the schema, can be "-" to skip the field or empty to use the name from json, yaml tags or original field name.
// json schema fields:
// - ref:<ref> is a reference to the schema, can not be used with jsonschema fields.
// - required, marks the field as required by adding it to the required list of the parent schema.
// - deprecated, marks the field as deprecated.
// - title:<title>, sets the title of the field or summary for the fereference.
// - summary:<summary>, sets the summary of the reference.
// - description:<description>, sets the description of the field.
// - type:<type> (boolean, integer, number, string, array, object), may be used multiple times.
// The first usage overrides the default type, all other types are added.
// - addtype:<type>, adds additional type, may be used multiple times.
// - format:<format>, sets the format of the type.
//
// The `components` parameter is needed to store the schemas of the structs, and to avoid the circular references.
// In case of the given object is struct, the function will return a reference to the schema stored in the components
// Otherwise, the function will return the schema itself.
func ParseObject(obj any, components *Extendable[Components], opts ...ParseOption) (*SchemaBuilder, error) {
opt := &parseOptions{
DefaultEncoding: Base64Encoding, // default encoding for []byte
}
for _, o := range opts {
o(opt)
}
t := reflect.TypeOf(obj)
if t == nil {
return NewSchemaBuilder().Type(NullType).GoType("nil"), nil
}
return parseObject(joinLoc("", t.String()), t, components, opt)
}
// MapKeyMustBeStringError is an error that is returned when the map key is not a string.
type MapKeyMustBeStringError struct {
Location string
KeyType reflect.Kind
}
func (e *MapKeyMustBeStringError) Error() string {
return fmt.Sprintf("%s: unsupported map key type %s, expected string", e.Location, e.KeyType)
}
func (e *MapKeyMustBeStringError) Is(target error) bool {
if target == nil {
return false
}
_, ok := target.(*MapKeyMustBeStringError)
return ok
}
// NewMapKeyMustBeStringError creates a new MapKeyMustBeStringError with the given location and key type.
func NewMapKeyMustBeStringError(location string, keyType reflect.Kind) *MapKeyMustBeStringError {
return &MapKeyMustBeStringError{
Location: location,
KeyType: keyType,
}
}
func parseObject(location string, t reflect.Type, components *Extendable[Components], opt *parseOptions) (*SchemaBuilder, error) {
if t == nil {
return NewSchemaBuilder().Type(NullType).GoType("nil"), nil
}
kind := t.Kind()
if kind == reflect.Ptr {
builder, err := parseObject(location, t.Elem(), components, opt)
if err != nil {
return nil, err
}
if builder.IsRef() {
builder = NewSchemaBuilder().OneOf(
builder.Build(),
NewSchemaBuilder().Type(NullType).Build(),
)
} else {
builder.AddType(NullType)
}
return builder, nil
}
if kind == reflect.Interface {
return NewSchemaBuilder().GoType("any"), nil
}
obj := reflect.New(t).Elem()
builder := NewSchemaBuilder().GoType(fmt.Sprintf("%T", obj.Interface()))
switch obj.Interface().(type) {
case bool:
builder.Type(BooleanType)
case int, uint:
if is64Bit {
builder.Type(IntegerType).Format(Int64Format)
} else {
builder.Type(IntegerType).Format(Int32Format)
}
case int8, int16, int32, uint8, uint16, uint32:
builder.Type(IntegerType).Format(Int32Format)
case int64, uint64:
builder.Type(IntegerType).Format(Int64Format)
case float32:
builder.Type(NumberType).Format(FloatFormat)
case float64:
builder.Type(NumberType).Format(DoubleFormat)
case string:
builder.Type(StringType)
case []byte:
builder.Type(StringType).ContentEncoding(opt.DefaultEncoding).GoType("[]byte")
case json.Number:
builder.Type(NumberType).GoPackage(t.PkgPath())
case json.RawMessage:
builder.Type(StringType).ContentMediaType("application/json").GoPackage(t.PkgPath())
default:
switch kind {
case reflect.Array, reflect.Slice:
var elemSchema any
elem := t.Elem()
if elem.Kind() == reflect.Interface {
elemSchema = true
} else {
var err error
elemSchema, err = parseObject(location, elem, components, opt)
if err != nil {
return nil, err
}
}
builder.Type(ArrayType).Items(NewBoolOrSchema(elemSchema)).GoType("")
case reflect.Map:
if k := t.Key().Kind(); k != reflect.String {
return nil, NewMapKeyMustBeStringError(location, k)
}
var elemSchema any
elem := t.Elem()
if elem.Kind() == reflect.Interface {
elemSchema = true
} else {
var err error
elemSchema, err = parseObject(location, elem, components, opt)
if err != nil {
return nil, err
}
}
builder.Type(ObjectType).AdditionalProperties(NewBoolOrSchema(elemSchema)).GoType("")
case reflect.Struct:
objName := strings.ReplaceAll(t.PkgPath()+"."+t.Name(), "/", ".")
if components.Spec.Schemas[objName] != nil {
return NewSchemaBuilder().Ref("#/components/schemas/" + objName), nil
}
// add a temporary schema to avoid circular references
if components.Spec.Schemas == nil {
components.Spec.Schemas = make(map[string]*RefOrSpec[Schema], 1)
}
// reserve the name of the schema
const toBeDeleted = "to be deleted"
components.Spec.Schemas[objName] = NewSchemaBuilder().Ref(toBeDeleted).Build()
var allOf []*RefOrSpec[Schema]
for i := range t.NumField() {
field := t.Field(i)
// skip unexported fields
if !field.IsExported() {
continue
}
fieldSchema, err := parseObject(joinLoc(location, field.Name), obj.Field(i).Type(), components, opt)
if err != nil {
// remove the temporary schema
delete(components.Spec.Schemas, objName)
return nil, err
}
if field.Anonymous {
allOf = append(allOf, fieldSchema.Build())
continue
}
name := applyTag(&field, fieldSchema, builder)
// skip the field if it's marked as "-"
if name == "-" {
continue
}
builder.AddProperty(name, fieldSchema.Build())
}
if len(allOf) > 0 {
allOf = append(allOf, builder.Type(ObjectType).GoType("").Build())
builder = NewSchemaBuilder().AllOf(allOf...).GoType(t.String())
} else {
builder.Type(ObjectType)
}
builder.GoPackage(t.PkgPath())
components.Spec.Schemas[objName] = builder.Build()
builder = NewSchemaBuilder().Ref("#/components/schemas/" + objName)
default:
// ignore unsupported types gracefully
}
}
return builder, nil
}
func applyTag(field *reflect.StructField, schema, parent *SchemaBuilder) (name string) {
name = field.Name
for _, tagName := range []string{"json", "yaml"} {
if tag, ok := field.Tag.Lookup(tagName); ok {
parts := strings.SplitN(tag, ",", 2)
if len(parts) > 0 {
part := strings.TrimSpace(parts[0])
if part != "" {
name = part
break
}
}
}
}
tag, ok := field.Tag.Lookup("openapi")
if !ok {
return
}
parts := strings.Split(tag, ",")
if len(parts) == 0 {
return
}
if parts[0] != "" {
name = parts[0]
}
if name == "-" {
return parts[0]
}
parts = parts[1:]
if len(parts) == 0 {
return
}
if strings.HasPrefix(parts[0], "ref:") {
schema.Ref(parts[0][4:])
}
var isTypeOverridden bool
for _, part := range parts {
prefixIndex := strings.Index(part, ":")
var prefix string
if prefixIndex == -1 {
prefix = part
} else {
prefix = part[:prefixIndex]
if prefixIndex == len(part)-1 {
part = ""
}
part = part[prefixIndex+1:]
}
// the tags for the references only
if schema.IsRef() {
switch prefix {
case "required":
parent.AddRequired(name)
case "description":
schema.Description(part)
case "title", "summary":
schema.Title(part)
default:
// ignore unknown or unsupported tag prefixes gracefully
}
continue
}
switch prefix {
case "required":
parent.AddRequired(name)
case "deprecated":
schema.Deprecated(true)
case "title":
schema.Title(part)
case "description":
schema.Description(part)
case "type":
// first type overrides the default type, all other types are added
if !isTypeOverridden {
schema.Type(part)
isTypeOverridden = true
} else {
schema.AddType(part)
}
case "addtype":
schema.AddType(part)
case "format":
schema.Format(part)
default:
// handle unknown or unsupported tag prefixes gracefully
}
}
return
}