parameter.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package restful
  2. import "sort"
  3. // Copyright 2013 Ernest Micklei. All rights reserved.
  4. // Use of this source code is governed by a license
  5. // that can be found in the LICENSE file.
  6. const (
  7. // PathParameterKind = indicator of Request parameter type "path"
  8. PathParameterKind = iota
  9. // QueryParameterKind = indicator of Request parameter type "query"
  10. QueryParameterKind
  11. // BodyParameterKind = indicator of Request parameter type "body"
  12. BodyParameterKind
  13. // HeaderParameterKind = indicator of Request parameter type "header"
  14. HeaderParameterKind
  15. // FormParameterKind = indicator of Request parameter type "form"
  16. FormParameterKind
  17. // MultiPartFormParameterKind = indicator of Request parameter type "multipart/form-data"
  18. MultiPartFormParameterKind
  19. // CollectionFormatCSV comma separated values `foo,bar`
  20. CollectionFormatCSV = CollectionFormat("csv")
  21. // CollectionFormatSSV space separated values `foo bar`
  22. CollectionFormatSSV = CollectionFormat("ssv")
  23. // CollectionFormatTSV tab separated values `foo\tbar`
  24. CollectionFormatTSV = CollectionFormat("tsv")
  25. // CollectionFormatPipes pipe separated values `foo|bar`
  26. CollectionFormatPipes = CollectionFormat("pipes")
  27. // CollectionFormatMulti corresponds to multiple parameter instances instead of multiple values for a single
  28. // instance `foo=bar&foo=baz`. This is valid only for QueryParameters and FormParameters
  29. CollectionFormatMulti = CollectionFormat("multi")
  30. )
  31. type CollectionFormat string
  32. func (cf CollectionFormat) String() string {
  33. return string(cf)
  34. }
  35. // Parameter is for documententing the parameter used in a Http Request
  36. // ParameterData kinds are Path,Query and Body
  37. type Parameter struct {
  38. data *ParameterData
  39. }
  40. // ParameterData represents the state of a Parameter.
  41. // It is made public to make it accessible to e.g. the Swagger package.
  42. type ParameterData struct {
  43. ExtensionProperties
  44. Name, Description, DataType, DataFormat string
  45. Kind int
  46. Required bool
  47. // AllowableValues is deprecated. Use PossibleValues instead
  48. AllowableValues map[string]string
  49. PossibleValues []string
  50. AllowMultiple bool
  51. AllowEmptyValue bool
  52. DefaultValue string
  53. CollectionFormat string
  54. Pattern string
  55. Minimum *float64
  56. Maximum *float64
  57. MinLength *int64
  58. MaxLength *int64
  59. MinItems *int64
  60. MaxItems *int64
  61. UniqueItems bool
  62. }
  63. // Data returns the state of the Parameter
  64. func (p *Parameter) Data() ParameterData {
  65. return *p.data
  66. }
  67. // Kind returns the parameter type indicator (see const for valid values)
  68. func (p *Parameter) Kind() int {
  69. return p.data.Kind
  70. }
  71. func (p *Parameter) bePath() *Parameter {
  72. p.data.Kind = PathParameterKind
  73. return p
  74. }
  75. func (p *Parameter) beQuery() *Parameter {
  76. p.data.Kind = QueryParameterKind
  77. return p
  78. }
  79. func (p *Parameter) beBody() *Parameter {
  80. p.data.Kind = BodyParameterKind
  81. return p
  82. }
  83. func (p *Parameter) beHeader() *Parameter {
  84. p.data.Kind = HeaderParameterKind
  85. return p
  86. }
  87. func (p *Parameter) beForm() *Parameter {
  88. p.data.Kind = FormParameterKind
  89. return p
  90. }
  91. func (p *Parameter) beMultiPartForm() *Parameter {
  92. p.data.Kind = MultiPartFormParameterKind
  93. return p
  94. }
  95. // Required sets the required field and returns the receiver
  96. func (p *Parameter) Required(required bool) *Parameter {
  97. p.data.Required = required
  98. return p
  99. }
  100. // AllowMultiple sets the allowMultiple field and returns the receiver
  101. func (p *Parameter) AllowMultiple(multiple bool) *Parameter {
  102. p.data.AllowMultiple = multiple
  103. return p
  104. }
  105. // AddExtension adds or updates a key=value pair to the extension map
  106. func (p *Parameter) AddExtension(key string, value interface{}) *Parameter {
  107. p.data.AddExtension(key, value)
  108. return p
  109. }
  110. // AllowEmptyValue sets the AllowEmptyValue field and returns the receiver
  111. func (p *Parameter) AllowEmptyValue(multiple bool) *Parameter {
  112. p.data.AllowEmptyValue = multiple
  113. return p
  114. }
  115. // AllowableValues is deprecated. Use PossibleValues instead. Both will be set.
  116. func (p *Parameter) AllowableValues(values map[string]string) *Parameter {
  117. p.data.AllowableValues = values
  118. allowableSortedKeys := make([]string, 0, len(values))
  119. for k := range values {
  120. allowableSortedKeys = append(allowableSortedKeys, k)
  121. }
  122. sort.Strings(allowableSortedKeys)
  123. p.data.PossibleValues = make([]string, 0, len(values))
  124. for _, k := range allowableSortedKeys {
  125. p.data.PossibleValues = append(p.data.PossibleValues, values[k])
  126. }
  127. return p
  128. }
  129. // PossibleValues sets the possible values field and returns the receiver
  130. func (p *Parameter) PossibleValues(values []string) *Parameter {
  131. p.data.PossibleValues = values
  132. return p
  133. }
  134. // DataType sets the dataType field and returns the receiver
  135. func (p *Parameter) DataType(typeName string) *Parameter {
  136. p.data.DataType = typeName
  137. return p
  138. }
  139. // DataFormat sets the dataFormat field for Swagger UI
  140. func (p *Parameter) DataFormat(formatName string) *Parameter {
  141. p.data.DataFormat = formatName
  142. return p
  143. }
  144. // DefaultValue sets the default value field and returns the receiver
  145. func (p *Parameter) DefaultValue(stringRepresentation string) *Parameter {
  146. p.data.DefaultValue = stringRepresentation
  147. return p
  148. }
  149. // Description sets the description value field and returns the receiver
  150. func (p *Parameter) Description(doc string) *Parameter {
  151. p.data.Description = doc
  152. return p
  153. }
  154. // CollectionFormat sets the collection format for an array type
  155. func (p *Parameter) CollectionFormat(format CollectionFormat) *Parameter {
  156. p.data.CollectionFormat = format.String()
  157. return p
  158. }
  159. // Pattern sets the pattern field and returns the receiver
  160. func (p *Parameter) Pattern(pattern string) *Parameter {
  161. p.data.Pattern = pattern
  162. return p
  163. }
  164. // Minimum sets the minimum field and returns the receiver
  165. func (p *Parameter) Minimum(minimum float64) *Parameter {
  166. p.data.Minimum = &minimum
  167. return p
  168. }
  169. // Maximum sets the maximum field and returns the receiver
  170. func (p *Parameter) Maximum(maximum float64) *Parameter {
  171. p.data.Maximum = &maximum
  172. return p
  173. }
  174. // MinLength sets the minLength field and returns the receiver
  175. func (p *Parameter) MinLength(minLength int64) *Parameter {
  176. p.data.MinLength = &minLength
  177. return p
  178. }
  179. // MaxLength sets the maxLength field and returns the receiver
  180. func (p *Parameter) MaxLength(maxLength int64) *Parameter {
  181. p.data.MaxLength = &maxLength
  182. return p
  183. }
  184. // MinItems sets the minItems field and returns the receiver
  185. func (p *Parameter) MinItems(minItems int64) *Parameter {
  186. p.data.MinItems = &minItems
  187. return p
  188. }
  189. // MaxItems sets the maxItems field and returns the receiver
  190. func (p *Parameter) MaxItems(maxItems int64) *Parameter {
  191. p.data.MaxItems = &maxItems
  192. return p
  193. }
  194. // UniqueItems sets the uniqueItems field and returns the receiver
  195. func (p *Parameter) UniqueItems(uniqueItems bool) *Parameter {
  196. p.data.UniqueItems = uniqueItems
  197. return p
  198. }