http urls monitor.

parameter.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // QueryParam creates a query parameter
  22. func QueryParam(name string) *Parameter {
  23. return &Parameter{ParamProps: ParamProps{Name: name, In: "query"}}
  24. }
  25. // HeaderParam creates a header parameter, this is always required by default
  26. func HeaderParam(name string) *Parameter {
  27. return &Parameter{ParamProps: ParamProps{Name: name, In: "header", Required: true}}
  28. }
  29. // PathParam creates a path parameter, this is always required
  30. func PathParam(name string) *Parameter {
  31. return &Parameter{ParamProps: ParamProps{Name: name, In: "path", Required: true}}
  32. }
  33. // BodyParam creates a body parameter
  34. func BodyParam(name string, schema *Schema) *Parameter {
  35. return &Parameter{ParamProps: ParamProps{Name: name, In: "body", Schema: schema}, SimpleSchema: SimpleSchema{Type: "object"}}
  36. }
  37. // FormDataParam creates a body parameter
  38. func FormDataParam(name string) *Parameter {
  39. return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}}
  40. }
  41. // FileParam creates a body parameter
  42. func FileParam(name string) *Parameter {
  43. return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}, SimpleSchema: SimpleSchema{Type: "file"}}
  44. }
  45. // SimpleArrayParam creates a param for a simple array (string, int, date etc)
  46. func SimpleArrayParam(name, tpe, fmt string) *Parameter {
  47. return &Parameter{ParamProps: ParamProps{Name: name}, SimpleSchema: SimpleSchema{Type: "array", CollectionFormat: "csv", Items: &Items{SimpleSchema: SimpleSchema{Type: "string", Format: fmt}}}}
  48. }
  49. // ParamRef creates a parameter that's a json reference
  50. func ParamRef(uri string) *Parameter {
  51. p := new(Parameter)
  52. p.Ref = MustCreateRef(uri)
  53. return p
  54. }
  55. // ParamProps describes the specific attributes of an operation parameter
  56. type ParamProps struct {
  57. Description string `json:"description,omitempty"`
  58. Name string `json:"name,omitempty"`
  59. In string `json:"in,omitempty"`
  60. Required bool `json:"required,omitempty"`
  61. Schema *Schema `json:"schema,omitempty"` // when in == "body"
  62. AllowEmptyValue bool `json:"allowEmptyValue,omitempty"` // when in == "query" || "formData"
  63. }
  64. // Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn).
  65. //
  66. // There are five possible parameter types.
  67. // * Path - Used together with [Path Templating](#pathTemplating), where the parameter value is actually part of the operation's URL. This does not include the host or base path of the API. For example, in `/items/{itemId}`, the path parameter is `itemId`.
  68. // * Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
  69. // * Header - Custom headers that are expected as part of the request.
  70. // * Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be *one* body parameter. The name of the body parameter has no effect on the parameter itself and is used for documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist together for the same operation.
  71. // * Form - Used to describe the payload of an HTTP request when either `application/x-www-form-urlencoded` or `multipart/form-data` are used as the content type of the request (in Swagger's definition, the [`consumes`](#operationConsumes) property of an operation). This is the only parameter type that can be used to send files, thus supporting the `file` type. Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the same operation. Form parameters have a different format based on the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4):
  72. // * `application/x-www-form-urlencoded` - Similar to the format of Query parameters but as a payload. For example, `foo=1&bar=swagger` - both `foo` and `bar` are form parameters. This is normally used for simple parameters that are being transferred.
  73. // * `multipart/form-data` - each parameter takes a section in the payload with an internal header. For example, for the header `Content-Disposition: form-data; name="submit-name"` the name of the parameter is `submit-name`. This type of form parameters is more commonly used for file transfers.
  74. //
  75. // For more information: http://goo.gl/8us55a#parameterObject
  76. type Parameter struct {
  77. Refable
  78. CommonValidations
  79. SimpleSchema
  80. VendorExtensible
  81. ParamProps
  82. }
  83. // JSONLookup look up a value by the json property name
  84. func (p Parameter) JSONLookup(token string) (interface{}, error) {
  85. if ex, ok := p.Extensions[token]; ok {
  86. return &ex, nil
  87. }
  88. if token == "$ref" {
  89. return &p.Ref, nil
  90. }
  91. r, _, err := jsonpointer.GetForToken(p.CommonValidations, token)
  92. if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
  93. return nil, err
  94. }
  95. if r != nil {
  96. return r, nil
  97. }
  98. r, _, err = jsonpointer.GetForToken(p.SimpleSchema, token)
  99. if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
  100. return nil, err
  101. }
  102. if r != nil {
  103. return r, nil
  104. }
  105. r, _, err = jsonpointer.GetForToken(p.ParamProps, token)
  106. return r, err
  107. }
  108. // WithDescription a fluent builder method for the description of the parameter
  109. func (p *Parameter) WithDescription(description string) *Parameter {
  110. p.Description = description
  111. return p
  112. }
  113. // Named a fluent builder method to override the name of the parameter
  114. func (p *Parameter) Named(name string) *Parameter {
  115. p.Name = name
  116. return p
  117. }
  118. // WithLocation a fluent builder method to override the location of the parameter
  119. func (p *Parameter) WithLocation(in string) *Parameter {
  120. p.In = in
  121. return p
  122. }
  123. // Typed a fluent builder method for the type of the parameter value
  124. func (p *Parameter) Typed(tpe, format string) *Parameter {
  125. p.Type = tpe
  126. p.Format = format
  127. return p
  128. }
  129. // CollectionOf a fluent builder method for an array parameter
  130. func (p *Parameter) CollectionOf(items *Items, format string) *Parameter {
  131. p.Type = "array"
  132. p.Items = items
  133. p.CollectionFormat = format
  134. return p
  135. }
  136. // WithDefault sets the default value on this parameter
  137. func (p *Parameter) WithDefault(defaultValue interface{}) *Parameter {
  138. p.AsOptional() // with default implies optional
  139. p.Default = defaultValue
  140. return p
  141. }
  142. // AllowsEmptyValues flags this parameter as being ok with empty values
  143. func (p *Parameter) AllowsEmptyValues() *Parameter {
  144. p.AllowEmptyValue = true
  145. return p
  146. }
  147. // NoEmptyValues flags this parameter as not liking empty values
  148. func (p *Parameter) NoEmptyValues() *Parameter {
  149. p.AllowEmptyValue = false
  150. return p
  151. }
  152. // AsOptional flags this parameter as optional
  153. func (p *Parameter) AsOptional() *Parameter {
  154. p.Required = false
  155. return p
  156. }
  157. // AsRequired flags this parameter as required
  158. func (p *Parameter) AsRequired() *Parameter {
  159. if p.Default != nil { // with a default required makes no sense
  160. return p
  161. }
  162. p.Required = true
  163. return p
  164. }
  165. // WithMaxLength sets a max length value
  166. func (p *Parameter) WithMaxLength(max int64) *Parameter {
  167. p.MaxLength = &max
  168. return p
  169. }
  170. // WithMinLength sets a min length value
  171. func (p *Parameter) WithMinLength(min int64) *Parameter {
  172. p.MinLength = &min
  173. return p
  174. }
  175. // WithPattern sets a pattern value
  176. func (p *Parameter) WithPattern(pattern string) *Parameter {
  177. p.Pattern = pattern
  178. return p
  179. }
  180. // WithMultipleOf sets a multiple of value
  181. func (p *Parameter) WithMultipleOf(number float64) *Parameter {
  182. p.MultipleOf = &number
  183. return p
  184. }
  185. // WithMaximum sets a maximum number value
  186. func (p *Parameter) WithMaximum(max float64, exclusive bool) *Parameter {
  187. p.Maximum = &max
  188. p.ExclusiveMaximum = exclusive
  189. return p
  190. }
  191. // WithMinimum sets a minimum number value
  192. func (p *Parameter) WithMinimum(min float64, exclusive bool) *Parameter {
  193. p.Minimum = &min
  194. p.ExclusiveMinimum = exclusive
  195. return p
  196. }
  197. // WithEnum sets a the enum values (replace)
  198. func (p *Parameter) WithEnum(values ...interface{}) *Parameter {
  199. p.Enum = append([]interface{}{}, values...)
  200. return p
  201. }
  202. // WithMaxItems sets the max items
  203. func (p *Parameter) WithMaxItems(size int64) *Parameter {
  204. p.MaxItems = &size
  205. return p
  206. }
  207. // WithMinItems sets the min items
  208. func (p *Parameter) WithMinItems(size int64) *Parameter {
  209. p.MinItems = &size
  210. return p
  211. }
  212. // UniqueValues dictates that this array can only have unique items
  213. func (p *Parameter) UniqueValues() *Parameter {
  214. p.UniqueItems = true
  215. return p
  216. }
  217. // AllowDuplicates this array can have duplicates
  218. func (p *Parameter) AllowDuplicates() *Parameter {
  219. p.UniqueItems = false
  220. return p
  221. }
  222. // UnmarshalJSON hydrates this items instance with the data from JSON
  223. func (p *Parameter) UnmarshalJSON(data []byte) error {
  224. if err := json.Unmarshal(data, &p.CommonValidations); err != nil {
  225. return err
  226. }
  227. if err := json.Unmarshal(data, &p.Refable); err != nil {
  228. return err
  229. }
  230. if err := json.Unmarshal(data, &p.SimpleSchema); err != nil {
  231. return err
  232. }
  233. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  234. return err
  235. }
  236. if err := json.Unmarshal(data, &p.ParamProps); err != nil {
  237. return err
  238. }
  239. return nil
  240. }
  241. // MarshalJSON converts this items object to JSON
  242. func (p Parameter) MarshalJSON() ([]byte, error) {
  243. b1, err := json.Marshal(p.CommonValidations)
  244. if err != nil {
  245. return nil, err
  246. }
  247. b2, err := json.Marshal(p.SimpleSchema)
  248. if err != nil {
  249. return nil, err
  250. }
  251. b3, err := json.Marshal(p.Refable)
  252. if err != nil {
  253. return nil, err
  254. }
  255. b4, err := json.Marshal(p.VendorExtensible)
  256. if err != nil {
  257. return nil, err
  258. }
  259. b5, err := json.Marshal(p.ParamProps)
  260. if err != nil {
  261. return nil, err
  262. }
  263. return swag.ConcatJSON(b3, b1, b2, b4, b5), nil
  264. }