2 Copyright 2017 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
25 // Defines openapi types.
32 // These types are private as they should never leak, and are
33 // represented by actual structs.
38 // Models interface describe a model provider. They can give you the
39 // schema for a specific model.
40 type Models interface {
41 LookupModel(string) Schema
45 // SchemaVisitor is an interface that you need to implement if you want
46 // to "visit" an openapi schema. A dispatch on the Schema type will call
47 // the appropriate function based on its actual type:
48 // - Array is a list of one and only one given subtype
49 // - Map is a map of string to one and only one given subtype
50 // - Primitive can be string, integer, number and boolean.
51 // - Kind is an object with specific fields mapping to specific types.
52 // - Reference is a link to another definition.
53 type SchemaVisitor interface {
56 VisitPrimitive(*Primitive)
58 VisitReference(Reference)
61 // SchemaVisitorArbitrary is an additional visitor interface which handles
62 // arbitrary types. For backwards compatibility, it's a separate interface
63 // which is checked for at runtime.
64 type SchemaVisitorArbitrary interface {
66 VisitArbitrary(*Arbitrary)
69 // Schema is the base definition of an openapi type.
70 type Schema interface {
71 // Giving a visitor here will let you visit the actual type.
74 // Pretty print the name of the type.
76 // Describes how to access this field.
78 // Describes the field.
79 GetDescription() string
80 // Returns type extensions.
81 GetExtensions() map[string]interface{}
84 // Path helps us keep track of type paths
90 func NewPath(key string) Path {
94 func (p *Path) Get() []string {
101 return append(p.parent.Get(), p.key)
104 func (p *Path) Len() int {
108 func (p *Path) String() string {
109 return strings.Join(p.Get(), "")
112 // ArrayPath appends an array index and creates a new path
113 func (p *Path) ArrayPath(i int) Path {
116 key: fmt.Sprintf("[%d]", i),
120 // FieldPath appends a field name and creates a new path
121 func (p *Path) FieldPath(field string) Path {
124 key: fmt.Sprintf(".%s", field),
128 // BaseSchema holds data used by each types of schema.
129 type BaseSchema struct {
131 Extensions map[string]interface{}
136 func (b *BaseSchema) GetDescription() string {
140 func (b *BaseSchema) GetExtensions() map[string]interface{} {
144 func (b *BaseSchema) GetPath() *Path {
148 // Array must have all its element of the same `SubType`.
155 var _ Schema = &Array{}
157 func (a *Array) Accept(v SchemaVisitor) {
161 func (a *Array) GetName() string {
162 return fmt.Sprintf("Array of %s", a.SubType.GetName())
165 // Kind is a complex object. It can have multiple different
166 // subtypes for each field, as defined in the `Fields` field. Mandatory
167 // fields are listed in `RequiredFields`. The key of the object is
168 // always of type `string`.
172 // Lists names of required fields.
173 RequiredFields []string
174 // Maps field names to types.
175 Fields map[string]Schema
176 // FieldOrder reports the canonical order for the fields.
180 var _ Schema = &Kind{}
182 func (k *Kind) Accept(v SchemaVisitor) {
186 func (k *Kind) GetName() string {
187 properties := []string{}
188 for key := range k.Fields {
189 properties = append(properties, key)
191 return fmt.Sprintf("Kind(%v)", properties)
194 // IsRequired returns true if `field` is a required field for this type.
195 func (k *Kind) IsRequired(field string) bool {
196 for _, f := range k.RequiredFields {
204 // Keys returns a alphabetically sorted list of keys.
205 func (k *Kind) Keys() []string {
206 keys := make([]string, 0)
207 for key := range k.Fields {
208 keys = append(keys, key)
214 // Map is an object who values must all be of the same `SubType`.
215 // The key of the object is always of type `string`.
222 var _ Schema = &Map{}
224 func (m *Map) Accept(v SchemaVisitor) {
228 func (m *Map) GetName() string {
229 return fmt.Sprintf("Map of %s", m.SubType.GetName())
232 // Primitive is a literal. There can be multiple types of primitives,
233 // and this subtype can be visited through the `subType` field.
234 type Primitive struct {
237 // Type of a primitive must be one of: integer, number, string, boolean.
242 var _ Schema = &Primitive{}
244 func (p *Primitive) Accept(v SchemaVisitor) {
248 func (p *Primitive) GetName() string {
252 return fmt.Sprintf("%s (%s)", p.Type, p.Format)
255 // Arbitrary is a value of any type (primitive, object or array)
256 type Arbitrary struct {
260 var _ Schema = &Arbitrary{}
262 func (a *Arbitrary) Accept(v SchemaVisitor) {
263 if visitor, ok := v.(SchemaVisitorArbitrary); ok {
264 visitor.VisitArbitrary(a)
268 func (a *Arbitrary) GetName() string {
269 return "Arbitrary value (primitive, object or array)"
272 // Reference implementation depends on the type of document.
273 type Reference interface {