http urls monitor.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // SimpleSchema describe swagger simple schemas for parameters and headers
  22. type SimpleSchema struct {
  23. Type string `json:"type,omitempty"`
  24. Format string `json:"format,omitempty"`
  25. Items *Items `json:"items,omitempty"`
  26. CollectionFormat string `json:"collectionFormat,omitempty"`
  27. Default interface{} `json:"default,omitempty"`
  28. Example interface{} `json:"example,omitempty"`
  29. }
  30. // TypeName return the type (or format) of a simple schema
  31. func (s *SimpleSchema) TypeName() string {
  32. if s.Format != "" {
  33. return s.Format
  34. }
  35. return s.Type
  36. }
  37. // ItemsTypeName yields the type of items in a simple schema array
  38. func (s *SimpleSchema) ItemsTypeName() string {
  39. if s.Items == nil {
  40. return ""
  41. }
  42. return s.Items.TypeName()
  43. }
  44. // CommonValidations describe common JSON-schema validations
  45. type CommonValidations struct {
  46. Maximum *float64 `json:"maximum,omitempty"`
  47. ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
  48. Minimum *float64 `json:"minimum,omitempty"`
  49. ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
  50. MaxLength *int64 `json:"maxLength,omitempty"`
  51. MinLength *int64 `json:"minLength,omitempty"`
  52. Pattern string `json:"pattern,omitempty"`
  53. MaxItems *int64 `json:"maxItems,omitempty"`
  54. MinItems *int64 `json:"minItems,omitempty"`
  55. UniqueItems bool `json:"uniqueItems,omitempty"`
  56. MultipleOf *float64 `json:"multipleOf,omitempty"`
  57. Enum []interface{} `json:"enum,omitempty"`
  58. }
  59. // Items a limited subset of JSON-Schema's items object.
  60. // It is used by parameter definitions that are not located in "body".
  61. //
  62. // For more information: http://goo.gl/8us55a#items-object
  63. type Items struct {
  64. Refable
  65. CommonValidations
  66. SimpleSchema
  67. VendorExtensible
  68. }
  69. // NewItems creates a new instance of items
  70. func NewItems() *Items {
  71. return &Items{}
  72. }
  73. // Typed a fluent builder method for the type of item
  74. func (i *Items) Typed(tpe, format string) *Items {
  75. i.Type = tpe
  76. i.Format = format
  77. return i
  78. }
  79. // CollectionOf a fluent builder method for an array item
  80. func (i *Items) CollectionOf(items *Items, format string) *Items {
  81. i.Type = "array"
  82. i.Items = items
  83. i.CollectionFormat = format
  84. return i
  85. }
  86. // WithDefault sets the default value on this item
  87. func (i *Items) WithDefault(defaultValue interface{}) *Items {
  88. i.Default = defaultValue
  89. return i
  90. }
  91. // WithMaxLength sets a max length value
  92. func (i *Items) WithMaxLength(max int64) *Items {
  93. i.MaxLength = &max
  94. return i
  95. }
  96. // WithMinLength sets a min length value
  97. func (i *Items) WithMinLength(min int64) *Items {
  98. i.MinLength = &min
  99. return i
  100. }
  101. // WithPattern sets a pattern value
  102. func (i *Items) WithPattern(pattern string) *Items {
  103. i.Pattern = pattern
  104. return i
  105. }
  106. // WithMultipleOf sets a multiple of value
  107. func (i *Items) WithMultipleOf(number float64) *Items {
  108. i.MultipleOf = &number
  109. return i
  110. }
  111. // WithMaximum sets a maximum number value
  112. func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
  113. i.Maximum = &max
  114. i.ExclusiveMaximum = exclusive
  115. return i
  116. }
  117. // WithMinimum sets a minimum number value
  118. func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
  119. i.Minimum = &min
  120. i.ExclusiveMinimum = exclusive
  121. return i
  122. }
  123. // WithEnum sets a the enum values (replace)
  124. func (i *Items) WithEnum(values ...interface{}) *Items {
  125. i.Enum = append([]interface{}{}, values...)
  126. return i
  127. }
  128. // WithMaxItems sets the max items
  129. func (i *Items) WithMaxItems(size int64) *Items {
  130. i.MaxItems = &size
  131. return i
  132. }
  133. // WithMinItems sets the min items
  134. func (i *Items) WithMinItems(size int64) *Items {
  135. i.MinItems = &size
  136. return i
  137. }
  138. // UniqueValues dictates that this array can only have unique items
  139. func (i *Items) UniqueValues() *Items {
  140. i.UniqueItems = true
  141. return i
  142. }
  143. // AllowDuplicates this array can have duplicates
  144. func (i *Items) AllowDuplicates() *Items {
  145. i.UniqueItems = false
  146. return i
  147. }
  148. // UnmarshalJSON hydrates this items instance with the data from JSON
  149. func (i *Items) UnmarshalJSON(data []byte) error {
  150. var validations CommonValidations
  151. if err := json.Unmarshal(data, &validations); err != nil {
  152. return err
  153. }
  154. var ref Refable
  155. if err := json.Unmarshal(data, &ref); err != nil {
  156. return err
  157. }
  158. var simpleSchema SimpleSchema
  159. if err := json.Unmarshal(data, &simpleSchema); err != nil {
  160. return err
  161. }
  162. var vendorExtensible VendorExtensible
  163. if err := json.Unmarshal(data, &vendorExtensible); err != nil {
  164. return err
  165. }
  166. i.Refable = ref
  167. i.CommonValidations = validations
  168. i.SimpleSchema = simpleSchema
  169. i.VendorExtensible = vendorExtensible
  170. return nil
  171. }
  172. // MarshalJSON converts this items object to JSON
  173. func (i Items) MarshalJSON() ([]byte, error) {
  174. b1, err := json.Marshal(i.CommonValidations)
  175. if err != nil {
  176. return nil, err
  177. }
  178. b2, err := json.Marshal(i.SimpleSchema)
  179. if err != nil {
  180. return nil, err
  181. }
  182. b3, err := json.Marshal(i.Refable)
  183. if err != nil {
  184. return nil, err
  185. }
  186. b4, err := json.Marshal(i.VendorExtensible)
  187. if err != nil {
  188. return nil, err
  189. }
  190. return swag.ConcatJSON(b4, b3, b1, b2), nil
  191. }
  192. // JSONLookup look up a value by the json property name
  193. func (i Items) JSONLookup(token string) (interface{}, error) {
  194. if token == "$ref" {
  195. return &i.Ref, nil
  196. }
  197. r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)
  198. if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
  199. return nil, err
  200. }
  201. if r != nil {
  202. return r, nil
  203. }
  204. r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)
  205. return r, err
  206. }