items.go 5.8 KB

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