items.go 5.1 KB

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