podman
203 строки · 4.9 Кб
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 (
26jsonArray = "array"
27)
28
29// HeaderProps describes a response header
30type HeaderProps struct {
31Description string `json:"description,omitempty"`
32}
33
34// Header describes a header for a response of the API
35//
36// For more information: http://goo.gl/8us55a#headerObject
37type Header struct {
38CommonValidations
39SimpleSchema
40VendorExtensible
41HeaderProps
42}
43
44// ResponseHeader creates a new header instance for use in a response
45func ResponseHeader() *Header {
46return new(Header)
47}
48
49// WithDescription sets the description on this response, allows for chaining
50func (h *Header) WithDescription(description string) *Header {
51h.Description = description
52return h
53}
54
55// Typed a fluent builder method for the type of parameter
56func (h *Header) Typed(tpe, format string) *Header {
57h.Type = tpe
58h.Format = format
59return h
60}
61
62// CollectionOf a fluent builder method for an array item
63func (h *Header) CollectionOf(items *Items, format string) *Header {
64h.Type = jsonArray
65h.Items = items
66h.CollectionFormat = format
67return h
68}
69
70// WithDefault sets the default value on this item
71func (h *Header) WithDefault(defaultValue interface{}) *Header {
72h.Default = defaultValue
73return h
74}
75
76// WithMaxLength sets a max length value
77func (h *Header) WithMaxLength(max int64) *Header {
78h.MaxLength = &max
79return h
80}
81
82// WithMinLength sets a min length value
83func (h *Header) WithMinLength(min int64) *Header {
84h.MinLength = &min
85return h
86}
87
88// WithPattern sets a pattern value
89func (h *Header) WithPattern(pattern string) *Header {
90h.Pattern = pattern
91return h
92}
93
94// WithMultipleOf sets a multiple of value
95func (h *Header) WithMultipleOf(number float64) *Header {
96h.MultipleOf = &number
97return h
98}
99
100// WithMaximum sets a maximum number value
101func (h *Header) WithMaximum(max float64, exclusive bool) *Header {
102h.Maximum = &max
103h.ExclusiveMaximum = exclusive
104return h
105}
106
107// WithMinimum sets a minimum number value
108func (h *Header) WithMinimum(min float64, exclusive bool) *Header {
109h.Minimum = &min
110h.ExclusiveMinimum = exclusive
111return h
112}
113
114// WithEnum sets a the enum values (replace)
115func (h *Header) WithEnum(values ...interface{}) *Header {
116h.Enum = append([]interface{}{}, values...)
117return h
118}
119
120// WithMaxItems sets the max items
121func (h *Header) WithMaxItems(size int64) *Header {
122h.MaxItems = &size
123return h
124}
125
126// WithMinItems sets the min items
127func (h *Header) WithMinItems(size int64) *Header {
128h.MinItems = &size
129return h
130}
131
132// UniqueValues dictates that this array can only have unique items
133func (h *Header) UniqueValues() *Header {
134h.UniqueItems = true
135return h
136}
137
138// AllowDuplicates this array can have duplicates
139func (h *Header) AllowDuplicates() *Header {
140h.UniqueItems = false
141return h
142}
143
144// WithValidations is a fluent method to set header validations
145func (h *Header) WithValidations(val CommonValidations) *Header {
146h.SetValidations(SchemaValidations{CommonValidations: val})
147return h
148}
149
150// MarshalJSON marshal this to JSON
151func (h Header) MarshalJSON() ([]byte, error) {
152b1, err := json.Marshal(h.CommonValidations)
153if err != nil {
154return nil, err
155}
156b2, err := json.Marshal(h.SimpleSchema)
157if err != nil {
158return nil, err
159}
160b3, err := json.Marshal(h.HeaderProps)
161if err != nil {
162return nil, err
163}
164return swag.ConcatJSON(b1, b2, b3), nil
165}
166
167// UnmarshalJSON unmarshals this header from JSON
168func (h *Header) UnmarshalJSON(data []byte) error {
169if err := json.Unmarshal(data, &h.CommonValidations); err != nil {
170return err
171}
172if err := json.Unmarshal(data, &h.SimpleSchema); err != nil {
173return err
174}
175if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
176return err
177}
178return json.Unmarshal(data, &h.HeaderProps)
179}
180
181// JSONLookup look up a value by the json property name
182func (h Header) JSONLookup(token string) (interface{}, error) {
183if ex, ok := h.Extensions[token]; ok {
184return &ex, nil
185}
186
187r, _, err := jsonpointer.GetForToken(h.CommonValidations, token)
188if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
189return nil, err
190}
191if r != nil {
192return r, nil
193}
194r, _, err = jsonpointer.GetForToken(h.SimpleSchema, token)
195if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
196return nil, err
197}
198if r != nil {
199return r, nil
200}
201r, _, err = jsonpointer.GetForToken(h.HeaderProps, token)
202return r, err
203}
204