You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.7 KiB

  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. // MarshalJSON marshal this to JSON
  122. func (h Header) MarshalJSON() ([]byte, error) {
  123. b1, err := json.Marshal(h.CommonValidations)
  124. if err != nil {
  125. return nil, err
  126. }
  127. b2, err := json.Marshal(h.SimpleSchema)
  128. if err != nil {
  129. return nil, err
  130. }
  131. b3, err := json.Marshal(h.HeaderProps)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return swag.ConcatJSON(b1, b2, b3), nil
  136. }
  137. // UnmarshalJSON unmarshals this header from JSON
  138. func (h *Header) UnmarshalJSON(data []byte) error {
  139. if err := json.Unmarshal(data, &h.CommonValidations); err != nil {
  140. return err
  141. }
  142. if err := json.Unmarshal(data, &h.SimpleSchema); err != nil {
  143. return err
  144. }
  145. if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
  146. return err
  147. }
  148. return json.Unmarshal(data, &h.HeaderProps)
  149. }
  150. // JSONLookup look up a value by the json property name
  151. func (h Header) JSONLookup(token string) (interface{}, error) {
  152. if ex, ok := h.Extensions[token]; ok {
  153. return &ex, nil
  154. }
  155. r, _, err := jsonpointer.GetForToken(h.CommonValidations, token)
  156. if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
  157. return nil, err
  158. }
  159. if r != nil {
  160. return r, nil
  161. }
  162. r, _, err = jsonpointer.GetForToken(h.SimpleSchema, token)
  163. if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
  164. return nil, err
  165. }
  166. if r != nil {
  167. return r, nil
  168. }
  169. r, _, err = jsonpointer.GetForToken(h.HeaderProps, token)
  170. return r, err
  171. }