Back to godoc.org

Package spec

v1.60.0
Latest Go to latest

The highest tagged major version is .

Published: Jun 25, 2020 | License: Apache-2.0 | Module: go.aporeto.io/regolithe

Overview

Package spec contains the Go representation of an Regolithe Specification Set as well as various functions to handle reading and writing them.

Index

func Pluralize

func Pluralize(word string) string

Pluralize pluralizes the given word.

type APIInfo

type APIInfo struct {
	Prefix  string `yaml:"prefix,omitempty"     json:"prefix,omitempty"`
	Root    string `yaml:"root,omitempty"       json:"root,omitempty"`
	Version int    `yaml:"version,omitempty"    json:"version,omitempty"`
}

An APIInfo holds general information about the API.

func LoadAPIInfo

func LoadAPIInfo(path string) (*APIInfo, error)

LoadAPIInfo loads an APIInfo from the given file.

func NewAPIInfo

func NewAPIInfo() *APIInfo

NewAPIInfo returns a new APIInfo.

func (*APIInfo) Validate

func (a *APIInfo) Validate() []error

Validate validates the api info against the schema.

type Attribute

type Attribute struct {
	Name                string                 `yaml:"name,omitempty"                   json:"name,omitempty"`
	ExposedName         string                 `yaml:"exposed_name,omitempty"           json:"exposed_name,omitempty"`
	Description         string                 `yaml:"description,omitempty"            json:"description,omitempty"`
	Type                AttributeType          `yaml:"type,omitempty"                   json:"type,omitempty"`
	Exposed             bool                   `yaml:"exposed,omitempty"                json:"exposed,omitempty"`
	SubType             string                 `yaml:"subtype,omitempty"                json:"subtype,omitempty"`
	Stored              bool                   `yaml:"stored,omitempty"                 json:"stored,omitempty"`
	Required            bool                   `yaml:"required,omitempty"               json:"required,omitempty"`
	ReadOnly            bool                   `yaml:"read_only,omitempty"              json:"read_only,omitempty"`
	CreationOnly        bool                   `yaml:"creation_only,omitempty"          json:"creation_only,omitempty"`
	AllowedChars        string                 `yaml:"allowed_chars,omitempty"          json:"allowed_chars,omitempty"`
	AllowedCharsMessage string                 `yaml:"allowed_chars_message,omitempty"  json:"allowed_chars_message,omitempty"`
	AllowedChoices      []string               `yaml:"allowed_choices,omitempty"        json:"allowed_choices,omitempty"`
	Autogenerated       bool                   `yaml:"autogenerated,omitempty"          json:"autogenerated,omitempty"`
	DefaultValue        interface{}            `yaml:"default_value,omitempty"          json:"default_value,omitempty"`
	Deprecated          bool                   `yaml:"deprecated,omitempty"             json:"deprecated,omitempty"`
	ExampleValue        interface{}            `yaml:"example_value,omitempty"          json:"example_value,omitempty"`
	Filterable          bool                   `yaml:"filterable,omitempty"             json:"filterable,omitempty"`
	ForeignKey          bool                   `yaml:"foreign_key,omitempty"            json:"foreign_key,omitempty"`
	Getter              bool                   `yaml:"getter,omitempty"                 json:"getter,omitempty"`
	Setter              bool                   `yaml:"setter,omitempty"                 json:"setter,omitempty"`
	Identifier          bool                   `yaml:"identifier,omitempty"             json:"identifier,omitempty"`
	MaxLength           uint16                 `yaml:"max_length,omitempty"             json:"max_length,omitempty"`
	MinLength           uint16                 `yaml:"min_length,omitempty"             json:"min_length,omitempty"`
	MaxValue            float64                `yaml:"max_value,omitempty"              json:"max_value,omitempty"`
	MinValue            float64                `yaml:"min_value,omitempty"              json:"min_value,omitempty"`
	Orderable           bool                   `yaml:"orderable,omitempty"              json:"orderable,omitempty"`
	PrimaryKey          bool                   `yaml:"primary_key,omitempty"            json:"primary_key,omitempty"`
	Secret              bool                   `yaml:"secret,omitempty"                 json:"secret,omitempty"`
	Transient           bool                   `yaml:"transient,omitempty"              json:"transient,omitempty"`
	OmitEmpty           bool                   `yaml:"omit_empty,omitempty"             json:"omit_empty,omitempty"`
	Encrypted           bool                   `yaml:"encrypted,omitempty"              json:"encrypted,omitempty"`
	Validations         []string               `yaml:"validations,omitempty"            json:"validations,omitempty"`
	Extensions          map[string]interface{} `yaml:"extensions,omitempty"             json:"extensions,omitempty"`

	ConvertedName       string                    `yaml:"-" json:"-"`
	ConvertedType       string                    `yaml:"-" json:"-"`
	TypeProvider        string                    `yaml:"-" json:"-"`
	Initializer         string                    `yaml:"-" json:"-"`
	ValidationProviders map[string]*ValidationMap `yaml:"-" json:"-"`
	// contains filtered or unexported fields
}

