Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / go-openapi / spec / swagger.go
1 // Copyright 2015 go-swagger maintainers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package spec
16
17 import (
18         "encoding/json"
19         "fmt"
20         "strconv"
21
22         "github.com/go-openapi/jsonpointer"
23         "github.com/go-openapi/swag"
24 )
25
26 // Swagger this is the root document object for the API specification.
27 // It combines what previously was the Resource Listing and API Declaration (version 1.2 and earlier)
28 // together into one document.
29 //
30 // For more information: http://goo.gl/8us55a#swagger-object-
31 type Swagger struct {
32         VendorExtensible
33         SwaggerProps
34 }
35
36 // JSONLookup look up a value by the json property name
37 func (s Swagger) JSONLookup(token string) (interface{}, error) {
38         if ex, ok := s.Extensions[token]; ok {
39                 return &ex, nil
40         }
41         r, _, err := jsonpointer.GetForToken(s.SwaggerProps, token)
42         return r, err
43 }
44
45 // MarshalJSON marshals this swagger structure to json
46 func (s Swagger) MarshalJSON() ([]byte, error) {
47         b1, err := json.Marshal(s.SwaggerProps)
48         if err != nil {
49                 return nil, err
50         }
51         b2, err := json.Marshal(s.VendorExtensible)
52         if err != nil {
53                 return nil, err
54         }
55         return swag.ConcatJSON(b1, b2), nil
56 }
57
58 // UnmarshalJSON unmarshals a swagger spec from json
59 func (s *Swagger) UnmarshalJSON(data []byte) error {
60         var sw Swagger
61         if err := json.Unmarshal(data, &sw.SwaggerProps); err != nil {
62                 return err
63         }
64         if err := json.Unmarshal(data, &sw.VendorExtensible); err != nil {
65                 return err
66         }
67         *s = sw
68         return nil
69 }
70
71 // SwaggerProps captures the top-level properties of an Api specification
72 //
73 // NOTE: validation rules
74 // - the scheme, when present must be from [http, https, ws, wss]
75 // - BasePath must start with a leading "/"
76 // - Paths is required
77 type SwaggerProps struct {
78         ID                  string                 `json:"id,omitempty"`
79         Consumes            []string               `json:"consumes,omitempty"`
80         Produces            []string               `json:"produces,omitempty"`
81         Schemes             []string               `json:"schemes,omitempty"`
82         Swagger             string                 `json:"swagger,omitempty"`
83         Info                *Info                  `json:"info,omitempty"`
84         Host                string                 `json:"host,omitempty"`
85         BasePath            string                 `json:"basePath,omitempty"`
86         Paths               *Paths                 `json:"paths"`
87         Definitions         Definitions            `json:"definitions,omitempty"`
88         Parameters          map[string]Parameter   `json:"parameters,omitempty"`
89         Responses           map[string]Response    `json:"responses,omitempty"`
90         SecurityDefinitions SecurityDefinitions    `json:"securityDefinitions,omitempty"`
91         Security            []map[string][]string  `json:"security,omitempty"`
92         Tags                []Tag                  `json:"tags,omitempty"`
93         ExternalDocs        *ExternalDocumentation `json:"externalDocs,omitempty"`
94 }
95
96 // Dependencies represent a dependencies property
97 type Dependencies map[string]SchemaOrStringArray
98
99 // SchemaOrBool represents a schema or boolean value, is biased towards true for the boolean property
100 type SchemaOrBool struct {
101         Allows bool
102         Schema *Schema
103 }
104
105 // JSONLookup implements an interface to customize json pointer lookup
106 func (s SchemaOrBool) JSONLookup(token string) (interface{}, error) {
107         if token == "allows" {
108                 return s.Allows, nil
109         }
110         r, _, err := jsonpointer.GetForToken(s.Schema, token)
111         return r, err
112 }
113
114 var jsTrue = []byte("true")
115 var jsFalse = []byte("false")
116
117 // MarshalJSON convert this object to JSON
118 func (s SchemaOrBool) MarshalJSON() ([]byte, error) {
119         if s.Schema != nil {
120                 return json.Marshal(s.Schema)
121         }
122
123         if s.Schema == nil && !s.Allows {
124                 return jsFalse, nil
125         }
126         return jsTrue, nil
127 }
128
129 // UnmarshalJSON converts this bool or schema object from a JSON structure
130 func (s *SchemaOrBool) UnmarshalJSON(data []byte) error {
131         var nw SchemaOrBool
132         if len(data) >= 4 {
133                 if data[0] == '{' {
134                         var sch Schema
135                         if err := json.Unmarshal(data, &sch); err != nil {
136                                 return err
137                         }
138                         nw.Schema = &sch
139                 }
140                 nw.Allows = !(data[0] == 'f' && data[1] == 'a' && data[2] == 'l' && data[3] == 's' && data[4] == 'e')
141         }
142         *s = nw
143         return nil
144 }
145
146 // SchemaOrStringArray represents a schema or a string array
147 type SchemaOrStringArray struct {
148         Schema   *Schema
149         Property []string
150 }
151
152 // JSONLookup implements an interface to customize json pointer lookup
153 func (s SchemaOrStringArray) JSONLookup(token string) (interface{}, error) {
154         r, _, err := jsonpointer.GetForToken(s.Schema, token)
155         return r, err
156 }
157
158 // MarshalJSON converts this schema object or array into JSON structure
159 func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) {
160         if len(s.Property) > 0 {
161                 return json.Marshal(s.Property)
162         }
163         if s.Schema != nil {
164                 return json.Marshal(s.Schema)
165         }
166         return []byte("null"), nil
167 }
168
169 // UnmarshalJSON converts this schema object or array from a JSON structure
170 func (s *SchemaOrStringArray) UnmarshalJSON(data []byte) error {
171         var first byte
172         if len(data) > 1 {
173                 first = data[0]
174         }
175         var nw SchemaOrStringArray
176         if first == '{' {
177                 var sch Schema
178                 if err := json.Unmarshal(data, &sch); err != nil {
179                         return err
180                 }
181                 nw.Schema = &sch
182         }
183         if first == '[' {
184                 if err := json.Unmarshal(data, &nw.Property); err != nil {
185                         return err
186                 }
187         }
188         *s = nw
189         return nil
190 }
191
192 // Definitions contains the models explicitly defined in this spec
193 // An object to hold data types that can be consumed and produced by operations.
194 // These data types can be primitives, arrays or models.
195 //
196 // For more information: http://goo.gl/8us55a#definitionsObject
197 type Definitions map[string]Schema
198
199 // SecurityDefinitions a declaration of the security schemes available to be used in the specification.
200 // This does not enforce the security schemes on the operations and only serves to provide
201 // the relevant details for each scheme.
202 //
203 // For more information: http://goo.gl/8us55a#securityDefinitionsObject
204 type SecurityDefinitions map[string]*SecurityScheme
205
206 // StringOrArray represents a value that can either be a string
207 // or an array of strings. Mainly here for serialization purposes
208 type StringOrArray []string
209
210 // Contains returns true when the value is contained in the slice
211 func (s StringOrArray) Contains(value string) bool {
212         for _, str := range s {
213                 if str == value {
214                         return true
215                 }
216         }
217         return false
218 }
219
220 // JSONLookup implements an interface to customize json pointer lookup
221 func (s SchemaOrArray) JSONLookup(token string) (interface{}, error) {
222         if _, err := strconv.Atoi(token); err == nil {
223                 r, _, err := jsonpointer.GetForToken(s.Schemas, token)
224                 return r, err
225         }
226         r, _, err := jsonpointer.GetForToken(s.Schema, token)
227         return r, err
228 }
229
230 // UnmarshalJSON unmarshals this string or array object from a JSON array or JSON string
231 func (s *StringOrArray) UnmarshalJSON(data []byte) error {
232         var first byte
233         if len(data) > 1 {
234                 first = data[0]
235         }
236
237         if first == '[' {
238                 var parsed []string
239                 if err := json.Unmarshal(data, &parsed); err != nil {
240                         return err
241                 }
242                 *s = StringOrArray(parsed)
243                 return nil
244         }
245
246         var single interface{}
247         if err := json.Unmarshal(data, &single); err != nil {
248                 return err
249         }
250         if single == nil {
251                 return nil
252         }
253         switch v := single.(type) {
254         case string:
255                 *s = StringOrArray([]string{v})
256                 return nil
257         default:
258                 return fmt.Errorf("only string or array is allowed, not %T", single)
259         }
260 }
261
262 // MarshalJSON converts this string or array to a JSON array or JSON string
263 func (s StringOrArray) MarshalJSON() ([]byte, error) {
264         if len(s) == 1 {
265                 return json.Marshal([]string(s)[0])
266         }
267         return json.Marshal([]string(s))
268 }
269
270 // SchemaOrArray represents a value that can either be a Schema
271 // or an array of Schema. Mainly here for serialization purposes
272 type SchemaOrArray struct {
273         Schema  *Schema
274         Schemas []Schema
275 }
276
277 // Len returns the number of schemas in this property
278 func (s SchemaOrArray) Len() int {
279         if s.Schema != nil {
280                 return 1
281         }
282         return len(s.Schemas)
283 }
284
285 // ContainsType returns true when one of the schemas is of the specified type
286 func (s *SchemaOrArray) ContainsType(name string) bool {
287         if s.Schema != nil {
288                 return s.Schema.Type != nil && s.Schema.Type.Contains(name)
289         }
290         return false
291 }
292
293 // MarshalJSON converts this schema object or array into JSON structure
294 func (s SchemaOrArray) MarshalJSON() ([]byte, error) {
295         if len(s.Schemas) > 0 {
296                 return json.Marshal(s.Schemas)
297         }
298         return json.Marshal(s.Schema)
299 }
300
301 // UnmarshalJSON converts this schema object or array from a JSON structure
302 func (s *SchemaOrArray) UnmarshalJSON(data []byte) error {
303         var nw SchemaOrArray
304         var first byte
305         if len(data) > 1 {
306                 first = data[0]
307         }
308         if first == '{' {
309                 var sch Schema
310                 if err := json.Unmarshal(data, &sch); err != nil {
311                         return err
312                 }
313                 nw.Schema = &sch
314         }
315         if first == '[' {
316                 if err := json.Unmarshal(data, &nw.Schemas); err != nil {
317                         return err
318                 }
319         }
320         *s = nw
321         return nil
322 }
323
324 // vim:set ft=go noet sts=2 sw=2 ts=2: