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