design

package
v0.0.0-...-3014a84 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2015 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package design defines types which describe the data types used by action controllers. These are the data structures of the request payloads and parameters as well as the response payloads. There are primitive types corresponding to the JSON primitive types (bool, string, integer and number), array types which represent a collection of another type and object types corresponding to JSON objects (i.e. a map indexed by strings where each value may be any of the data types). On top of these the package also defines "user types" and "media types". Both these types are named objects with additional properties (a description and for media types the media type identifier, links and views).

Index

Constants

View Source
const (
	// BooleanKind represents a JSON bool.
	BooleanKind = iota + 1
	// IntegerKind represents a JSON integer.
	IntegerKind
	// NumberKind represents a JSON number including integers.
	NumberKind
	// StringKind represents a JSON string.
	StringKind
	// AnyKind represents a generic interface{}.
	AnyKind
	// ArrayKind represents a JSON array.
	ArrayKind
	// ObjectKind represents a JSON object.
	ObjectKind
	// HashKind represents a JSON object where the keys are not known in advance.
	HashKind
	// UserTypeKind represents a user type.
	UserTypeKind
	// MediaTypeKind represents a media type.
	MediaTypeKind
)
View Source
const (
	// Boolean is the type for a JSON boolean.
	Boolean = Primitive(BooleanKind)

	// Integer is the type for a JSON number without a fraction or exponent part.
	Integer = Primitive(IntegerKind)

	// Number is the type for any JSON number, including integers.
	Number = Primitive(NumberKind)

	// String is the type for a JSON string.
	String = Primitive(StringKind)

	// Any is the type for an arbitrary JSON value (interface{} in Go).
	Any = Primitive(AnyKind)
)

Variables

This section is empty.

Functions

func CanonicalIdentifier

func CanonicalIdentifier(identifier string) string

CanonicalIdentifier returns the media type identifier sans suffix which is what the DSL uses to store and lookup media types.

func ExtractWildcards

func ExtractWildcards(path string) []string

ExtractWildcards returns the names of the wildcards that appear in path.

Types

type APIDefinition

type APIDefinition struct {
	// API name
	Name string
	// API Title
	Title string
	// API description
	Description string // API description
	// API hostname
	Host string
	// API URL schemes
	Schemes []string
	// Common base path to all API actions
	BasePath string
	// Common path parameters to all API actions
	BaseParams *AttributeDefinition
	// TermsOfService describes or links to the API terms of service
	TermsOfService string
	// Contact provides the API users with contact information
	Contact *ContactDefinition
	// License describes the API license
	License *LicenseDefinition
	// Docs points to the API external documentation
	Docs *DocsDefinition
	// Exposed resources indexed by name
	Resources map[string]*ResourceDefinition
	// Traits available to all API resources and actions indexed by name
	Traits map[string]*TraitDefinition
	// Responses available to all API actions indexed by name
	Responses map[string]*ResponseDefinition
	// Response template factories available to all API actions indexed by name
	ResponseTemplates map[string]*ResponseTemplateDefinition
	// Built-in responses
	DefaultResponses map[string]*ResponseDefinition
	// Built-in response templates
	DefaultResponseTemplates map[string]*ResponseTemplateDefinition
	// User types
	Types map[string]*UserTypeDefinition
	// Media types
	MediaTypes map[string]*MediaTypeDefinition
	// dsl contains the DSL used to create this definition if any.
	DSL func()
	// metadata is a list of key/value pairs
	Metadata MetadataDefinition
	// contains filtered or unexported fields
}

APIDefinition defines the global properties of the API.

var (
	// Design is the API definition created via DSL.
	Design *APIDefinition

	// WildcardRegex is the regular expression used to capture path parameters.
	WildcardRegex = regexp.MustCompile(`/(?::|\*)([a-zA-Z0-9_]+)`)
)

func (*APIDefinition) Context

func (a *APIDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*APIDefinition) Example

func (a *APIDefinition) Example(dt DataType) interface{}

Example returns a random value for the given data type. If the data type has validations then the example value validates them. Example returns the same random value for a given api name (the random generator is seeded after the api name).

func (*APIDefinition) IterateMediaTypes

func (a *APIDefinition) IterateMediaTypes(it MediaTypeIterator) error

IterateMediaTypes calls the given iterator passing in each media type sorted in alphabetical order. Iteration stops if an iterator returns an error and in this case IterateMediaTypes returns that error.

