http urls monitor.

header.go 4.7KB

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