1 // Copyright 2015 go-swagger maintainers
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "github.com/go-openapi/jsonpointer"
22 "github.com/go-openapi/swag"
29 // HeaderProps describes a response header
30 type HeaderProps struct {
31 Description string `json:"description,omitempty"`
34 // Header describes a header for a response of the API
36 // For more information: http://goo.gl/8us55a#headerObject
44 // ResponseHeader creates a new header instance for use in a response
45 func ResponseHeader() *Header {
49 // WithDescription sets the description on this response, allows for chaining
50 func (h *Header) WithDescription(description string) *Header {
51 h.Description = description
55 // Typed a fluent builder method for the type of parameter
56 func (h *Header) Typed(tpe, format string) *Header {
62 // CollectionOf a fluent builder method for an array item
63 func (h *Header) CollectionOf(items *Items, format string) *Header {
66 h.CollectionFormat = format
70 // WithDefault sets the default value on this item
71 func (h *Header) WithDefault(defaultValue interface{}) *Header {
72 h.Default = defaultValue
76 // WithMaxLength sets a max length value
77 func (h *Header) WithMaxLength(max int64) *Header {
82 // WithMinLength sets a min length value
83 func (h *Header) WithMinLength(min int64) *Header {
88 // WithPattern sets a pattern value
89 func (h *Header) WithPattern(pattern string) *Header {
94 // WithMultipleOf sets a multiple of value
95 func (h *Header) WithMultipleOf(number float64) *Header {
96 h.MultipleOf = &number
100 // WithMaximum sets a maximum number value
101 func (h *Header) WithMaximum(max float64, exclusive bool) *Header {
103 h.ExclusiveMaximum = exclusive
107 // WithMinimum sets a minimum number value
108 func (h *Header) WithMinimum(min float64, exclusive bool) *Header {
110 h.ExclusiveMinimum = exclusive
114 // WithEnum sets a the enum values (replace)
115 func (h *Header) WithEnum(values ...interface{}) *Header {
116 h.Enum = append([]interface{}{}, values...)
120 // WithMaxItems sets the max items
121 func (h *Header) WithMaxItems(size int64) *Header {
126 // WithMinItems sets the min items
127 func (h *Header) WithMinItems(size int64) *Header {
132 // UniqueValues dictates that this array can only have unique items
133 func (h *Header) UniqueValues() *Header {
138 // AllowDuplicates this array can have duplicates
139 func (h *Header) AllowDuplicates() *Header {
140 h.UniqueItems = false
144 // MarshalJSON marshal this to JSON
145 func (h Header) MarshalJSON() ([]byte, error) {
146 b1, err := json.Marshal(h.CommonValidations)
150 b2, err := json.Marshal(h.SimpleSchema)
154 b3, err := json.Marshal(h.HeaderProps)
158 return swag.ConcatJSON(b1, b2, b3), nil
161 // UnmarshalJSON unmarshals this header from JSON
162 func (h *Header) UnmarshalJSON(data []byte) error {
163 if err := json.Unmarshal(data, &h.CommonValidations); err != nil {
166 if err := json.Unmarshal(data, &h.SimpleSchema); err != nil {
169 if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
172 return json.Unmarshal(data, &h.HeaderProps)
175 // JSONLookup look up a value by the json property name
176 func (h Header) JSONLookup(token string) (interface{}, error) {
177 if ex, ok := h.Extensions[token]; ok {
181 r, _, err := jsonpointer.GetForToken(h.CommonValidations, token)
182 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
188 r, _, err = jsonpointer.GetForToken(h.SimpleSchema, token)
189 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
195 r, _, err = jsonpointer.GetForToken(h.HeaderProps, token)