openapi.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // Returns type extensions.
  69. GetExtensions() map[string]interface{}
  70. }
  71. // Path helps us keep track of type paths
  72. type Path struct {
  73. parent *Path
  74. key string
  75. }
  76. func NewPath(key string) Path {
  77. return Path{key: key}
  78. }
  79. func (p *Path) Get() []string {
  80. if p == nil {
  81. return []string{}
  82. }
  83. if p.key == "" {
  84. return p.parent.Get()
  85. }
  86. return append(p.parent.Get(), p.key)
  87. }
  88. func (p *Path) Len() int {
  89. return len(p.Get())
  90. }
  91. func (p *Path) String() string {
  92. return strings.Join(p.Get(), "")
  93. }
  94. // ArrayPath appends an array index and creates a new path
  95. func (p *Path) ArrayPath(i int) Path {
  96. return Path{
  97. parent: p,
  98. key: fmt.Sprintf("[%d]", i),
  99. }
  100. }
  101. // FieldPath appends a field name and creates a new path
  102. func (p *Path) FieldPath(field string) Path {
  103. return Path{
  104. parent: p,
  105. key: fmt.Sprintf(".%s", field),
  106. }
  107. }
  108. // BaseSchema holds data used by each types of schema.
  109. type BaseSchema struct {
  110. Description string
  111. Extensions map[string]interface{}
  112. Path Path
  113. }
  114. func (b *BaseSchema) GetDescription() string {
  115. return b.Description
  116. }
  117. func (b *BaseSchema) GetExtensions() map[string]interface{} {
  118. return b.Extensions
  119. }
  120. func (b *BaseSchema) GetPath() *Path {
  121. return &b.Path
  122. }
  123. // Array must have all its element of the same `SubType`.
  124. type Array struct {
  125. BaseSchema
  126. SubType Schema
  127. }
  128. var _ Schema = &Array{}
  129. func (a *Array) Accept(v SchemaVisitor) {
  130. v.VisitArray(a)
  131. }
  132. func (a *Array) GetName() string {
  133. return fmt.Sprintf("Array of %s", a.SubType.GetName())
  134. }
  135. // Kind is a complex object. It can have multiple different
  136. // subtypes for each field, as defined in the `Fields` field. Mandatory
  137. // fields are listed in `RequiredFields`. The key of the object is
  138. // always of type `string`.
  139. type Kind struct {
  140. BaseSchema
  141. // Lists names of required fields.
  142. RequiredFields []string
  143. // Maps field names to types.
  144. Fields map[string]Schema
  145. // FieldOrder reports the canonical order for the fields.
  146. FieldOrder []string
  147. }
  148. var _ Schema = &Kind{}
  149. func (k *Kind) Accept(v SchemaVisitor) {
  150. v.VisitKind(k)
  151. }
  152. func (k *Kind) GetName() string {
  153. properties := []string{}
  154. for key := range k.Fields {
  155. properties = append(properties, key)
  156. }
  157. return fmt.Sprintf("Kind(%v)", properties)
  158. }
  159. // IsRequired returns true if `field` is a required field for this type.
  160. func (k *Kind) IsRequired(field string) bool {
  161. for _, f := range k.RequiredFields {
  162. if f == field {
  163. return true
  164. }
  165. }
  166. return false
  167. }
  168. // Keys returns a alphabetically sorted list of keys.
  169. func (k *Kind) Keys() []string {
  170. keys := make([]string, 0)
  171. for key := range k.Fields {
  172. keys = append(keys, key)
  173. }
  174. sort.Strings(keys)
  175. return keys
  176. }
  177. // Map is an object who values must all be of the same `SubType`.
  178. // The key of the object is always of type `string`.
  179. type Map struct {
  180. BaseSchema
  181. SubType Schema
  182. }
  183. var _ Schema = &Map{}
  184. func (m *Map) Accept(v SchemaVisitor) {
  185. v.VisitMap(m)
  186. }
  187. func (m *Map) GetName() string {
  188. return fmt.Sprintf("Map of %s", m.SubType.GetName())
  189. }
  190. // Primitive is a literal. There can be multiple types of primitives,
  191. // and this subtype can be visited through the `subType` field.
  192. type Primitive struct {
  193. BaseSchema
  194. // Type of a primitive must be one of: integer, number, string, boolean.
  195. Type string
  196. Format string
  197. }
  198. var _ Schema = &Primitive{}
  199. func (p *Primitive) Accept(v SchemaVisitor) {
  200. v.VisitPrimitive(p)
  201. }
  202. func (p *Primitive) GetName() string {
  203. if p.Format == "" {
  204. return p.Type
  205. }
  206. return fmt.Sprintf("%s (%s)", p.Type, p.Format)
  207. }
  208. // Arbitrary is a value of any type (primitive, object or array)
  209. type Arbitrary struct {
  210. BaseSchema
  211. }
  212. var _ Schema = &Arbitrary{}
  213. func (a *Arbitrary) Accept(v SchemaVisitor) {
  214. if visitor, ok := v.(SchemaVisitorArbitrary); ok {
  215. visitor.VisitArbitrary(a)
  216. }
  217. }
  218. func (a *Arbitrary) GetName() string {
  219. return "Arbitrary value (primitive, object or array)"
  220. }
  221. // Reference implementation depends on the type of document.
  222. type Reference interface {
  223. Schema
  224. Reference() string
  225. SubSchema() Schema
  226. }