Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [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 | type 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 |
| 32 | type Header struct { |
| 33 | CommonValidations |
| 34 | SimpleSchema |
| 35 | VendorExtensible |
| 36 | HeaderProps |
| 37 | } |
| 38 | |
| 39 | // ResponseHeader creates a new header instance for use in a response |
| 40 | func ResponseHeader() *Header { |
| 41 | return new(Header) |
| 42 | } |
| 43 | |
| 44 | // WithDescription sets the description on this response, allows for chaining |
| 45 | func (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 |
| 51 | func (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 |
| 58 | func (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 |
| 66 | func (h *Header) WithDefault(defaultValue interface{}) *Header { |
| 67 | h.Default = defaultValue |
| 68 | return h |
| 69 | } |
| 70 | |
| 71 | // WithMaxLength sets a max length value |
| 72 | func (h *Header) WithMaxLength(max int64) *Header { |
| 73 | h.MaxLength = &max |
| 74 | return h |
| 75 | } |
| 76 | |
| 77 | // WithMinLength sets a min length value |
| 78 | func (h *Header) WithMinLength(min int64) *Header { |
| 79 | h.MinLength = &min |
| 80 | return h |
| 81 | } |
| 82 | |
| 83 | // WithPattern sets a pattern value |
| 84 | func (h *Header) WithPattern(pattern string) *Header { |
| 85 | h.Pattern = pattern |
| 86 | return h |
| 87 | } |
| 88 | |
| 89 | // WithMultipleOf sets a multiple of value |
| 90 | func (h *Header) WithMultipleOf(number float64) *Header { |
| 91 | h.MultipleOf = &number |
| 92 | return h |
| 93 | } |
| 94 | |
| 95 | // WithMaximum sets a maximum number value |
| 96 | func (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 |
| 103 | func (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) |
| 110 | func (h *Header) WithEnum(values ...interface{}) *Header { |
| 111 | h.Enum = append([]interface{}{}, values...) |
| 112 | return h |
| 113 | } |
| 114 | |
| 115 | // WithMaxItems sets the max items |
| 116 | func (h *Header) WithMaxItems(size int64) *Header { |
| 117 | h.MaxItems = &size |
| 118 | return h |
| 119 | } |
| 120 | |
| 121 | // WithMinItems sets the min items |
| 122 | func (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 |
| 128 | func (h *Header) UniqueValues() *Header { |
| 129 | h.UniqueItems = true |
| 130 | return h |
| 131 | } |
| 132 | |
| 133 | // AllowDuplicates this array can have duplicates |
| 134 | func (h *Header) AllowDuplicates() *Header { |
| 135 | h.UniqueItems = false |
| 136 | return h |
| 137 | } |
| 138 | |
| 139 | // MarshalJSON marshal this to JSON |
| 140 | func (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 |
| 157 | func (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 |
| 174 | func (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 | } |