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