items.go 6.2 KB

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