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