podman

Форк
0
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

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
	Nullable         bool        `json:"nullable,omitempty"`
33
	Format           string      `json:"format,omitempty"`
34
	Items            *Items      `json:"items,omitempty"`
35
	CollectionFormat string      `json:"collectionFormat,omitempty"`
36
	Default          interface{} `json:"default,omitempty"`
37
	Example          interface{} `json:"example,omitempty"`
38
}
39

40
// TypeName return the type (or format) of a simple schema
41
func (s *SimpleSchema) TypeName() string {
42
	if s.Format != "" {
43
		return s.Format
44
	}
45
	return s.Type
46
}
47

48
// ItemsTypeName yields the type of items in a simple schema array
49
func (s *SimpleSchema) ItemsTypeName() string {
50
	if s.Items == nil {
51
		return ""
52
	}
53
	return 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
60
type Items struct {
61
	Refable
62
	CommonValidations
63
	SimpleSchema
64
	VendorExtensible
65
}
66

67
// NewItems creates a new instance of items
68
func NewItems() *Items {
69
	return &Items{}
70
}
71

72
// Typed a fluent builder method for the type of item
73
func (i *Items) Typed(tpe, format string) *Items {
74
	i.Type = tpe
75
	i.Format = format
76
	return i
77
}
78

79
// AsNullable flags this schema as nullable.
80
func (i *Items) AsNullable() *Items {
81
	i.Nullable = true
82
	return i
83
}
84

85
// CollectionOf a fluent builder method for an array item
86
func (i *Items) CollectionOf(items *Items, format string) *Items {
87
	i.Type = jsonArray
88
	i.Items = items
89
	i.CollectionFormat = format
90
	return i
91
}
92

93
// WithDefault sets the default value on this item
94
func (i *Items) WithDefault(defaultValue interface{}) *Items {
95
	i.Default = defaultValue
96
	return i
97
}
98

99
// WithMaxLength sets a max length value
100
func (i *Items) WithMaxLength(max int64) *Items {
101
	i.MaxLength = &max
102
	return i
103
}
104

105
// WithMinLength sets a min length value
106
func (i *Items) WithMinLength(min int64) *Items {
107
	i.MinLength = &min
108
	return i
109
}
110

111
// WithPattern sets a pattern value
112
func (i *Items) WithPattern(pattern string) *Items {
113
	i.Pattern = pattern
114
	return i
115
}
116

117
// WithMultipleOf sets a multiple of value
118
func (i *Items) WithMultipleOf(number float64) *Items {
119
	i.MultipleOf = &number
120
	return i
121
}
122

123
// WithMaximum sets a maximum number value
124
func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
125
	i.Maximum = &max
126
	i.ExclusiveMaximum = exclusive
127
	return i
128
}
129

130
// WithMinimum sets a minimum number value
131
func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
132
	i.Minimum = &min
133
	i.ExclusiveMinimum = exclusive
134
	return i
135
}
136

137
// WithEnum sets a the enum values (replace)
138
func (i *Items) WithEnum(values ...interface{}) *Items {
139
	i.Enum = append([]interface{}{}, values...)
140
	return i
141
}
142

143
// WithMaxItems sets the max items
144
func (i *Items) WithMaxItems(size int64) *Items {
145
	i.MaxItems = &size
146
	return i
147
}
148

149
// WithMinItems sets the min items
150
func (i *Items) WithMinItems(size int64) *Items {
151
	i.MinItems = &size
152
	return i
153
}
154

155
// UniqueValues dictates that this array can only have unique items
156
func (i *Items) UniqueValues() *Items {
157
	i.UniqueItems = true
158
	return i
159
}
160

161
// AllowDuplicates this array can have duplicates
162
func (i *Items) AllowDuplicates() *Items {
163
	i.UniqueItems = false
164
	return i
165
}
166

167
// WithValidations is a fluent method to set Items validations
168
func (i *Items) WithValidations(val CommonValidations) *Items {
169
	i.SetValidations(SchemaValidations{CommonValidations: val})
170
	return i
171
}
172

173
// UnmarshalJSON hydrates this items instance with the data from JSON
174
func (i *Items) UnmarshalJSON(data []byte) error {
175
	var validations CommonValidations
176
	if err := json.Unmarshal(data, &validations); err != nil {
177
		return err
178
	}
179
	var ref Refable
180
	if err := json.Unmarshal(data, &ref); err != nil {
181
		return err
182
	}
183
	var simpleSchema SimpleSchema
184
	if err := json.Unmarshal(data, &simpleSchema); err != nil {
185
		return err
186
	}
187
	var vendorExtensible VendorExtensible
188
	if err := json.Unmarshal(data, &vendorExtensible); err != nil {
189
		return err
190
	}
191
	i.Refable = ref
192
	i.CommonValidations = validations
193
	i.SimpleSchema = simpleSchema
194
	i.VendorExtensible = vendorExtensible
195
	return nil
196
}
197

198
// MarshalJSON converts this items object to JSON
199
func (i Items) MarshalJSON() ([]byte, error) {
200
	b1, err := json.Marshal(i.CommonValidations)
201
	if err != nil {
202
		return nil, err
203
	}
204
	b2, err := json.Marshal(i.SimpleSchema)
205
	if err != nil {
206
		return nil, err
207
	}
208
	b3, err := json.Marshal(i.Refable)
209
	if err != nil {
210
		return nil, err
211
	}
212
	b4, err := json.Marshal(i.VendorExtensible)
213
	if err != nil {
214
		return nil, err
215
	}
216
	return swag.ConcatJSON(b4, b3, b1, b2), nil
217
}
218

219
// JSONLookup look up a value by the json property name
220
func (i Items) JSONLookup(token string) (interface{}, error) {
221
	if token == jsonRef {
222
		return &i.Ref, nil
223
	}
224

225
	r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)
226
	if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
227
		return nil, err
228
	}
229
	if r != nil {
230
		return r, nil
231
	}
232
	r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)
233
	return r, err
234
}
235

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.