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 spec16
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.Format44}45return s.Type46}
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 = tpe75i.Format = format76return i77}
78
79// AsNullable flags this schema as nullable.
80func (i *Items) AsNullable() *Items {81i.Nullable = true82return i83}
84
85// CollectionOf a fluent builder method for an array item
86func (i *Items) CollectionOf(items *Items, format string) *Items {87i.Type = jsonArray88i.Items = items89i.CollectionFormat = format90return i91}
92
93// WithDefault sets the default value on this item
94func (i *Items) WithDefault(defaultValue interface{}) *Items {95i.Default = defaultValue96return i97}
98
99// WithMaxLength sets a max length value
100func (i *Items) WithMaxLength(max int64) *Items {101i.MaxLength = &max102return i103}
104
105// WithMinLength sets a min length value
106func (i *Items) WithMinLength(min int64) *Items {107i.MinLength = &min108return i109}
110
111// WithPattern sets a pattern value
112func (i *Items) WithPattern(pattern string) *Items {113i.Pattern = pattern114return i115}
116
117// WithMultipleOf sets a multiple of value
118func (i *Items) WithMultipleOf(number float64) *Items {119i.MultipleOf = &number120return i121}
122
123// WithMaximum sets a maximum number value
124func (i *Items) WithMaximum(max float64, exclusive bool) *Items {125i.Maximum = &max126i.ExclusiveMaximum = exclusive127return i128}
129
130// WithMinimum sets a minimum number value
131func (i *Items) WithMinimum(min float64, exclusive bool) *Items {132i.Minimum = &min133i.ExclusiveMinimum = exclusive134return i135}
136
137// WithEnum sets a the enum values (replace)
138func (i *Items) WithEnum(values ...interface{}) *Items {139i.Enum = append([]interface{}{}, values...)140return i141}
142
143// WithMaxItems sets the max items
144func (i *Items) WithMaxItems(size int64) *Items {145i.MaxItems = &size146return i147}
148
149// WithMinItems sets the min items
150func (i *Items) WithMinItems(size int64) *Items {151i.MinItems = &size152return i153}
154
155// UniqueValues dictates that this array can only have unique items
156func (i *Items) UniqueValues() *Items {157i.UniqueItems = true158return i159}
160
161// AllowDuplicates this array can have duplicates
162func (i *Items) AllowDuplicates() *Items {163i.UniqueItems = false164return i165}
166
167// WithValidations is a fluent method to set Items validations
168func (i *Items) WithValidations(val CommonValidations) *Items {169i.SetValidations(SchemaValidations{CommonValidations: val})170return i171}
172
173// UnmarshalJSON hydrates this items instance with the data from JSON
174func (i *Items) UnmarshalJSON(data []byte) error {175var validations CommonValidations176if err := json.Unmarshal(data, &validations); err != nil {177return err178}179var ref Refable180if err := json.Unmarshal(data, &ref); err != nil {181return err182}183var simpleSchema SimpleSchema184if err := json.Unmarshal(data, &simpleSchema); err != nil {185return err186}187var vendorExtensible VendorExtensible188if err := json.Unmarshal(data, &vendorExtensible); err != nil {189return err190}191i.Refable = ref192i.CommonValidations = validations193i.SimpleSchema = simpleSchema194i.VendorExtensible = vendorExtensible195return nil196}
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, err203}204b2, err := json.Marshal(i.SimpleSchema)205if err != nil {206return nil, err207}208b3, err := json.Marshal(i.Refable)209if err != nil {210return nil, err211}212b4, err := json.Marshal(i.VendorExtensible)213if err != nil {214return nil, err215}216return swag.ConcatJSON(b4, b3, b1, b2), nil217}
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, nil223}224
225r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)226if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {227return nil, err228}229if r != nil {230return r, nil231}232r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)233return r, err234}
235