An Attribute represents a regolithe specification attribute.

func (*Attribute) Validate

func (a *Attribute) Validate() []error

Validate validates the attribute definition.

type AttributeNameConverterFunc

type AttributeNameConverterFunc func(name string) string

AttributeNameConverterFunc is the type of a attribute name conveter.

type AttributeType

type AttributeType string

AttributeType represents the various type for an attribute.

const (
	AttributeTypeString  AttributeType = "string"
	AttributeTypeInt     AttributeType = "integer"
	AttributeTypeFloat   AttributeType = "float"
	AttributeTypeBool    AttributeType = "boolean"
	AttributeTypeEnum    AttributeType = "enum"
	AttributeTypeList    AttributeType = "list"
	AttributeTypeObject  AttributeType = "object"
	AttributeTypeTime    AttributeType = "time"
	AttributeTypeExt     AttributeType = "external"
	AttributeTypeRef     AttributeType = "ref"
	AttributeTypeRefList AttributeType = "refList"
	AttributeTypeRefMap  AttributeType = "refMap"
)

Various values for AttributeType.

type AttributeTypeConverterFunc

type AttributeTypeConverterFunc func(typ AttributeType, subtype string) (converted string, provider string)

AttributeTypeConverterFunc is the type of a attribute type conveter.

type Config

type Config struct {
	Author      string
	Copyright   string
	Description string
	Email       string
	Name        string
	ProductName string
	URL         string
	Version     string
	// contains filtered or unexported fields
}

Config holds the Specification Config.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig loads the config from an ini file.

func NewConfig

func NewConfig() *Config

NewConfig returns a new APIInfo.

func (*Config) Key

func (c *Config) Key(section, key string) string

Key returns the value of the given key in the given section.

type Model

type Model struct {
	RestName      string                 `yaml:"rest_name,omitempty"       json:"rest_name,omitempty"`
	ResourceName  string                 `yaml:"resource_name,omitempty"   json:"resource_name,omitempty"`
	EntityName    string                 `yaml:"entity_name,omitempty"     json:"entity_name,omitempty"`
	Package       string                 `yaml:"package,omitempty"         json:"package,omitempty"`
	Group         string                 `yaml:"group,omitempty"           json:"group,omitempty"`
	Description   string                 `yaml:"description,omitempty"     json:"description,omitempty"`
	Documentation string                 `yaml:"documentation,omitempty"   json:"documentation,omitempty"`
	Aliases       []string               `yaml:"aliases,omitempty"         json:"aliases,omitempty"`
	Private       bool                   `yaml:"private,omitempty"         json:"private,omitempty"`
	Get           *RelationAction        `yaml:"get,omitempty"             json:"get,omitempty"`
	Update        *RelationAction        `yaml:"update,omitempty"          json:"update,omitempty"`
	Delete        *RelationAction        `yaml:"delete,omitempty"          json:"delete,omitempty"`
	Extends       []string               `yaml:"extends,omitempty"         json:"extends,omitempty"`
	IsRoot        bool                   `yaml:"root,omitempty"            json:"root,omitempty"`
	Detached      bool                   `yaml:"detached,omitempty"        json:"detached,omitempty"`
	Validations   []string               `yaml:"validations,omitempty"     json:"validations,omitempty"`
	Extensions    map[string]interface{} `yaml:"extensions,omitempty"      json:"extensions,omitempty"`

	EntityNamePlural string `yaml:"-" json:"-"`
}

A Model holds generic information about a specification.

func (*Model) Validate

func (m *Model) Validate() []error

Validate validates the Model.

type Parameter

type Parameter struct {
	Name           string        `yaml:"name,omitempty"              json:"name,omitempty"`
	Description    string        `yaml:"description,omitempty"       json:"description,omitempty"`
	Type           ParameterType `yaml:"type,omitempty"              json:"type,omitempty"`
	Multiple       bool          `yaml:"multiple,omitempty"          json:"multiple,omitempty"`
	AllowedChoices []string      `yaml:"allowed_choices,omitempty"   json:"allowed_choices,omitempty"`
	DefaultValue   interface{}   `yaml:"default_value,omitempty"     json:"default_value,omitempty"`
	ExampleValue   interface{}   `yaml:"example_value,omitempty"     json:"example_value,omitempty"`
}

A Parameter represent one parameter that can be sent with a query

func (*Parameter) Validate

func (p *Parameter) Validate(relatedReSTName string) []error

Validate validates the parameter definition.

type ParameterDefinition

type ParameterDefinition struct {
	Required [][][]string `yaml:"required,omitempty"    json:"required,omitempty"`
	Entries  []*Parameter `yaml:"entries,omitempty"     json:"entries,omitempty"`
}

ParameterDefinition represents a parameter definition.

func (*ParameterDefinition) Validate

func (p *ParameterDefinition) Validate(relatedReSTName string) []error

Validate validates the parameter definition.

type ParameterMapping

type ParameterMapping map[string]*ParameterDefinition

A ParameterMapping is a list parameter mapping

func LoadGlobalParameters

func LoadGlobalParameters(path string) (ParameterMapping, error)

LoadGlobalParameters loads the global parameters file.

func NewParameterMapping

func NewParameterMapping() ParameterMapping

NewParameterMapping returns a new ParameterMapping.

func (ParameterMapping) Read

func (p ParameterMapping) Read(reader io.Reader, validate bool) (err error)

Read loads a validation mapping from the given io.Reader

func (ParameterMapping) Validate

func (p ParameterMapping) Validate() []error

Validate the ParameterMapping

func (ParameterMapping) Write

func (p ParameterMapping) Write(writer io.Writer) error

Write dumps the specification into a []byte.

type ParameterType

type ParameterType string

ParameterType represents the various type for a parameter.

const (
	ParameterTypeString   ParameterType = "string"
	ParameterTypeInt      ParameterType = "integer"
	ParameterTypeFloat    ParameterType = "float"
	ParameterTypeBool     ParameterType = "boolean"
	ParameterTypeTime     ParameterType = "time"
	ParameterTypeEnum     ParameterType = "enum"
	ParameterTypeDuration ParameterType = "duration"
)

Various values for ParameterType.

type Relation

type Relation struct {
	RestName string          `yaml:"rest_name,omitempty"    json:"rest_name,omitempty"`
	Get      *RelationAction `yaml:"get,omitempty"          json:"get,omitempty"`
	Create   *RelationAction `yaml:"create,omitempty"       json:"create,omitempty"`
	Update   *RelationAction `yaml:"update,omitempty"       json:"update,omitempty"`
	Delete   *RelationAction `yaml:"delete,omitempty"       json:"delete,omitempty"`
	// contains filtered or unexported fields
}

An Relation represents a specification Relation.

func (*Relation) Specification

func (r *Relation) Specification() Specification

Specification returns the Specification the API links to.

func (*Relation) Validate

func (r *Relation) Validate() []error

Validate validates the relationship

type RelationAction

type RelationAction struct {
	Description         string               `yaml:"description,omitempty"           json:"description,omitempty"`
	Deprecated          bool                 `yaml:"deprecated,omitempty"            json:"deprecated,omitempty"`
	ParameterReferences []string             `yaml:"global_parameters,omitempty"     json:"global_parameters,omitempty"`
	ParameterDefinition *ParameterDefinition `yaml:"parameters,omitempty"            json:"parameters,omitempty"`
}

A RelationAction represents one the the possible action

func (*RelationAction) Validate

func (ra *RelationAction) Validate(currentRestName string, remoteRestName string, k string) []error

Validate validates the relation action.

type Relationship

type Relationship struct {
	Create  map[string]*RelationAction
	Delete  map[string]*RelationAction
	Get     map[string]*RelationAction
	GetMany map[string]*RelationAction
	Update  map[string]*RelationAction
}

A Relationship describes the hierarchical relationship of the models.

func NewRelationship

func NewRelationship() *Relationship

NewRelationship returns a new Relationship.

func (*Relationship) Set

func (r *Relationship) Set(action string, name string, ra *RelationAction)

Set sets the names that are allows to do the given action.

type Specification

type Specification interface {

	// Read reads and load the given reader containing a specification.
	// If validates is true, validations will be done.
	Read(reader io.Reader, validate bool) error

	// Writes write the current state of the Specification in the given
	// writer.
	Write(writer io.Writer) error

	// Validate validates the specification content.
	Validate() []error

	// ApplyBaseSpecifications applyes the given abstract specification to
	// the specification.
	ApplyBaseSpecifications(specs ...Specification) error

	// Model returns the Specification model.
	Model() *Model

	// Attribute returns the attribute with the given name in the given version.
	Attribute(name string, version string) *Attribute

	// Attributes returns all attributes for the given version.
	Attributes(version string) []*Attribute

	// ExposedAttributes returns only the exposed attributes in the given version.
	ExposedAttributes(version string) []*Attribute

	// DefaultOrder returns the default ordering of the spec.
	DefaultOrder() []string

	// AttributeVersions returns all the versions of attributes.
	AttributeVersions() []string

	// LatestAttributesVersion returns the latest version of the attributes.
	LatestAttributesVersion() string

	// Relations returns the Specification relations.
	Relations() []*Relation

	// Relation returns the relation to the given restName.
	Relation(restName string) *Relation

	// Identitier returns the Attribute used as an identifier.
	Identifier() *Attribute
}

A Specification is the interface representing a Regolithe Specification.

func LoadSpecification

func LoadSpecification(specPath string, validate bool) (Specification, error)

LoadSpecification returns a new specification using the given file path.

func NewSpecification

func NewSpecification() Specification

NewSpecification returns a new specification.

type SpecificationSet

type SpecificationSet interface {

	// Specification returns the Specification with the given name.
	Specification(name string) Specification

	// SpecificationGroup returns the Specifications in the given group name.
	SpecificationGroup(groupName string) []Specification

	// Specifications returns all Specifications.
	Specifications() (specs []Specification)

	// Len returns the number of specifications in the set.
	Len() int

	// Relationships is better
	Relationships() map[string]*Relationship

	// RelationshipsByRestName returns the relationships indexed by rest name.
	RelationshipsByRestName() map[string]*Relationship

	// RelationshipsByResourceName returns the relationships indexed by resource name.
	RelationshipsByResourceName() map[string]*Relationship

	// Configuration returns the specification set Config.
	Configuration() *Config

	// TypeMapping returns the specification set TypeMapping.
	TypeMapping() TypeMapping

	// ValidationMapping returns the specification set ValidationMapping.
	ValidationMapping() ValidationMapping

	// APIInfo returns the specification set APIInfo.
	APIInfo() *APIInfo

	// Groups returns the list of group names.
	Groups() []string
}

SpecificationSet represents an entire set of specification.

func LoadSpecificationSet

func LoadSpecificationSet(
	dirname string,
	nameConvertFunc AttributeNameConverterFunc,
	typeConvertFunc AttributeTypeConverterFunc,
	typeMappingName string,
) (SpecificationSet, error)

LoadSpecificationSet loads and parses all specification in a folder.

func LoadSpecificationSetFromGithub

func LoadSpecificationSetFromGithub(
	token string,
	repoURL string,
	refName string,
	internalPath string,
	nameConvertFunc AttributeNameConverterFunc,
	typeConvertFunc AttributeTypeConverterFunc,
	typeMappingName string,
) (SpecificationSet, error)

LoadSpecificationSetFromGithub loads a set of specs from github.

type TypeMap

type TypeMap struct {
	Type        string `yaml:"type,omitempty"           json:"type,omitempty"`
	Initializer string `yaml:"init,omitempty"           json:"init,omitempty"`
	Import      string `yaml:"import,omitempty"         json:"import,omitempty"`
	Description string `yaml:"description,omitempty"    json:"description,omitempty"`
}

A TypeMap represent a single Type Map.

type TypeMapping

type TypeMapping map[string]map[string]*TypeMap

TypeMapping holds the mapping of the external types.

func LoadTypeMapping

func LoadTypeMapping(path string) (TypeMapping, error)

LoadTypeMapping loads a TypeMapping from the given ini file.

func NewTypeMapping

func NewTypeMapping() TypeMapping

NewTypeMapping returns a new TypeMapping.

func (TypeMapping) All

func (t TypeMapping) All(mode string) (mapping []*TypeMap)

All returns the all the TypeMap for the given mode.

func (TypeMapping) Mapping

func (t TypeMapping) Mapping(mode string, externalType string) (mapping *TypeMap, err error)

Mapping returns the TypeMap for the given external type.

func (TypeMapping) Read

func (t TypeMapping) Read(reader io.Reader, validate bool) (err error)

Read loads a type mapping from the given io.Reader

func (TypeMapping) Validate

func (t TypeMapping) Validate() []error

Validate validates the type mappings against the schema.

func (TypeMapping) Write

func (t TypeMapping) Write(writer io.Writer) error

Write dumps the specification into a []byte.

type ValidationMap

type ValidationMap struct {
	Name   string `yaml:"name,omitempty"           json:"name,omitempty"`
	Import string `yaml:"import,omitempty"         json:"import,omitempty"`
}

A ValidationMap represent a single ValidationMap.

type ValidationMapping

type ValidationMapping map[string]map[string]*ValidationMap

ValidationMapping holds the mapping of the validation function.

func LoadValidationMapping

func LoadValidationMapping(path string) (ValidationMapping, error)

LoadValidationMapping loads a ValidationMapping from the given ini file.

func NewValidationMapping

func NewValidationMapping() ValidationMapping

NewValidationMapping returns a new ValidationMapping.

func (ValidationMapping) Mapping

func (v ValidationMapping) Mapping(mode string, functionName string) (mapping *ValidationMap, err error)

Mapping returns the ValidationMap for the given external type.

func (ValidationMapping) Read

func (v ValidationMapping) Read(reader io.Reader, validate bool) (err error)

Read loads a validation mapping from the given io.Reader

func (ValidationMapping) Validate

func (v ValidationMapping) Validate() []error

Validate validates the type mappings against the schema.

func (ValidationMapping) Write

func (v ValidationMapping) Write(writer io.Writer) error

Write dumps the specification into a []byte.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier