openapi.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package proto
  14. import (
  15. "fmt"
  16. "sort"
  17. "strings"
  18. )
  19. // Defines openapi types.
  20. const (
  21. Integer = "integer"
  22. Number = "number"
  23. String = "string"
  24. Boolean = "boolean"
  25. // These types are private as they should never leak, and are
  26. // represented by actual structs.
  27. array = "array"
  28. object = "object"
  29. )
  30. // Models interface describe a model provider. They can give you the
  31. // schema for a specific model.
  32. type Models interface {
  33. LookupModel(string) Schema
  34. ListModels() []string
  35. }
  36. // SchemaVisitor is an interface that you need to implement if you want
  37. // to "visit" an openapi schema. A dispatch on the Schema type will call
  38. // the appropriate function based on its actual type:
  39. // - Array is a list of one and only one given subtype
  40. // - Map is a map of string to one and only one given subtype
  41. // - Primitive can be string, integer, number and boolean.
  42. // - Kind is an object with specific fields mapping to specific types.
  43. // - Reference is a link to another definition.
  44. type SchemaVisitor interface {
  45. VisitArray(*Array)
  46. VisitMap(*Map)
  47. VisitPrimitive(*Primitive)
  48. VisitKind(*Kind)
  49. VisitReference(Reference)
  50. }
  51. // SchemaVisitorArbitrary is an additional visitor interface which handles
  52. // arbitrary types. For backwards compatibility, it's a separate interface
  53. // which is checked for at runtime.
  54. type SchemaVisitorArbitrary interface {
  55. SchemaVisitor
  56. VisitArbitrary(*Arbitrary)
  57. }
  58. // Schema is the base definition of an openapi type.
  59. type Schema interface {
  60. // Giving a visitor here will let you visit the actual type.
  61. Accept(SchemaVisitor)
  62. // Pretty print the name of the type.
  63. GetName() string
  64. // Describes how to access this field.
  65. GetPath() *Path
  66. // Describes the field.
  67. GetDescription() string
  68. // Default for that schema.
  69. GetDefault() interface{}
  70. // Returns type extensions.
  71. GetExtensions() map[string]interface{}
  72. }
  73. // Path helps us keep track of type paths
  74. type Path struct {
  75. parent *Path
  76. key string
  77. }
  78. func NewPath(key string) Path {
  79. return Path{key: key}
  80. }
  81. func (p *Path) Get() []string {
  82. if p == nil {
  83. return []string{}
  84. }
  85. if p.key == "" {
  86. return p.parent.Get()
  87. }
  88. return append(p.parent.Get(), p.key)
  89. }
  90. func (p *Path) Len() int {
  91. return len(p.Get())
  92. }
  93. func (p *Path) String() string {
  94. return strings.Join(p.Get(), "")
  95. }
  96. // ArrayPath appends an array index and creates a new path
  97. func (p *Path) ArrayPath(i int) Path {
  98. return Path{
  99. parent: p,
  100. key: fmt.Sprintf("[%d]", i),
  101. }
  102. }
  103. // FieldPath appends a field name and creates a new path
  104. func (p *Path) FieldPath(field string) Path {
  105. return Path{
  106. parent: p,
  107. key: fmt.Sprintf(".%s", field),
  108. }
  109. }
  110. // BaseSchema holds data used by each types of schema.
  111. type BaseSchema struct {
  112. Description string
  113. Extensions map[string]interface{}
  114. Default interface{}
  115. Path Path
  116. }
  117. func (b *BaseSchema) GetDescription() string {
  118. return b.Description
  119. }
  120. func (b *BaseSchema) GetExtensions() map[string]interface{} {
  121. return b.Extensions
  122. }
  123. func (b *BaseSchema) GetDefault() interface{} {
  124. return b.Default
  125. }
  126. func (b *BaseSchema) GetPath() *Path {
  127. return &b.Path
  128. }
  129. // Array must have all its element of the same `SubType`.
  130. type Array struct {
  131. BaseSchema
  132. SubType Schema
  133. }
  134. var _ Schema = &Array{}
  135. func (a *Array) Accept(v SchemaVisitor) {
  136. v.VisitArray(a)
  137. }
  138. func (a *Array) GetName() string {
  139. return fmt.Sprintf("Array of %s", a.SubType.GetName())
  140. }
  141. // Kind is a complex object. It can have multiple different
  142. // subtypes for each field, as defined in the `Fields` field. Mandatory
  143. // fields are listed in `RequiredFields`. The key of the object is
  144. // always of type `string`.
  145. type Kind struct {
  146. BaseSchema
  147. // Lists names of required fields.
  148. RequiredFields []string
  149. // Maps field names to types.
  150. Fields map[string]Schema
  151. // FieldOrder reports the canonical order for the fields.
  152. FieldOrder []string
  153. }
  154. var _ Schema = &Kind{}
  155. func (k *Kind) Accept(v SchemaVisitor) {
  156. v.VisitKind(k)
  157. }
  158. func (k *Kind) GetName() string {
  159. properties := []string{}
  160. for key := range k.Fields {
  161. properties = append(properties, key)
  162. }
  163. return fmt.Sprintf("Kind(%v)", properties)
  164. }
  165. // IsRequired returns true if `field` is a required field for this type.
  166. func (k *Kind) IsRequired(field string) bool {
  167. for _, f := range k.RequiredFields {
  168. if f == field {
  169. return true
  170. }
  171. }
  172. return false
  173. }
  174. // Keys returns a alphabetically sorted list of keys.
  175. func (k *Kind) Keys() []string {
  176. keys := make([]string, 0)
  177. for key := range k.Fields {
  178. keys = append(keys, key)
  179. }
  180. sort.Strings(keys)
  181. return keys
  182. }
  183. // Map is an object who values must all be of the same `SubType`.
  184. // The key of the object is always of type `string`.
  185. type Map struct {
  186. BaseSchema
  187. SubType Schema
  188. }
  189. var _ Schema = &Map{}
  190. func (m *Map) Accept(v SchemaVisitor) {
  191. v.VisitMap(m)
  192. }
  193. func (m *Map) GetName() string {
  194. return fmt.Sprintf("Map of %s", m.SubType.GetName())
  195. }
  196. // Primitive is a literal. There can be multiple types of primitives,
  197. // and this subtype can be visited through the `subType` field.
  198. type Primitive struct {
  199. BaseSchema
  200. // Type of a primitive must be one of: integer, number, string, boolean.
  201. Type string
  202. Format string
  203. }
  204. var _ Schema = &Primitive{}
  205. func (p *Primitive) Accept(v SchemaVisitor) {
  206. v.VisitPrimitive(p)
  207. }
  208. func (p *Primitive) GetName() string {
  209. if p.Format == "" {
  210. return p.Type
  211. }
  212. return fmt.Sprintf("%s (%s)", p.Type, p.Format)
  213. }
  214. // Arbitrary is a value of any type (primitive, object or array)
  215. type Arbitrary struct {
  216. BaseSchema
  217. }
  218. var _ Schema = &Arbitrary{}
  219. func (a *Arbitrary) Accept(v SchemaVisitor) {
  220. if visitor, ok := v.(SchemaVisitorArbitrary); ok {
  221. visitor.VisitArbitrary(a)
  222. }
  223. }
  224. func (a *Arbitrary) GetName() string {
  225. return "Arbitrary value (primitive, object or array)"
  226. }
  227. // Reference implementation depends on the type of document.
  228. type Reference interface {
  229. Schema
  230. Reference() string
  231. SubSchema() Schema
  232. }