func (*APIDefinition) IterateResources

func (a *APIDefinition) IterateResources(it ResourceIterator) error

IterateResources calls the given iterator passing in each resource sorted in alphabetical order. Iteration stops if an iterator returns an error and in this case IterateResources returns that error.

func (*APIDefinition) IterateResponses

func (a *APIDefinition) IterateResponses(it ResponseIterator) error

IterateResponses calls the given iterator passing in each response sorted in alphabetical order. Iteration stops if an iterator returns an error and in this case IterateResponses returns that error.

func (*APIDefinition) IterateUserTypes

func (a *APIDefinition) IterateUserTypes(it UserTypeIterator) error

IterateUserTypes calls the given iterator passing in each user type sorted in alphabetical order. Iteration stops if an iterator returns an error and in this case IterateUserTypes returns that error.

func (*APIDefinition) MediaTypeWithIdentifier

func (a *APIDefinition) MediaTypeWithIdentifier(id string) *MediaTypeDefinition

MediaTypeWithIdentifier returns the media type with a matching media type identifier. Two media type identifiers match if their values sans suffix match. So for example "application/vnd.foo+xml", "application/vnd.foo+json" and "application/vnd.foo" all match.

func (*APIDefinition) Validate

func (a *APIDefinition) Validate() *ValidationErrors

Validate tests whether the API definition is consistent: all resource parent names resolve to an actual resource.

type ActionDefinition

type ActionDefinition struct {
	// Action name, e.g. "create"
	Name string
	// Action description, e.g. "Creates a task"
	Description string
	// Docs points to the API external documentation
	Docs *DocsDefinition
	// Parent resource
	Parent *ResourceDefinition
	// Action routes
	Routes []*RouteDefinition
	// Map of possible response definitions indexed by name
	Responses map[string]*ResponseDefinition
	// Path and query string parameters
	Params *AttributeDefinition
	// Query string parameters only
	QueryParams *AttributeDefinition
	// Payload blueprint (request body) if any
	Payload *UserTypeDefinition
	// Request headers that need to be made available to action
	Headers *AttributeDefinition
	// Metadata is a list of key/value pairs
	Metadata MetadataDefinition
}

ActionDefinition defines a resource action. It defines both an HTTP endpoint and the shape of HTTP requests and responses made to that endpoint. The shape of requests is defined via "parameters", there are path parameters (i.e. portions of the URL that define parameter values), query string parameters and a payload parameter (request body).

func (*ActionDefinition) AllParamNames

func (a *ActionDefinition) AllParamNames() []string

AllParamNames returns the path and query string parameter names of the action across all its routes.

func (*ActionDefinition) AllParams

func (a *ActionDefinition) AllParams() *AttributeDefinition

AllParams returns the path and query string parameters of the action across all its routes.

func (*ActionDefinition) Context

func (a *ActionDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*ActionDefinition) Validate

func (a *ActionDefinition) Validate() *ValidationErrors

Validate tests whether the action definition is consistent: parameters have unique names and it has at least one response.

func (*ActionDefinition) ValidateParams

func (a *ActionDefinition) ValidateParams() *ValidationErrors

ValidateParams checks the action parameters (make sure they have names, members and types).

type ActionIterator

type ActionIterator func(a *ActionDefinition) error

ActionIterator is the type of functions given to IterateActions.

type Array

type Array struct {
	ElemType *AttributeDefinition
}

Array is the type for a JSON array.

func (*Array) Dup

func (a *Array) Dup() DataType

Dup calls Dup on the array ElemType and creates an array with the result.

func (*Array) Example

func (a *Array) Example(r *RandomGenerator) interface{}

Example produces a random array value.

func (*Array) IsArray

func (a *Array) IsArray() bool

IsArray returns true.

func (*Array) IsCompatible

func (a *Array) IsCompatible(val interface{}) bool

IsCompatible returns true if val is compatible with p.

func (*Array) IsHash

func (a *Array) IsHash() bool

IsHash returns false.

func (*Array) IsObject

func (a *Array) IsObject() bool

IsObject returns false.

func (*Array) IsPrimitive

func (a *Array) IsPrimitive() bool

IsPrimitive returns false.

func (*Array) Kind

func (a *Array) Kind() Kind

Kind implements DataKind.

