Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / go-openapi / spec / items.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         "strings"
20
21         "github.com/go-openapi/jsonpointer"
22         "github.com/go-openapi/swag"
23 )
24
25 const (
26         jsonRef = "$ref"
27 )
28
29 // SimpleSchema describe swagger simple schemas for parameters and headers
30 type SimpleSchema struct {
31         Type             string      `json:"type,omitempty"`
32         Format           string      `json:"format,omitempty"`
33         Items            *Items      `json:"items,omitempty"`
34         CollectionFormat string      `json:"collectionFormat,omitempty"`
35         Default          interface{} `json:"default,omitempty"`
36         Example          interface{} `json:"example,omitempty"`
37 }
38
39 // TypeName return the type (or format) of a simple schema
40 func (s *SimpleSchema) TypeName() string {
41         if s.Format != "" {
42                 return s.Format
43         }
44         return s.Type
45 }
46
47 // ItemsTypeName yields the type of items in a simple schema array
48 func (s *SimpleSchema) ItemsTypeName() string {
49         if s.Items == nil {
50                 return ""
51         }
52         return s.Items.TypeName()
53 }
54
55 // CommonValidations describe common JSON-schema validations
56 type CommonValidations struct {
57         Maximum          *float64      `json:"maximum,omitempty"`
58         ExclusiveMaximum bool          `json:"exclusiveMaximum,omitempty"`
59         Minimum          *float64      `json:"minimum,omitempty"`
60         ExclusiveMinimum bool          `json:"exclusiveMinimum,omitempty"`
61         MaxLength        *int64        `json:"maxLength,omitempty"`
62         MinLength        *int64        `json:"minLength,omitempty"`
63         Pattern          string        `json:"pattern,omitempty"`
64         MaxItems         *int64        `json:"maxItems,omitempty"`
65         MinItems         *int64        `json:"minItems,omitempty"`
66         UniqueItems      bool          `json:"uniqueItems,omitempty"`
67         MultipleOf       *float64      `json:"multipleOf,omitempty"`
68         Enum             []interface{} `json:"enum,omitempty"`
69 }
70
71 // Items a limited subset of JSON-Schema's items object.
72 // It is used by parameter definitions that are not located in "body".
73 //
74 // For more information: http://goo.gl/8us55a#items-object
75 type Items struct {
76         Refable
77         CommonValidations
78         SimpleSchema
79         VendorExtensible
80 }
81
82 // NewItems creates a new instance of items
83 func NewItems() *Items {
84         return &Items{}
85 }
86
87 // Typed a fluent builder method for the type of item
88 func (i *Items) Typed(tpe, format string) *Items {
89         i.Type = tpe
90         i.Format = format
91         return i
92 }
93
94 // CollectionOf a fluent builder method for an array item
95 func (i *Items) CollectionOf(items *Items, format string) *Items {
96         i.Type = jsonArray
97         i.Items = items
98         i.CollectionFormat = format
99         return i
100 }
101
102 // WithDefault sets the default value on this item
103 func (i *Items) WithDefault(defaultValue interface{}) *Items {
104         i.Default = defaultValue
105         return i
106 }
107
108 // WithMaxLength sets a max length value
109 func (i *Items) WithMaxLength(max int64) *Items {
110         i.MaxLength = &max
111         return i
112 }
113
114 // WithMinLength sets a min length value
115 func (i *Items) WithMinLength(min int64) *Items {
116         i.MinLength = &min
117         return i
118 }
119
120 // WithPattern sets a pattern value
121 func (i *Items) WithPattern(pattern string) *Items {
122         i.Pattern = pattern
123         return i
124 }
125
126 // WithMultipleOf sets a multiple of value
127 func (i *Items) WithMultipleOf(number float64) *Items {
128         i.MultipleOf = &number
129         return i
130 }
131
132 // WithMaximum sets a maximum number value
133 func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
134         i.Maximum = &max
135         i.ExclusiveMaximum = exclusive
136         return i
137 }
138
139 // WithMinimum sets a minimum number value
140 func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
141         i.Minimum = &min
142         i.ExclusiveMinimum = exclusive
143         return i
144 }
145
146 // WithEnum sets a the enum values (replace)
147 func (i *Items) WithEnum(values ...interface{}) *Items {
148         i.Enum = append([]interface{}{}, values...)
149         return i
150 }
151
152 // WithMaxItems sets the max items
153 func (i *Items) WithMaxItems(size int64) *Items {
154         i.MaxItems = &size
155         return i
156 }
157
158 // WithMinItems sets the min items
159 func (i *Items) WithMinItems(size int64) *Items {
160         i.MinItems = &size
161         return i
162 }
163
164 // UniqueValues dictates that this array can only have unique items
165 func (i *Items) UniqueValues() *Items {
166         i.UniqueItems = true
167         return i
168 }
169
170 // AllowDuplicates this array can have duplicates
171 func (i *Items) AllowDuplicates() *Items {
172         i.UniqueItems = false
173         return i
174 }
175
176 // UnmarshalJSON hydrates this items instance with the data from JSON
177 func (i *Items) UnmarshalJSON(data []byte) error {
178         var validations CommonValidations
179         if err := json.Unmarshal(data, &validations); err != nil {
180                 return err
181         }
182         var ref Refable
183         if err := json.Unmarshal(data, &ref); err != nil {
184                 return err
185         }
186         var simpleSchema SimpleSchema
187         if err := json.Unmarshal(data, &simpleSchema); err != nil {
188                 return err
189         }
190         var vendorExtensible VendorExtensible
191         if err := json.Unmarshal(data, &vendorExtensible); err != nil {
192                 return err
193         }
194         i.Refable = ref
195         i.CommonValidations = validations
196         i.SimpleSchema = simpleSchema
197         i.VendorExtensible = vendorExtensible
198         return nil
199 }
200
201 // MarshalJSON converts this items object to JSON
202 func (i Items) MarshalJSON() ([]byte, error) {
203         b1, err := json.Marshal(i.CommonValidations)
204         if err != nil {
205                 return nil, err
206         }
207         b2, err := json.Marshal(i.SimpleSchema)
208         if err != nil {
209                 return nil, err
210         }
211         b3, err := json.Marshal(i.Refable)
212         if err != nil {
213                 return nil, err
214         }
215         b4, err := json.Marshal(i.VendorExtensible)
216         if err != nil {
217                 return nil, err
218         }
219         return swag.ConcatJSON(b4, b3, b1, b2), nil
220 }
221
222 // JSONLookup look up a value by the json property name
223 func (i Items) JSONLookup(token string) (interface{}, error) {
224         if token == jsonRef {
225                 return &i.Ref, nil
226         }
227
228         r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)
229         if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
230                 return nil, err
231         }
232         if r != nil {
233                 return r, nil
234         }
235         r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)
236         return r, err
237 }