spec

package
v0.1.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2020 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Operation

type Operation struct {
	// Name of the operation if any.
	Name string `json:"name"`

	// The original ID of the operation if any.
	ID string `json:"id"`

	// Description of the operation if any.
	Description string `json:"description"`

	// Additional comments for the operation, if any.
	Comments []string `json:"comments"`

	// HTTP method of the operation
	Method string `json:"method"`

	// Parameters of the operation, if any.
	Parameters []*Parameter `json:"parameters"`

	// Responses of the operation mapped to status codes.
	Responses []*Response `json:"responses"`

	// Callbacks of the operation
	Callbacks map[string][]*Path `json:"callbacks"`
}

Operation is a HTTP operation.

type Parameter

type Parameter struct {
	// Name of the parameter.
	Name string `json:"name"`

	// Description of the parameter if any.
	Description string `json:"description"`

	// Type of the parameter.
	Type ParameterType `json:"type"`

	// Sometimes parameters are grouped in a struct type.
	// GroupType is the name of it.
	GroupType string `json:"groupType"`

	// The content type, if the parameter.
	// is expected in the body.
	ContentType string `json:"contentType"`

	// The schema of the parameter, if any.
	Schema *Schema `json:"schema"`

	Serialization ParameterSerialization `json:"serialization"`

	// Marks the parameter as required.
	Required bool `json:"required"`
}

Parameter is a parameter for a HTTP operation.

func (*Parameter) IsPtr

func (p *Parameter) IsPtr() bool

type ParameterSerialization

type ParameterSerialization struct {
	Style   ParameterSerializationStyle `json:"style"`
	Explode bool                        `json:"explode"`
}

type ParameterSerializationStyle

type ParameterSerializationStyle string
const (
	SerializationSimple         ParameterSerializationStyle = "simple"
	SerializationLabel          ParameterSerializationStyle = "label"
	SerializationMatrix         ParameterSerializationStyle = "matrix"
	SerializationForm           ParameterSerializationStyle = "form"
	SerializationSpaceDelimited ParameterSerializationStyle = "spaceDelimited"
	SerializationPipeDelimited  ParameterSerializationStyle = "pipeDelimited"
	SerializationDeepObject     ParameterSerializationStyle = "deepObject"
)

type ParameterType

type ParameterType string

ParameterType describes where the parameter is expected.

const (
	// ParameterTypeQuery means the parameter is expected in the query string of the request.
	ParameterTypeQuery ParameterType = "query"

	// ParameterTypeBody means the parameter is expected in the body of the request.
	ParameterTypeBody ParameterType = "body"

	// ParameterTypePath means the parameter is expected in the path of the request.
	ParameterTypePath ParameterType = "path"

	// ParameterTypeHeader means the parameter is expected in a header of the request.
	ParameterTypeHeader ParameterType = "header"

	// ParameterTypeCookie means the parameter is expected in a cookie of the request.
	ParameterTypeCookie ParameterType = "cookie"
)

type Path

type Path struct {
	// PathString is the original string of the path
	// like "/pets/{id}/profile"
	PathString string `json:"pathString"`

	// Name of the path, either automatically generated
	// from the path string, or given in an extension.
	Name string `json:"name"`

	// Description of the path if any.
	Description string `json:"description"`

	// Additional comments for the path, if any.
	Comments []string `json:"comments"`

	// Operations of the path
	Operations []*Operation `json:"operations"`
}

Path is a HTTP REST-like path.

type Response

type Response struct {
	// Name of the response if any.
	Name string `json:"name"`

	// Description of the response if any.
	Description string `json:"description"`

	// HTTP status code of the response.
	// It is a sting because Open API schema permits codes like 50x.
	Code string `json:"code"`

	// The content type of the parameter.
	ContentType string `json:"contentType"`

	// The schema of the response, if any.
	Schema *Schema `json:"schema"`
}

Response is one of the expected responses for a HTTP operation.

func (*Response) IsPtr

func (r *Response) IsPtr() bool

type Schema

type Schema struct {
	// Name is the given name of the schema.
	// It is also the type's name.
	Name string

	// The type in case of simple types.
	PrimitiveType string

	// Whether the type should be an alias
	// for an another one.
	Alias bool

	// OriginalName is the original name of
	// the schema from the specification.
	OriginalName string

	// FieldName is the name of the field
	// the schema is part of, if any.
	FieldName string

	// Description is the description of the original object
	// parsed from the specification.
	Description string

	// Additional comments for the schema, if any.
	Comments []string

	// Nullable defines whether the type can be nil.
	Nullable bool

	// Create indicates that the type must be created
	Create bool

	// Tags of the schema for json and such
	Tags map[string][]string

	// Variant is the variant of the schema.
	Variant SchemaVariant

	// Name of the additional properties field
	// if any.
	AdditionalPropsName string

	// Additional properties of the schema,
	// if it is a struct.
	AdditionalProps *Schema

	// Used for enum types
	Enum []interface{}

	// Children are needed in cases like when the
	// parent object is a struct, or a compound object.
	Children *SchemaObject
}

Schema is an abstraction over a specification schema.

func NewSchema

func NewSchema() *Schema

NewSchema creates a new empty schema

func (*Schema) AddComments

func (s *Schema) AddComments(comments ...string) *Schema

AddComments is a helper method to add comments

func (*Schema) AllOf

func (s *Schema) AllOf(children interface{}) *Schema

AllOf is a convenience method for AllOf variant

func (*Schema) Any

func (s *Schema) Any() *Schema

Any is a convenience method for Any variant

func (*Schema) AnyOf