func (*Array) Name

func (a *Array) Name() string

Name returns the type name.

func (*Array) ToArray

func (a *Array) ToArray() *Array

ToArray returns a.

func (*Array) ToHash

func (a *Array) ToHash() *Hash

ToHash returns nil.

func (*Array) ToObject

func (a *Array) ToObject() Object

ToObject returns nil.

type AttributeDefinition

type AttributeDefinition struct {
	// Attribute type
	Type DataType
	// Attribute reference type if any
	Reference DataType
	// Optional description
	Description string
	// Optional validation functions
	Validations []ValidationDefinition
	// Metadata is a list of key/value pairs
	Metadata MetadataDefinition
	// Optional member default value
	DefaultValue interface{}
	// Optional view used to render Attribute (only applies to media type attributes).
	View string
}

AttributeDefinition defines a JSON object member with optional description, default value and validations.

func (*AttributeDefinition) AllRequired

func (a *AttributeDefinition) AllRequired() []string

AllRequired returns the complete list of all required attribute names, nil if it doesn't have a RequiredValidationDefinition validation.

func (*AttributeDefinition) Context

func (a *AttributeDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*AttributeDefinition) Definition

func (a *AttributeDefinition) Definition() *AttributeDefinition

Definition returns the underlying attribute definition. Note that this function is "inherited" by both UserTypeDefinition and MediaTypeDefinition.

func (*AttributeDefinition) Dup

Dup returns a copy of the attribute definition. Note: the primitive underlying types are not duplicated for simplicity.

func (*AttributeDefinition) Example

func (a *AttributeDefinition) Example(r *RandomGenerator) interface{}

Example returns a random instance of the attribute that validates.

func (*AttributeDefinition) Inherit

func (a *AttributeDefinition) Inherit(parent *AttributeDefinition)

Inherit merges the properties of existing target type attributes with the argument's. The algorithm is recursive so that child attributes are also merged.

func (*AttributeDefinition) IsRequired

func (a *AttributeDefinition) IsRequired(attName string) bool

IsRequired returns true if the given string matches the name of a required attribute, false otherwise.

func (*AttributeDefinition) Merge

Merge merges the argument attributes into the target and returns the target overriding existing attributes with identical names. This only applies to attributes of type Object and Merge panics if the argument or the target is not of type Object.

func (*AttributeDefinition) Validate

func (a *AttributeDefinition) Validate(ctx string, parent DSLDefinition) *ValidationErrors

Validate tests whether the attribute definition is consistent: required fields exist. Since attributes are unaware of their context, additional context information can be provided to be used in error messages. The parent definition context is automatically added to error messages.

type AttributeIterator

type AttributeIterator func(string, *AttributeDefinition) error

AttributeIterator is the type of the function given to IterateAttributes.

type ContactDefinition

type ContactDefinition struct {
	// Name of the contact person/organization
	Name string `json:"name,omitempty"`
	// Email address of the contact person/organization
	Email string `json:"email,omitempty"`
	// URL pointing to the contact information
	URL string `json:"url,omitempty"`
}

ContactDefinition contains the API contact information.

func (*ContactDefinition) Context

func (c *ContactDefinition) Context() string

Context returns the generic definition name used in error messages.

type DSLDefinition

type DSLDefinition interface {
	// Context is used to build error messages that refer to the definition.
	Context() string
}

DSLDefinition is the common interface implemented by all definitions.

type DataStructure

type DataStructure interface {
	// Definition returns the data structure definition.
	Definition() *AttributeDefinition
}

DataStructure is the interface implemented by all data structure types. That is attribute definitions, user types and media types.

type DataType

type DataType interface {
	// Kind of data type, one of the Kind enum.
	Kind() Kind
	// Name returns the type name.
	Name() string
	// IsPrimitive returns true if the underlying type is one of the primitive types.
	IsPrimitive() bool
	// IsObject returns true if the underlying type is an object, a user type which
	// is an object or a media type whose type is an object.
	IsObject() bool
	// IsArray returns true if the underlying type is an array, a user type which
	// is an array or a media type whose type is an array.
	IsArray() bool
	// IsHash returns true if the underlying type is a hash map, a user type which
	// is a hash map or a media type whose type is a hash map.
	IsHash() bool
	// ToObject returns the underlying object if any (i.e. if IsObject returns true),
	// nil otherwise.
	ToObject() Object
	// ToArray returns the underlying array if any (i.e. if IsArray returns true),
	// nil otherwise.
	ToArray() *Array
	// ToHash returns the underlying hash map if any (i.e. if IsHash returns true),
	// nil otherwise.
	ToHash() *Hash
	// IsCompatible checks whether val has a Go type that is
	// compatible with the data type.
	IsCompatible(val interface{}) bool
	// Dup creates a copy of the type. This is only relevant for types that are
	// DSLDefinition (i.e. have an attribute definition).
	Dup() DataType
	// Example returns a random value for the given data type.
	// If the data type has validations then the example value validates them.
	Example(r *RandomGenerator) interface{}
}

DataType is the common interface to all types.

type DocsDefinition

type DocsDefinition struct {
	// Description of documentation.
	Description string `json:"description,omitempty"`
	// URL to documentation.
	URL string `json:"url,omitempty"`
}

DocsDefinition points to external documentation.

func (*DocsDefinition) Context

func (d *DocsDefinition) Context() string

Context returns the generic definition name used in error messages.

type EnumValidationDefinition

type EnumValidationDefinition struct {
	Values []interface{}
}

EnumValidationDefinition represents an enum validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor76.

func (*EnumValidationDefinition) Context

func (v *EnumValidationDefinition) Context() string

Context returns the generic definition name used in error messages.

type FormatValidationDefinition

type FormatValidationDefinition struct {
	Format string
}

FormatValidationDefinition represents a format validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor104.

func (*FormatValidationDefinition) Context

func (f *FormatValidationDefinition) Context() string

Context returns the generic definition name used in error messages.

type Hash

type Hash struct {
	KeyType  *AttributeDefinition
	ElemType *AttributeDefinition
}

Hash is the type for a hash map.

func (*Hash) Dup

func (h *Hash) Dup() DataType

Dup creates a copy of h.

func (*Hash) Example

func (h *Hash) Example(r *RandomGenerator) interface{}

Example returns a random hash value.

func (*Hash) IsArray

func (h *Hash) IsArray() bool

IsArray returns false.

func (*Hash) IsCompatible

func (h *Hash) IsCompatible(val interface{}) bool

IsCompatible returns true if val is compatible with p.

func (*Hash) IsHash

func (h *Hash) IsHash() bool

IsHash returns true.

func (*Hash) IsObject

func (h *Hash) IsObject() bool

IsObject returns false.

func (*Hash) IsPrimitive

func (h *Hash) IsPrimitive() bool

IsPrimitive returns false.

func (*Hash) Kind

func (h *Hash) Kind() Kind

Kind implements DataKind.

func (*Hash) Name

func (h *Hash) Name() string

Name returns the type name.

func (*Hash) ToArray

func (h *Hash) ToArray() *Array

ToArray returns nil.

func (*Hash) ToHash

func (h *Hash) ToHash() *Hash

ToHash returns the underlying hash map.

func (*Hash) ToObject

func (h *Hash) ToObject() Object

ToObject returns nil.

type Kind

type Kind uint

A Kind defines the JSON type that a DataType represents.

type LicenseDefinition

type LicenseDefinition struct {
	// Name of license used for the API
	Name string `json:"name,omitempty"`
	// URL to the license used for the API
	URL string `json:"url,omitempty"`
}

LicenseDefinition contains the license information for the API.

func (*LicenseDefinition) Context

func (l *LicenseDefinition) Context() string

Context returns the generic definition name used in error messages.

type LinkDefinition

type LinkDefinition struct {
	// Link name
	Name string
	// View used to render link if not "link"
	View string
	// URITemplate is the RFC6570 URI template of the link Href.
	URITemplate string

	// Parent media Type
	Parent *MediaTypeDefinition
}

LinkDefinition defines a media type link, it specifies a URL to a related resource.

func (*LinkDefinition) Attribute

func (l *LinkDefinition) Attribute() *AttributeDefinition

Attribute returns the linked attribute.

func (*LinkDefinition) Context

func (l *LinkDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*LinkDefinition) MediaType

func (l *LinkDefinition) MediaType() *MediaTypeDefinition

MediaType returns the media type of the linked attribute.

func (*LinkDefinition) Validate

func (l *LinkDefinition) Validate() *ValidationErrors

Validate checks that the link definition is consistent: it has a media type or the name of an attribute part of the parent media type.

type MaxLengthValidationDefinition

type MaxLengthValidationDefinition struct {
	MaxLength int
}

MaxLengthValidationDefinition represents an maximum length validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor26.

func (*MaxLengthValidationDefinition) Context

Context returns the generic definition name used in error messages.

type MaximumValidationDefinition

type MaximumValidationDefinition struct {
	Max float64
}

MaximumValidationDefinition represents a maximum value validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor17.

func (*MaximumValidationDefinition) Context

func (m *MaximumValidationDefinition) Context() string

Context returns the generic definition name used in error messages.

type MediaTypeDefinition

type MediaTypeDefinition struct {
	// A media type is a type
	*UserTypeDefinition
	// Identifier is the RFC 6838 media type identifier.
	Identifier string
	// Links list the rendered links indexed by name.
	Links map[string]*LinkDefinition
	// Views list the supported views indexed by name.
	Views map[string]*ViewDefinition
	// Resource this media type is the canonical representation for if any
	Resource *ResourceDefinition
}

MediaTypeDefinition describes the rendering of a resource using property and link definitions. A property corresponds to a single member of the media type, it has a name and a type as well as optional validation rules. A link has a name and a URL that points to a related resource. Media types also define views which describe which members and links to render when building the response body for the corresponding view.

func NewMediaTypeDefinition

func NewMediaTypeDefinition(name, identifier string, dsl func()) *MediaTypeDefinition

NewMediaTypeDefinition creates a media type definition but does not execute the DSL.

func (*MediaTypeDefinition) ComputeViews

func (m *MediaTypeDefinition) ComputeViews() map[string]*ViewDefinition

ComputeViews returns the media type views recursing as necessary if the media type is a collection.

func (*MediaTypeDefinition) Dup

func (m *MediaTypeDefinition) Dup() DataType

Dup returns a copy of m.

func (*MediaTypeDefinition) Kind

func (m *MediaTypeDefinition) Kind() Kind

Kind implements DataKind.

func (*MediaTypeDefinition) Validate

func (m *MediaTypeDefinition) Validate() *ValidationErrors

Validate checks that the media type definition is consistent: its identifier is a valid media type identifier.

type MediaTypeIterator

type MediaTypeIterator func(m *MediaTypeDefinition) error

MediaTypeIterator is the type of functions given to IterateMediaTypes.

type MetadataDefinition

type MetadataDefinition map[string]string

MetadataDefinition is a set of key/value pairs

type MinLengthValidationDefinition

type MinLengthValidationDefinition struct {
	MinLength int
}

MinLengthValidationDefinition represents an minimum length validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor29.

func (*MinLengthValidationDefinition) Context

Context returns the generic definition name used in error messages.

type MinimumValidationDefinition

type MinimumValidationDefinition struct {
	Min float64
}

MinimumValidationDefinition represents an minimum value validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor21.

func (*MinimumValidationDefinition) Context

func (m *MinimumValidationDefinition) Context() string

Context returns the generic definition name used in error messages.

type Object

type Object map[string]*AttributeDefinition

Object is the type for a JSON object.

func (Object) Dup

func (o Object) Dup() DataType

Dup creates a copy of o.

func (Object) Example

func (o Object) Example(r *RandomGenerator) interface{}

Example returns a random value of the object.

func (Object) IsArray

func (o Object) IsArray() bool

IsArray returns false.

func (Object) IsCompatible

func (o Object) IsCompatible(val interface{}) bool

IsCompatible returns true if val is compatible with p.

func (Object) IsHash

func (o Object) IsHash() bool

IsHash returns false.

func (Object) IsObject

func (o Object) IsObject() bool

IsObject returns true.

func (Object) IsPrimitive

func (o Object) IsPrimitive() bool

IsPrimitive returns false.

func (Object) IterateAttributes

func (o Object) IterateAttributes(it AttributeIterator) error

IterateAttributes calls the given iterator passing in each attribute sorted in alphabetical order. Iteration stops if an iterator returns an error and in this case IterateObject returns that error.

func (Object) Kind

func (o Object) Kind() Kind

Kind implements DataKind.

func (Object) Merge

func (o Object) Merge(other Object)

Merge copies other's attributes into o overridding any pre-existing attribute with the same name.

func (Object) Name

func (o Object) Name() string

Name returns the type name.

func (Object) ToArray

func (o Object) ToArray() *Array

ToArray returns nil.

func (Object) ToHash

func (o Object) ToHash() *Hash

ToHash returns nil.

func (Object) ToObject

func (o Object) ToObject() Object

ToObject returns the underlying object.

type PatternValidationDefinition

type PatternValidationDefinition struct {
	Pattern string
}

PatternValidationDefinition represents a pattern validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor33

func (*PatternValidationDefinition) Context

func (f *PatternValidationDefinition) Context() string

Context returns the generic definition name used in error messages.

type Primitive

type Primitive Kind

Primitive is the type for null, boolean, integer, number and string.

func (Primitive) Dup

func (p Primitive) Dup() DataType

Dup returns the primitive type.

func (Primitive) Example

func (p Primitive) Example(r *RandomGenerator) interface{}

Example returns an instance of the given data type.

func (Primitive) IsArray

func (p Primitive) IsArray() bool

IsArray returns false.

func (Primitive) IsCompatible

func (p Primitive) IsCompatible(val interface{}) (ok bool)

IsCompatible returns true if val is compatible with p.

func (Primitive) IsHash

func (p Primitive) IsHash() bool

IsHash returns false.

func (Primitive) IsObject

func (p Primitive) IsObject() bool

IsObject returns false.

func (Primitive) IsPrimitive

func (p Primitive) IsPrimitive() bool

IsPrimitive returns true.

func (Primitive) Kind

func (p Primitive) Kind() Kind

Kind implements DataKind.

func (Primitive) Name

func (p Primitive) Name() string

Name returns the type name.

func (Primitive) ToArray

func (p Primitive) ToArray() *Array

ToArray returns nil.

func (Primitive) ToHash

func (p Primitive) ToHash() *Hash

ToHash returns nil.

func (Primitive) ToObject

func (p Primitive) ToObject() Object

ToObject returns nil.

type RandomGenerator

type RandomGenerator struct {
	Seed string
	// contains filtered or unexported fields
}

RandomGenerator generates consistent random values of different types given a seed. The random values are consistent in that given the same seed the same random values get generated.

func NewRandomGenerator

func NewRandomGenerator(seed string) *RandomGenerator

NewRandomGenerator returns a random value generator seeded from the given string value.

func (*RandomGenerator) Bool

func (r *RandomGenerator) Bool() bool

Bool produces a random boolean.

func (*RandomGenerator) Float64

func (r *RandomGenerator) Float64() float64

Float64 produces a random float64 value.

func (*RandomGenerator) Int

func (r *RandomGenerator) Int() int

Int produces a random integer.

func (*RandomGenerator) String

func (r *RandomGenerator) String() string

String produces a random string of the given

type RequiredValidationDefinition

type RequiredValidationDefinition struct {
	Names []string
}

RequiredValidationDefinition represents a required validation as described at http://json-schema.org/latest/json-schema-validation.html#anchor61.

func (*RequiredValidationDefinition) Context

func (r *RequiredValidationDefinition) Context() string

Context returns the generic definition name used in error messages.

type ResourceDefinition

type ResourceDefinition struct {
	// Resource name
	Name string
	// Common URL prefix to all resource action HTTP requests
	BasePath string
	// Object describing each parameter that appears in BasePath if any
	BaseParams *AttributeDefinition
	// Name of parent resource if any
	ParentName string
	// Optional description
	Description string
	// Optional version
	Version string
	// Default media type, describes the resource attributes
	MediaType string
	// Exposed resource actions indexed by name
	Actions map[string]*ActionDefinition
	// Action with canonical resource path
	CanonicalActionName string
	// Map of response definitions that apply to all actions indexed by name.
	Responses map[string]*ResponseDefinition
	// Path and query string parameters that apply to all actions.
	Params *AttributeDefinition
	// Request headers that apply to all actions.
	Headers *AttributeDefinition
	// dsl contains the DSL used to create this definition if any.
	DSL func()
	// metadata is a list of key/value pairs
	Metadata MetadataDefinition
}

ResourceDefinition describes a REST resource. It defines both a media type and a set of actions that can be executed through HTTP requests. A resource is versioned so that multiple versions of the same resource may be exposed by the API.

func NewResourceDefinition

func NewResourceDefinition(name string, dsl func()) *ResourceDefinition

NewResourceDefinition creates a resource definition but does not execute the DSL.

func (*ResourceDefinition) CanonicalAction

func (r *ResourceDefinition) CanonicalAction() *ActionDefinition

CanonicalAction returns the canonical action of the resource if any. The canonical action is used to compute hrefs to resources.

func (*ResourceDefinition) Context

func (r *ResourceDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*ResourceDefinition) FullPath

func (r *ResourceDefinition) FullPath() string

FullPath computes the base path to the resource actions concatenating the API and parent resource base paths as needed.

func (*ResourceDefinition) IterateActions

func (r *ResourceDefinition) IterateActions(it ActionIterator) error

IterateActions calls the given iterator passing in each resource action sorted in alphabetical order. Iteration stops if an iterator returns an error and in this case IterateActions returns that error.

func (*ResourceDefinition) Parent

Parent returns the parent resource if any, nil otherwise.

func (*ResourceDefinition) URITemplate

func (r *ResourceDefinition) URITemplate() string

URITemplate returns a httprouter compliant URI template to this resource. The result is the empty string if the resource does not have a "show" action and does not define a different canonical action.

func (*ResourceDefinition) Validate

func (r *ResourceDefinition) Validate() *ValidationErrors

Validate tests whether the resource definition is consistent: action names are valid and each action is valid.

type ResourceIterator

type ResourceIterator func(r *ResourceDefinition) error

ResourceIterator is the type of functions given to IterateResources.

type ResponseDefinition

type ResponseDefinition struct {
	// Response name
	Name string
	// HTTP status
	Status int
	// Response description
	Description string
	// Response body media type if any
	MediaType string
	// Response header definitions
	Headers *AttributeDefinition
	// Parent action or resource
	Parent DSLDefinition
	// Metadata is a list of key/value pairs
	Metadata MetadataDefinition
}

ResponseDefinition defines a HTTP response status and optional validation rules.

func (*ResponseDefinition) Context

func (r *ResponseDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*ResponseDefinition) Dup

Dup returns a copy of the response definition.

func (*ResponseDefinition) Merge

func (r *ResponseDefinition) Merge(other *ResponseDefinition)

Merge merges other into target. Only the fields of target that are not already set are merged.

func (*ResponseDefinition) Validate

func (r *ResponseDefinition) Validate() *ValidationErrors

Validate checks that the response definition is consistent: its status is set and the media type definition if any is valid.

type ResponseIterator

type ResponseIterator func(r *ResponseDefinition) error

ResponseIterator is the type of functions given to IterateResponses.

type ResponseTemplateDefinition

type ResponseTemplateDefinition struct {
	// Response template name
	Name string
	// Response template function
	Template func(params ...string) *ResponseDefinition
}

ResponseTemplateDefinition defines a response template. A response template is a function that takes an arbitrary number of strings and returns a response definition.

func (*ResponseTemplateDefinition) Context

func (r *ResponseTemplateDefinition) Context() string

Context returns the generic definition name used in error messages.

type RouteDefinition

type RouteDefinition struct {
	// Verb is the HTTP method, e.g. "GET", "POST", etc.
	Verb string
	// Path is the URL path e.g. "/tasks/:id"
	Path string
	// Parent is the action this route applies to.
	Parent *ActionDefinition
}

RouteDefinition represents an action route.

func (*RouteDefinition) Context

func (r *RouteDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*RouteDefinition) FullPath

func (r *RouteDefinition) FullPath() string

FullPath returns the action full path computed by concatenating the API and resource base paths with the action specific path.

func (*RouteDefinition) Params

func (r *RouteDefinition) Params() []string

Params returns the route parameters. For example for the route "GET /foo/:fooID" Params returns []string{"fooID"}.

func (*RouteDefinition) Validate

func (r *RouteDefinition) Validate() *ValidationErrors

Validate checks that the route definition is consistent: it has a parent.

type TraitDefinition

type TraitDefinition struct {
	// Trait name
	Name string
	// Trait DSL
	DSL func()
}

TraitDefinition defines a set of reusable properties.

func (*TraitDefinition) Context

func (t *TraitDefinition) Context() string

Context returns the generic definition name used in error messages.

type UserTypeDefinition

type UserTypeDefinition struct {
	// A user type is an attribute definition.
	*AttributeDefinition
	// Name of type
	TypeName string
	// DSL contains the DSL used to create this definition if any.
	DSL func()
}

UserTypeDefinition is the type for user defined types that are not media types (e.g. payload types).

func NewUserTypeDefinition

func NewUserTypeDefinition(name string, dsl func()) *UserTypeDefinition

NewUserTypeDefinition creates a user type definition but does not execute the DSL.

func (*UserTypeDefinition) Context

func (t *UserTypeDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*UserTypeDefinition) Dup

func (u *UserTypeDefinition) Dup() DataType

Dup returns a copy of u.

func (*UserTypeDefinition) IsArray

func (u *UserTypeDefinition) IsArray() bool

IsArray calls IsArray on the user type underlying data type.

func (*UserTypeDefinition) IsCompatible

func (u *UserTypeDefinition) IsCompatible(val interface{}) bool

IsCompatible returns true if val is compatible with p.

func (*UserTypeDefinition) IsHash

func (u *UserTypeDefinition) IsHash() bool

IsHash calls IsHash on the user type underlying data type.

func (*UserTypeDefinition) IsObject

func (u *UserTypeDefinition) IsObject() bool

IsObject calls IsObject on the user type underlying data type.

func (*UserTypeDefinition) IsPrimitive

func (u *UserTypeDefinition) IsPrimitive() bool

IsPrimitive calls IsPrimitive on the user type underlying data type.

func (*UserTypeDefinition) Kind

func (u *UserTypeDefinition) Kind() Kind

Kind implements DataKind.

func (*UserTypeDefinition) Name

func (u *UserTypeDefinition) Name() string

Name returns the JSON type name.

func (*UserTypeDefinition) ToArray

func (u *UserTypeDefinition) ToArray() *Array

ToArray calls ToArray on the user type underlying data type.

func (*UserTypeDefinition) ToHash

func (u *UserTypeDefinition) ToHash() *Hash

ToHash calls ToHash on the user type underlying data type.

func (*UserTypeDefinition) ToObject

func (u *UserTypeDefinition) ToObject() Object

ToObject calls ToObject on the user type underlying data type.

func (*UserTypeDefinition) Validate

func (u *UserTypeDefinition) Validate(ctx string, parent DSLDefinition) *ValidationErrors

Validate checks that the user type definition is consistent: it has a name.

type UserTypeIterator

type UserTypeIterator func(m *UserTypeDefinition) error

UserTypeIterator is the type of functions given to IterateUserTypes.

type ValidationDefinition

type ValidationDefinition interface {
	DSLDefinition
}

ValidationDefinition is the common interface for all validation data structures. It doesn't expose any method and simply exists to help with documentation.

type ValidationErrors

type ValidationErrors struct {
	Errors      []error
	Definitions []DSLDefinition
}

ValidationErrors records the errors encountered when running Validate.

func (*ValidationErrors) Add

func (verr *ValidationErrors) Add(def DSLDefinition, format string, vals ...interface{})

Add adds a validation error to the target. Add "flattens" validation errors so that the recorded errors are never ValidationErrors themselves.

func (*ValidationErrors) AsError

func (verr *ValidationErrors) AsError() *ValidationErrors

AsError returns an error if there are validation errors, nil otherwise.

func (*ValidationErrors) Error

func (verr *ValidationErrors) Error() string

Error implements the error interface.

func (*ValidationErrors) Merge

func (verr *ValidationErrors) Merge(err *ValidationErrors)

Merge merges validation errors into the target.

type ViewDefinition

type ViewDefinition struct {
	// Set of properties included in view
	*AttributeDefinition
	// Name of view
	Name string
	// Parent media Type
	Parent *MediaTypeDefinition
}

ViewDefinition defines which members and links to render when building a response. The view is a JSON object whose property names must match the names of the parent media type members. The members fields are inherited from the parent media type but may be overridden.

func (*ViewDefinition) Context

func (v *ViewDefinition) Context() string

Context returns the generic definition name used in error messages.

func (*ViewDefinition) Validate

func (v *ViewDefinition) Validate() *ValidationErrors

Validate checks that the view definition is consistent: it has a parent media type and the underlying definition type is consistent.

Directories

Path Synopsis
dsl
Package dsl implements the goa design language.
Package dsl implements the goa design language.
test
Package test contains a self-contained DSL test.
Package test contains a self-contained DSL test.

Jump to

Keyboard shortcuts

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