forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
293 lines (260 loc) · 6.43 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
package swag
import (
"errors"
"fmt"
"github.com/go-openapi/spec"
)
const (
// ARRAY represent a array value.
ARRAY = "array"
// OBJECT represent a object value.
OBJECT = "object"
// PRIMITIVE represent a primitive value.
PRIMITIVE = "primitive"
// BOOLEAN represent a boolean value.
BOOLEAN = "boolean"
// INTEGER represent a integer value.
INTEGER = "integer"
// NUMBER represent a number value.
NUMBER = "number"
// STRING represent a string value.
STRING = "string"
// FUNC represent a function value.
FUNC = "func"
// ERROR represent a error value.
ERROR = "error"
// INTERFACE represent a interface value.
INTERFACE = "interface{}"
// ANY represent a any value.
ANY = "any"
// NIL represent a empty value.
NIL = "nil"
// IgnoreNameOverridePrefix Prepend to model to avoid renaming based on comment.
IgnoreNameOverridePrefix = '$'
)
// CheckSchemaType checks if typeName is not a name of primitive type.
func CheckSchemaType(typeName string) error {
if !IsPrimitiveType(typeName) {
return fmt.Errorf("%s is not basic types", typeName)
}
return nil
}
// IsSimplePrimitiveType determine whether the type name is a simple primitive type.
func IsSimplePrimitiveType(typeName string) bool {
switch typeName {
case STRING, NUMBER, INTEGER, BOOLEAN:
return true
}
return false
}
// IsPrimitiveType determine whether the type name is a primitive type.
func IsPrimitiveType(typeName string) bool {
switch typeName {
case STRING, NUMBER, INTEGER, BOOLEAN, ARRAY, OBJECT, FUNC:
return true
}
return false
}
// IsInterfaceLike determines whether the swagger type name is an go named interface type like error type.
func IsInterfaceLike(typeName string) bool {
return typeName == ERROR || typeName == ANY
}
// IsNumericType determines whether the swagger type name is a numeric type.
func IsNumericType(typeName string) bool {
return typeName == INTEGER || typeName == NUMBER
}
// TransToValidSchemeType indicates type will transfer golang basic type to swagger supported type.
func TransToValidSchemeType(typeName string) string {
switch typeName {
case "uint", "int", "uint8", "int8", "uint16", "int16", "byte":
return INTEGER
case "uint32", "int32", "rune":
return INTEGER
case "uint64", "int64":
return INTEGER
case "float32", "float64":
return NUMBER
case "bool":
return BOOLEAN
case "string":
return STRING
}
return typeName
}
// IsGolangPrimitiveType determine whether the type name is a golang primitive type.
func IsGolangPrimitiveType(typeName string) bool {
switch typeName {
case "uint",
"int",
"uint8",
"int8",
"uint16",
"int16",
"byte",
"uint32",
"int32",
"rune",
"uint64",
"int64",
"float32",
"float64",
"bool",
"string":
return true
}
return false
}
// TransToValidCollectionFormat determine valid collection format.
func TransToValidCollectionFormat(format string) string {
switch format {
case "csv", "multi", "pipes", "tsv", "ssv":
return format
}
return ""
}
func ignoreNameOverride(name string) bool {
return len(name) != 0 && name[0] == IgnoreNameOverridePrefix
}
// IsComplexSchema whether a schema is complex and should be a ref schema
func IsComplexSchema(schema *spec.Schema) bool {
// a enum type should be complex
if len(schema.Enum) > 0 {
return true
}
// a deep array type is complex, how to determine deep? here more than 2 ,for example: [][]object,[][][]int
if len(schema.Type) > 2 {
return true
}
//Object included, such as Object or []Object
for _, st := range schema.Type {
if st == OBJECT {
return true
}
}
return false
}
// IsRefSchema whether a schema is a reference schema.
func IsRefSchema(schema *spec.Schema) bool {
return schema.Ref.Ref.GetURL() != nil
}
// RefSchema build a reference schema.
func RefSchema(refType string) *spec.Schema {
return spec.RefSchema("#/definitions/" + refType)
}
// PrimitiveSchema build a primitive schema.
func PrimitiveSchema(refType string) *spec.Schema {
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{refType}}}
}
// BuildCustomSchema build custom schema specified by tag swaggertype.
func BuildCustomSchema(types []string) (*spec.Schema, error) {
if len(types) == 0 {
return nil, nil
}
switch types[0] {
case PRIMITIVE:
if len(types) == 1 {
return nil, errors.New("need primitive type after primitive")
}
return BuildCustomSchema(types[1:])
case ARRAY:
if len(types) == 1 {
return nil, errors.New("need array item type after array")
}
schema, err := BuildCustomSchema(types[1:])
if err != nil {
return nil, err
}
return spec.ArrayProperty(schema), nil
case OBJECT:
if len(types) == 1 {
return PrimitiveSchema(types[0]), nil
}
schema, err := BuildCustomSchema(types[1:])
if err != nil {
return nil, err
}
return spec.MapProperty(schema), nil
default:
err := CheckSchemaType(types[0])
if err != nil {
return nil, err
}
return PrimitiveSchema(types[0]), nil
}
}
// MergeSchema merge schemas
func MergeSchema(dst *spec.Schema, src *spec.Schema) *spec.Schema {
if len(src.Type) > 0 {
dst.Type = src.Type
}
if len(src.Properties) > 0 {
dst.Properties = src.Properties
}
if src.Items != nil {
dst.Items = src.Items
}
if src.AdditionalProperties != nil {
dst.AdditionalProperties = src.AdditionalProperties
}
if len(src.Description) > 0 {
dst.Description = src.Description
}
if src.Nullable {
dst.Nullable = src.Nullable
}
if len(src.Format) > 0 {
dst.Format = src.Format
}
if src.Default != nil {
dst.Default = src.Default
}
if src.Example != nil {
dst.Example = src.Example
}
if len(src.Extensions) > 0 {
dst.Extensions = src.Extensions
}
if src.Maximum != nil {
dst.Maximum = src.Maximum
}
if src.Minimum != nil {
dst.Minimum = src.Minimum
}
if src.ExclusiveMaximum {
dst.ExclusiveMaximum = src.ExclusiveMaximum
}
if src.ExclusiveMinimum {
dst.ExclusiveMinimum = src.ExclusiveMinimum
}
if src.MaxLength != nil {
dst.MaxLength = src.MaxLength
}
if src.MinLength != nil {
dst.MinLength = src.MinLength
}
if len(src.Pattern) > 0 {
dst.Pattern = src.Pattern
}
if src.MaxItems != nil {
dst.MaxItems = src.MaxItems
}
if src.MinItems != nil {
dst.MinItems = src.MinItems
}
if src.UniqueItems {
dst.UniqueItems = src.UniqueItems
}
if src.MultipleOf != nil {
dst.MultipleOf = src.MultipleOf
}
if len(src.Enum) > 0 {
dst.Enum = src.Enum
}
if len(src.Extensions) > 0 {
dst.Extensions = src.Extensions
}
if len(src.ExtraProps) > 0 {
dst.ExtraProps = src.ExtraProps
}
return dst
}