本站源代码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

245 rindas
6.4KB

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