-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathentities.go
333 lines (284 loc) · 12 KB
/
entities.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
package swgen
import (
"encoding/json"
"strings"
)
// ServiceType data type for type of your service
type ServiceType string
const (
// ServiceTypeRest define service type for RESTful service
ServiceTypeRest ServiceType = "rest"
// ServiceTypeJSONRPC define service type for JSON-RPC service
ServiceTypeJSONRPC ServiceType = "json-rpc"
)
// Document represent for a document object of swagger data
// see http://swagger.io/specification/
type Document struct {
Version string `json:"swagger"` // Specifies the Swagger Specification version being used
Info InfoObj `json:"info"` // Provides metadata about the API
Host string `json:"host,omitempty"` // The host (name or ip) serving the API
BasePath string `json:"basePath,omitempty"` // The base path on which the API is served, which is relative to the host
Schemes []string `json:"schemes,omitempty"` // Values MUST be from the list: "http", "https", "ws", "wss"
Paths map[string]PathItem `json:"paths"` // The available paths and operations for the API
Definitions map[string]SchemaObj `json:"definitions"` // An object to hold data types produced and consumed by operations
SecurityDefinitions map[string]SecurityDef `json:"securityDefinitions,omitempty"` // An object to hold available security mechanisms
additionalData
}
type _Document Document
// MarshalJSON marshal Document with additionalData inlined
func (s Document) MarshalJSON() ([]byte, error) {
return s.marshalJSONWithStruct(_Document(s))
}
// InfoObj provides metadata about the API
type InfoObj struct {
Title string `json:"title"` // The title of the application
Description string `json:"description"`
TermsOfService string `json:"termsOfService"`
Contact ContactObj `json:"contact"`
License LicenseObj `json:"license"`
Version string `json:"version"`
}
// ContactObj contains contact information for the exposed API
type ContactObj struct {
Name string `json:"name"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
// LicenseObj license information for the exposed API
type LicenseObj struct {
Name string `json:"name"`
URL string `json:"url,omitempty"`
}
// PathItem describes the operations available on a single path
// see http://swagger.io/specification/#pathItemObject
type PathItem struct {
Ref string `json:"$ref,omitempty"`
Get *OperationObj `json:"get,omitempty"`
Put *OperationObj `json:"put,omitempty"`
Post *OperationObj `json:"post,omitempty"`
Delete *OperationObj `json:"delete,omitempty"`
Options *OperationObj `json:"options,omitempty"`
Head *OperationObj `json:"head,omitempty"`
Patch *OperationObj `json:"patch,omitempty"`
Params *ParamObj `json:"parameters,omitempty"`
}
// HasMethod returns true if in path item already have operation for given method
func (pi PathItem) HasMethod(method string) bool {
switch strings.ToUpper(method) {
case "GET":
return pi.Get != nil
case "POST":
return pi.Post != nil
case "PUT":
return pi.Put != nil
case "DELETE":
return pi.Delete != nil
case "OPTIONS":
return pi.Options != nil
case "HEAD":
return pi.Head != nil
case "PATCH":
return pi.Patch != nil
}
return false
}
type securityType string
const (
// SecurityBasicAuth is a HTTP Basic Authentication security type
SecurityBasicAuth securityType = "basic"
// SecurityAPIKey is an API key security type
SecurityAPIKey securityType = "apiKey"
// SecurityOAuth2 is an OAuth2 security type
SecurityOAuth2 securityType = "oauth2"
)
type apiKeyIn string
const (
// APIKeyInHeader defines API key in header
APIKeyInHeader apiKeyIn = "header"
// APIKeyInQuery defines API key in query parameter
APIKeyInQuery apiKeyIn = "query"
)
type oauthFlow string
const (
// Oauth2AccessCode is access code Oauth2 flow
Oauth2AccessCode oauthFlow = "accessCode"
// Oauth2Application is application Oauth2 flow
Oauth2Application oauthFlow = "application"
// Oauth2Implicit is implicit Oauth2 flow
Oauth2Implicit oauthFlow = "implicit"
// Oauth2Password is password Oauth2 flow
Oauth2Password oauthFlow = "password"
)
// SecurityDef holds security definition
type SecurityDef struct {
Type securityType `json:"type"`
// apiKey properties
In apiKeyIn `json:"in,omitempty"`
Name string `json:"name,omitempty"` // Example: X-API-Key
// oauth2 properties
Flow oauthFlow `json:"flow,omitempty"`
AuthorizationURL string `json:"authorizationUrl,omitempty"` // Example: https://example.com/oauth/authorize
TokenURL string `json:"tokenUrl,omitempty"` // Example: https://example.com/oauth/token
Scopes map[string]string `json:"scopes,omitempty"` // Example: {"read": "Grants read access", "write": "Grants write access"}
}
// PathItemInfo some basic information of a path item and operation object
type PathItemInfo struct {
Path string
Method string
Title string
Description string
Tag string
Deprecated bool
Security []string // Names of security definitions
SecurityOAuth2 map[string][]string // Map of names of security definitions to required scopes
additionalData
}
// Enum can be use for sending Enum data that need validate
type Enum struct {
Enum []interface{} `json:"enum,omitempty"`
EnumNames []string `json:"x-enum-names,omitempty"`
}
type enumer interface {
// GetEnumSlices return the const-name pair slice
GetEnumSlices() ([]interface{}, []string)
}
// OperationObj describes a single API operation on a path
// see http://swagger.io/specification/#operationObject
type OperationObj struct {
Tags []string `json:"tags,omitempty"`
Summary string `json:"summary"` // like a title, a short summary of what the operation does (120 chars)
Description string `json:"description"` // A verbose explanation of the operation behavior
Parameters []ParamObj `json:"parameters,omitempty"`
Responses Responses `json:"responses"`
Security []map[string][]string `json:"security,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
additionalData
}
type _OperationObj OperationObj
// MarshalJSON marshal OperationObj with additionalData inlined
func (o OperationObj) MarshalJSON() ([]byte, error) {
return o.marshalJSONWithStruct(_OperationObj(o))
}
// ParamObj describes a single operation parameter
// see http://swagger.io/specification/#parameterObject
type ParamObj struct {
Ref string `json:"$ref,omitempty"`
Name string `json:"name"`
In string `json:"in"` // Possible values are "query", "header", "path", "formData" or "body"
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Items *ParamItemObj `json:"items,omitempty"` // Required if type is "array"
Schema *SchemaObj `json:"schema,omitempty"` // Required if type is "body"
CollectionFormat string `json:"collectionFormat,omitempty"` // "multi" - this is valid only for parameters in "query" or "formData"
Description string `json:"description,omitempty"`
Default interface{} `json:"default,omitempty"`
Required bool `json:"required,omitempty"`
Enum
additionalData
}
type _ParamObj ParamObj
// MarshalJSON marshal OperationObj with additionalData inlined
func (o ParamObj) MarshalJSON() ([]byte, error) {
return o.marshalJSONWithStruct(_ParamObj(o))
}
// ParamItemObj describes an property object, in param object or property of definition
// see http://swagger.io/specification/#itemsObject
type ParamItemObj struct {
Ref string `json:"$ref,omitempty"`
Type string `json:"type"`
Format string `json:"format,omitempty"`
Items *ParamItemObj `json:"items,omitempty"` // Required if type is "array"
CollectionFormat string `json:"collectionFormat,omitempty"` // "multi" - this is valid only for parameters in "query" or "formData"
}
// Responses list of response object
type Responses map[string]ResponseObj
// ResponseObj describes a single response from an API Operation
type ResponseObj struct {
Ref string `json:"$ref,omitempty"`
Description string `json:"description,omitempty"`
Schema *SchemaObj `json:"schema,omitempty"`
Headers interface{} `json:"headers,omitempty"`
Examples interface{} `json:"examples,omitempty"`
}
// SchemaObj describes a schema for json format
type SchemaObj struct {
Ref string `json:"$ref,omitempty"`
Description string `json:"description,omitempty"`
Default interface{} `json:"default,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Title string `json:"title,omitempty"`
Items *SchemaObj `json:"items,omitempty"` // if type is array
AdditionalProperties *SchemaObj `json:"additionalProperties,omitempty"` // if type is object (map[])
Properties map[string]SchemaObj `json:"properties,omitempty"` // if type is object
TypeName string `json:"-"` // for internal using, passing typeName
GoType string `json:"x-go-type,omitempty"`
GoPropertyNames map[string]string `json:"x-go-property-names,omitempty"`
GoPropertyTypes map[string]string `json:"x-go-property-types,omitempty"`
}
// NewSchemaObj Constructor function for SchemaObj struct type
func NewSchemaObj(jsonType, typeName string) (so *SchemaObj) {
so = &SchemaObj{
Type: jsonType,
TypeName: typeName,
}
if typeName != "" {
so.Ref = refDefinitionPrefix + typeName
}
return
}
// Checks whether current SchemaObj is "empty". A schema object is considered "empty" if it is an object without visible
// (exported) properties, an array without elements, or in other cases when it has neither regular nor additional
// properties, and format is not specified. Schema objects that describe common types ("string", "integer", "boolean" etc.)
// are always considered non-empty. Same is true for "schema reference objects" (objects that have a non-empty Ref field).
func (so *SchemaObj) isEmpty() bool {
if isCommonName(so.TypeName) || so.Ref != "" {
return false
}
switch so.Type {
case "object":
return len(so.Properties) == 0
case "array":
return so.Items == nil
default:
return len(so.Properties) == 0 && so.AdditionalProperties == nil && so.Format == ""
}
}
// Export returns a "schema reference object" corresponding to this schema object. A "schema reference object" is an abridged
// version of the original SchemaObj, having only two non-empty fields: Ref and TypeName. "Schema reference objects"
// are used to refer original schema objects from other schemas.
func (so SchemaObj) Export() SchemaObj {
return SchemaObj{
Ref: so.Ref,
TypeName: so.TypeName,
}
}
type additionalData struct {
data map[string]interface{}
}
// AddExtendedField add field to additional data map
func (ad *additionalData) AddExtendedField(name string, value interface{}) {
if ad.data == nil {
ad.data = make(map[string]interface{})
}
ad.data[name] = value
}
func (ad additionalData) marshalJSONWithStruct(i interface{}) ([]byte, error) {
result, err := json.Marshal(i)
if err != nil {
return result, err
}
if len(ad.data) == 0 {
return result, nil
}
dataJSON, err := json.Marshal(ad.data)
if err != nil {
return dataJSON, err
}
if string(result) == "{}" {
return dataJSON, nil
}
result = append(result[:len(result)-1], ',')
result = append(result, dataJSON[1:]...)
return result, nil
}