header.go 4.9 KB

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