func (s *Schema) AnyOf(children interface{}) *Schema

AnyOf is a convenience method for AnyOf variant

func (*Schema) Array

func (s *Schema) Array(child *Schema) *Schema

Array is a convenience method for setting an Array

func (*Schema) CanBeNil

func (s *Schema) CanBeNil() bool

CanBeNil is a helper method to determine whether the type can be nil (E.g. maps).

func (*Schema) HasChildren

func (s *Schema) HasChildren() bool

HasChildren check whether the schema has children

func (*Schema) Map

func (s *Schema) Map(key *Schema, value *Schema) *Schema

Map is a convenience method for setting a Map

func (*Schema) OneOf

func (s *Schema) OneOf(children interface{}) *Schema

OneOf is a convenience method for OneOf variant

func (*Schema) Primitive

func (s *Schema) Primitive(name string) *Schema

Primitive is a convenience method for Primitive

func (*Schema) SetNullable

func (s *Schema) SetNullable() *Schema

SetNullable sets the schema to be nullable.

func (*Schema) SetVariant

func (s *Schema) SetVariant(variant SchemaVariant) *Schema

SetVariant sets the variant of the type

func (*Schema) ShouldBePtr

func (s *Schema) ShouldBePtr() bool

ShouldBePtr is a helper method to determine if a schema type should be passed by value or by reference.

func (*Schema) ShouldCreate

func (s *Schema) ShouldCreate(create bool) *Schema

ShouldCreate is a convenience method for Create.

func (*Schema) Struct

func (s *Schema) Struct(child map[string]*Schema) *Schema

Struct is a convenience method for setting a Struct

func (*Schema) Walk

func (s *Schema) Walk(walker SchemaWalker, bottomUp bool) error

Walk traverses the schema tree, calling the walker function for every schema in it.

func (*Schema) WithChildren

func (s *Schema) WithChildren(children interface{}) *Schema

WithChildren sets the children of the schema. If the type of the children isn't compatible with a SchemaObject, it will panic.

func (*Schema) WithName

func (s *Schema) WithName(name string) *Schema

WithName sets the name of the schema.

func (*Schema) WithType

func (s *Schema) WithType(name string) *Schema

WithType sets the type of the schema.

type SchemaObject

type SchemaObject struct {
	Schema *Schema
	Array  []*Schema
	Map    map[string]*Schema
}

SchemaObject is used in cases where multiple forms of schemas might be needed.

func NewSchemaObject

func NewSchemaObject(values interface{}) (*SchemaObject, error)

NewSchemaObject creates a new schema object of a compatible type

func (*SchemaObject) GetArray

func (s *SchemaObject) GetArray() []*Schema

GetArray is a helper method to avoid nil panics

func (*SchemaObject) GetMap

func (s *SchemaObject) GetMap() map[string]*Schema

GetMap is a helper method to avoid nil panics

func (*SchemaObject) GetSchema

func (s *SchemaObject) GetSchema() *Schema

GetSchema is a helper method to avoid nil panics

func (*SchemaObject) Is

func (s *SchemaObject) Is(obj interface{}) bool

Is checks whether SchemaObject equals the given value

func (*SchemaObject) IsArray

func (s *SchemaObject) IsArray() bool

IsArray checks whether the object is an array of schemas

func (*SchemaObject) IsMap

func (s *SchemaObject) IsMap() bool

IsMap checks whether the object is a map of schemas

func (*SchemaObject) IsSchema

func (s *SchemaObject) IsSchema() bool

IsSchema checks whether the object is a single schema

type SchemaPath

type SchemaPath []*Schema

SchemaPath is a path of nested schemas. It is used while walking a schema.

func (SchemaPath) First

func (s SchemaPath) First() *Schema

First returns the first schema in the path

func (SchemaPath) Last

func (s SchemaPath) Last() *Schema

Last returns the last schema in the path

type SchemaVariant

type SchemaVariant string

SchemaVariant defines the variant of the schema. In most cases giving the schema a Go type is not enough, for example if a schema is an AllOf or even an object with properties.

const (
	// VariantPrimitive is a schema with no special attributes,
	// and has a simple Go type.
	VariantPrimitive SchemaVariant = "simple"

	// VariantAny is a schema where the type can be anything.
	// Typically it's an interface{}.
	VariantAny SchemaVariant = "any"

	// VariantArray is a schema where there can be
	// more than one of its children.
	VariantArray SchemaVariant = "array"

	// VariantMap is a schema where the first child
	// is the key type of the map, and the second is the value type
	VariantMap SchemaVariant = "map"

	// VariantStruct is a schema which
	// forms a struct of its children.
	VariantStruct SchemaVariant = "struct"

	// VariantAllOf defines a compound schema
	// of which all its children must be part of.
	VariantAllOf SchemaVariant = "allOf"

	// VariantAnyOf defines a compound schema where
	// one of its children might be present.
	VariantAnyOf SchemaVariant = "anyOf"

	// VariantOneOf defines a compound schema where
	// at least one of its children must be present.
	VariantOneOf SchemaVariant = "oneOf"
)

type SchemaWalker

type SchemaWalker func(path SchemaPath) error

SchemaWalker is used when traversing a schema tree. A returned non-nil error will stop the traversal.

type Spec

type Spec struct {
	Paths []*Path `json:"paths"`
	// Schemas used in the specification
	Schemas []*Schema `json:"schemas"`
}

Spec is an abstraction over a specification.

Due to the ever-changing specificaitions, we'd have to rewrite code every few months in order to support more versions.

Instead of that, we create an abstraction with the information of just what we need.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL