parameter.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package restful
  2. // Copyright 2013 Ernest Micklei. All rights reserved.
  3. // Use of this source code is governed by a license
  4. // that can be found in the LICENSE file.
  5. const (
  6. // PathParameterKind = indicator of Request parameter type "path"
  7. PathParameterKind = iota
  8. // QueryParameterKind = indicator of Request parameter type "query"
  9. QueryParameterKind
  10. // BodyParameterKind = indicator of Request parameter type "body"
  11. BodyParameterKind
  12. // HeaderParameterKind = indicator of Request parameter type "header"
  13. HeaderParameterKind
  14. // FormParameterKind = indicator of Request parameter type "form"
  15. FormParameterKind
  16. // CollectionFormatCSV comma separated values `foo,bar`
  17. CollectionFormatCSV = CollectionFormat("csv")
  18. // CollectionFormatSSV space separated values `foo bar`
  19. CollectionFormatSSV = CollectionFormat("ssv")
  20. // CollectionFormatTSV tab separated values `foo\tbar`
  21. CollectionFormatTSV = CollectionFormat("tsv")
  22. // CollectionFormatPipes pipe separated values `foo|bar`
  23. CollectionFormatPipes = CollectionFormat("pipes")
  24. // CollectionFormatMulti corresponds to multiple parameter instances instead of multiple values for a single
  25. // instance `foo=bar&foo=baz`. This is valid only for QueryParameters and FormParameters
  26. CollectionFormatMulti = CollectionFormat("multi")
  27. )
  28. type CollectionFormat string
  29. func (cf CollectionFormat) String() string {
  30. return string(cf)
  31. }
  32. // Parameter is for documententing the parameter used in a Http Request
  33. // ParameterData kinds are Path,Query and Body
  34. type Parameter struct {
  35. data *ParameterData
  36. }
  37. // ParameterData represents the state of a Parameter.
  38. // It is made public to make it accessible to e.g. the Swagger package.
  39. type ParameterData struct {
  40. Name, Description, DataType, DataFormat string
  41. Kind int
  42. Required bool
  43. AllowableValues map[string]string
  44. AllowMultiple bool
  45. DefaultValue string
  46. CollectionFormat string
  47. }
  48. // Data returns the state of the Parameter
  49. func (p *Parameter) Data() ParameterData {
  50. return *p.data
  51. }
  52. // Kind returns the parameter type indicator (see const for valid values)
  53. func (p *Parameter) Kind() int {
  54. return p.data.Kind
  55. }
  56. func (p *Parameter) bePath() *Parameter {
  57. p.data.Kind = PathParameterKind
  58. return p
  59. }
  60. func (p *Parameter) beQuery() *Parameter {
  61. p.data.Kind = QueryParameterKind
  62. return p
  63. }
  64. func (p *Parameter) beBody() *Parameter {
  65. p.data.Kind = BodyParameterKind
  66. return p
  67. }
  68. func (p *Parameter) beHeader() *Parameter {
  69. p.data.Kind = HeaderParameterKind
  70. return p
  71. }
  72. func (p *Parameter) beForm() *Parameter {
  73. p.data.Kind = FormParameterKind
  74. return p
  75. }
  76. // Required sets the required field and returns the receiver
  77. func (p *Parameter) Required(required bool) *Parameter {
  78. p.data.Required = required
  79. return p
  80. }
  81. // AllowMultiple sets the allowMultiple field and returns the receiver
  82. func (p *Parameter) AllowMultiple(multiple bool) *Parameter {
  83. p.data.AllowMultiple = multiple
  84. return p
  85. }
  86. // AllowableValues sets the allowableValues field and returns the receiver
  87. func (p *Parameter) AllowableValues(values map[string]string) *Parameter {
  88. p.data.AllowableValues = values
  89. return p
  90. }
  91. // DataType sets the dataType field and returns the receiver
  92. func (p *Parameter) DataType(typeName string) *Parameter {
  93. p.data.DataType = typeName
  94. return p
  95. }
  96. // DataFormat sets the dataFormat field for Swagger UI
  97. func (p *Parameter) DataFormat(formatName string) *Parameter {
  98. p.data.DataFormat = formatName
  99. return p
  100. }
  101. // DefaultValue sets the default value field and returns the receiver
  102. func (p *Parameter) DefaultValue(stringRepresentation string) *Parameter {
  103. p.data.DefaultValue = stringRepresentation
  104. return p
  105. }
  106. // Description sets the description value field and returns the receiver
  107. func (p *Parameter) Description(doc string) *Parameter {
  108. p.data.Description = doc
  109. return p
  110. }
  111. // CollectionFormat sets the collection format for an array type
  112. func (p *Parameter) CollectionFormat(format CollectionFormat) *Parameter {
  113. p.data.CollectionFormat = format.String()
  114. return p
  115. }