types

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResourceFieldID = "id"
)

Variables

View Source
var (
	ASC  = SortOrder("asc")
	DESC = SortOrder("desc")
)
View Source
var (
	ChangeAPIEvent = "resource.change"
	RemoveAPIEvent = "resource.remove"
	CreateAPIEvent = "resource.create"
)

Functions

func APIChan

func APIChan(c <-chan APIEvent, f func(APIObject) APIObject) chan APIEvent

func GenerateName

func GenerateName(typeName string) string

func JSONEncoder

func JSONEncoder(writer io.Writer, v interface{}) error

func Name

func Name(data map[string]interface{}) string

func Namespace

func Namespace(data map[string]interface{}) string

func NewErrors

func NewErrors(inErrors ...error) error

func ValidMod

func ValidMod(mod ModifierType) bool

func YAMLEncoder

func YAMLEncoder(writer io.Writer, v interface{}) error

Types

type APIEvent

type APIEvent struct {
	Name         string    `json:"name,omitempty"`
	ResourceType string    `json:"resourceType,omitempty"`
	Revision     string    `json:"revision,omitempty"`
	Object       APIObject `json:"-"`
	Error        error     `json:"-"`
	// Data should be used
	Data interface{} `json:"data,omitempty"`
}

type APIObject

type APIObject struct {
	ListRevision string      `json:"-"`
	Object       interface{} `json:",inline"`
}

func ToAPI

func ToAPI(data interface{}) APIObject

func (*APIObject) First

func (a *APIObject) First() APIObject

func (*APIObject) IsList

func (a *APIObject) IsList() bool

func (APIObject) IsNil

func (a APIObject) IsNil() bool

func (*APIObject) List

func (a *APIObject) List() data.List

func (*APIObject) ListCheck

func (a *APIObject) ListCheck() (data.List, bool)

func (*APIObject) Map

func (a *APIObject) Map() data.Object

func (*APIObject) Name

func (a *APIObject) Name() string

func (*APIObject) Namespace

func (a *APIObject) Namespace() string

func (*APIObject) Raw

func (a *APIObject) Raw() interface{}

type APIRequest

type APIRequest struct {
	Action             string
	Name               string
	Type               string
	Link               string
	Method             string
	Namespaces         []string
	Schema             *Schema
	Schemas            *Schemas
	Query              url.Values
	ResponseFormat     string
	ReferenceValidator ReferenceValidator
	ResponseWriter     ResponseWriter
	QueryFilter        QueryFilter
	URLPrefix          string
	URLBuilder         URLBuilder
	AccessControl      AccessControl
	Pagination         *Pagination

	Request  *http.Request
	Response http.ResponseWriter
}

func (*APIRequest) Context

func (r *APIRequest) Context() context.Context

func (*APIRequest) Filter

func (r *APIRequest) Filter(opts *QueryOptions, schema *Schema, obj APIObject) APIObject

func (*APIRequest) FilterList

func (r *APIRequest) FilterList(opts *QueryOptions, schema *Schema, obj APIObject) APIObject

func (*APIRequest) FilterObject

func (r *APIRequest) FilterObject(opts *QueryOptions, schema *Schema, obj APIObject) APIObject

func (*APIRequest) GetUser

func (r *APIRequest) GetUser() string

func (*APIRequest) GetUserInfo

func (r *APIRequest) GetUserInfo() (user.Info, bool)

func (*APIRequest) Option

func (r *APIRequest) Option(key string) string

func (*APIRequest) WithContext

func (r *APIRequest) WithContext(ctx context.Context) *APIRequest

func (*APIRequest) WriteResponse

func (r *APIRequest) WriteResponse(code int, obj interface{})

type AccessControl

type AccessControl interface {
	CanCreate(apiOp *APIRequest, schema *Schema) error
	CanList(apiOp *APIRequest, schema *Schema) error
	CanGet(apiOp *APIRequest, schema *Schema) error
	CanUpdate(apiOp *APIRequest, obj APIObject, schema *Schema) error
	CanDelete(apiOp *APIRequest, obj APIObject, schema *Schema) error
	CanWatch(apiOp *APIRequest, schema *Schema) error
}

type Action

type Action struct {
	Input  string `json:"input,omitempty"`
	Output string `json:"output,omitempty"`
}

type ActionHandler

type ActionHandler func(actionName string, action *Action, request *APIRequest) error

type Collection

type Collection struct {
	Type         string                 `json:"type,omitempty"`
	Links        map[string]string      `json:"links"`
	CreateTypes  map[string]string      `json:"createTypes,omitempty"`
	Actions      map[string]string      `json:"actions"`
	Pagination   *Pagination            `json:"pagination,omitempty"`
	Sort         *Sort                  `json:"sort,omitempty"`
	Filters      map[string][]Condition `json:"filters,omitempty"`
	ResourceType string                 `json:"resourceType"`
	Revision     string                 `json:"revision,omitempty"`
}

func (*Collection) AddAction

func (c *Collection) AddAction(apiOp *APIRequest, name string)

type CollectionFormatter

type CollectionFormatter func(request *APIRequest, collection *GenericCollection)

type Condition

type Condition struct {
	Modifier ModifierType `json:"modifier,omitempty"`
	Value    interface{}  `json:"value,omitempty"`
}

type EmptyMapper

type EmptyMapper struct {
}

func (*EmptyMapper) FromInternal

func (e *EmptyMapper) FromInternal(data data.Object)

func (*EmptyMapper) ModifySchema

func (e *EmptyMapper) ModifySchema(schema *Schema, schemas *Schemas) error

func (*EmptyMapper) ToInternal

func (e *EmptyMapper) ToInternal(data data.Object) error

type ErrorHandler

type ErrorHandler func(request *APIRequest, err error)

type Errors

type Errors struct {
	// contains filtered or unexported fields
}

func (*Errors) Add

func (e *Errors) Add(err error)

func (*Errors) Err

func (e *Errors) Err() error

type Field

type Field struct {
	Type         string      `json:"type,omitempty"`
	Default      interface{} `json:"default,omitempty"`
	Nullable     bool        `json:"nullable,omitempty"`
	Create       bool        `json:"create"`
	WriteOnly    bool        `json:"writeOnly,omitempty"`
	Required     bool        `json:"required,omitempty"`
	Update       bool        `json:"update"`
	MinLength    *int64      `json:"minLength,omitempty"`
	MaxLength    *int64      `json:"maxLength,omitempty"`
	Min          *int64      `json:"min,omitempty"`
	Max          *int64      `json:"max,omitempty"`
	Options      []string    `json:"options,omitempty"`
	ValidChars   string      `json:"validChars,omitempty"`
	InvalidChars string      `json:"invalidChars,omitempty"`
	Description  string      `json:"description,omitempty"`
	Dynamic      bool        `json:"dynamic,omitempty"`
	CodeName     string      `json:"-"`
}

type FieldMapperFactory

type FieldMapperFactory func(fieldName string, args ...string) Mapper

type Filter

type Filter struct {
	Modifiers []ModifierType `json:"modifiers,omitempty"`
}

type Formatter

type Formatter func(request *APIRequest, resource *RawResource)

func FormatterChain

func FormatterChain(formatter Formatter, next Formatter) Formatter

type GenericCollection

type GenericCollection struct {
	Collection
	Data []interface{} `json:"data"`
}

type InputFormatter

type InputFormatter func(request *APIRequest, schema *Schema, data APIObject, create bool) error

type ListOpts

type ListOpts struct {
	Filters map[string]interface{}
}

type Mapper

type Mapper interface {
	FromInternal(data data.Object)
	ToInternal(data data.Object) error
	ModifySchema(schema *Schema, schemas *Schemas) error
}

type MapperFactory

type MapperFactory func() Mapper

type Mappers

type Mappers []Mapper

func (Mappers) FromInternal

func (m Mappers) FromInternal(data data.Object)

func (Mappers) ModifySchema

func (m Mappers) ModifySchema(schema *Schema, schemas *Schemas) error

func (Mappers) ToInternal

func (m Mappers) ToInternal(data data.Object) error

type ModifierType

type ModifierType string
var (
	ModifierEQ      ModifierType = "eq"
	ModifierNE      ModifierType = "ne"
	ModifierNull    ModifierType = "null"
	ModifierNotNull ModifierType = "notnull"
	ModifierIn      ModifierType = "in"
	ModifierNotIn   ModifierType = "notin"
)

type MultiErrors

type MultiErrors struct {
	Errors []error
}

func (*MultiErrors) Error

func (m *MultiErrors) Error() string

type NamedResource

type NamedResource struct {
	Resource
	Name        string `json:"name"`
	Description string `json:"description"`
}

type NamedResourceCollection

type NamedResourceCollection struct {
	Collection
	Data []NamedResource `json:"data,omitempty"`
}

type Pagination

type Pagination struct {
	Marker   string `json:"marker,omitempty"`
	First    string `json:"first,omitempty"`
	Previous string `json:"previous,omitempty"`
	Next     string `json:"next,omitempty"`
	Last     string `json:"last,omitempty"`
	Limit    *int64 `json:"limit,omitempty"`
	Total    *int64 `json:"total,omitempty"`
	Partial  bool   `json:"partial,omitempty"`
}

type QueryCondition

type QueryCondition struct {
	Field  string
	Value  string
	Values map[string]bool
	// contains filtered or unexported fields
}

func EQ

func EQ(key, value string) *QueryCondition

func NewConditionFromString

func NewConditionFromString(field string, mod ModifierType, values ...string) *QueryCondition

func (*QueryCondition) ToCondition

func (q *QueryCondition) ToCondition() Condition

func (*QueryCondition) Valid

func (q *QueryCondition) Valid(schema *Schema, data map[string]interface{}) bool

type QueryConditionType

type QueryConditionType struct {
	Name ModifierType
	Args int
}

type QueryFilter

type QueryFilter func(opts *QueryOptions, schema *Schema, data APIObject) APIObject

type QueryOptions

type QueryOptions struct {
	Sort       Sort
	Pagination *Pagination
	Conditions []*QueryCondition
	Options    map[string]string
}

type RawResource

type RawResource struct {
	ID           string                 `json:"id,omitempty" yaml:"id,omitempty"`
	Type         string                 `json:"type,omitempty" yaml:"type,omitempty"`
	Schema       *Schema                `json:"-" yaml:"-"`
	Links        map[string]string      `json:"links,omitempty" yaml:"links,omitempty"`
	Actions      map[string]string      `json:"actions,omitempty" yaml:"actions,omitempty"`
	Values       map[string]interface{} `json:",inline" yaml:",inline"`
	ActionLinks  bool                   `json:"-" yaml:"-"`
	DropReadOnly bool                   `json:"-" yaml:"-"`
}

func (*RawResource) AddAction

func (r *RawResource) AddAction(apiOp *APIRequest, name string)

func (*RawResource) MarshalJSON

func (r *RawResource) MarshalJSON() ([]byte, error)

func (*RawResource) ToMap

func (r *RawResource) ToMap() map[string]interface{}

type ReferenceValidator

type ReferenceValidator interface {
	Validate(resourceType, resourceID string) bool
	Lookup(resourceType, resourceID string) *RawResource
}

type RequestHandler

type RequestHandler func(request *APIRequest) (APIObject, error)

type Resource

type Resource struct {
	ID      string            `json:"id,omitempty"`
	Type    string            `json:"type,omitempty"`
	Links   map[string]string `json:"links"`
	Actions map[string]string `json:"actions"`
}

type ResourceCollection

type ResourceCollection struct {
	Collection
	Data []Resource `json:"data,omitempty"`
}

type ResponseWriter

type ResponseWriter interface {
	Write(apiOp *APIRequest, code int, obj interface{})
}

type Schema

type Schema struct {
	ID                string                 `json:"id,omitempty"`
	Description       string                 `json:"description,omitempty"`
	CodeName          string                 `json:"-"`
	CodeNamePlural    string                 `json:"-"`
	PkgName           string                 `json:"-"`
	Type              string                 `json:"type,omitempty"`
	Links             map[string]string      `json:"links"`
	PluralName        string                 `json:"pluralName,omitempty"`
	ResourceMethods   []string               `json:"resourceMethods,omitempty"`
	ResourceFields    map[string]Field       `json:"resourceFields"`
	ResourceActions   map[string]Action      `json:"resourceActions,omitempty"`
	CollectionMethods []string               `json:"collectionMethods,omitempty"`
	CollectionFields  map[string]Field       `json:"collectionFields,omitempty"`
	CollectionActions map[string]Action      `json:"collectionActions,omitempty"`
	CollectionFilters map[string]Filter      `json:"collectionFilters,omitempty"`
	Attributes        map[string]interface{} `json:"attributes,omitempty"`
	Dynamic           bool                   `json:"dynamic,omitempty"`

	InternalSchema      *Schema             `json:"-"`
	Mapper              Mapper              `json:"-"`
	ActionHandler       ActionHandler       `json:"-"`
	LinkHandler         RequestHandler      `json:"-"`
	ListHandler         RequestHandler      `json:"-"`
	CreateHandler       RequestHandler      `json:"-"`
	DeleteHandler       RequestHandler      `json:"-"`
	UpdateHandler       RequestHandler      `json:"-"`
	InputFormatter      InputFormatter      `json:"-"`
	Formatter           Formatter           `json:"-"`
	CollectionFormatter CollectionFormatter `json:"-"`
	ErrorHandler        ErrorHandler        `json:"-"`
	Validator           Validator           `json:"-"`
	Store               Store               `json:"-"`
}

func (*Schema) CanCreate

func (s *Schema) CanCreate(context *APIRequest) error

func (*Schema) CanDelete

func (s *Schema) CanDelete(context *APIRequest) error

func (*Schema) CanGet

func (s *Schema) CanGet(context *APIRequest) error

func (*Schema) CanList

func (s *Schema) CanList(context *APIRequest) error

func (*Schema) CanUpdate

func (s *Schema) CanUpdate(context *APIRequest) error

func (*Schema) DeepCopy

func (s *Schema) DeepCopy() *Schema

func (*Schema) MustCustomizeField

func (s *Schema) MustCustomizeField(name string, f func(f Field) Field) *Schema

type SchemaCollection

type SchemaCollection struct {
	Data []Schema
}

type Schemas

type Schemas struct {
	sync.Mutex

	DefaultMapper     MapperFactory
	DefaultPostMapper MapperFactory
	// contains filtered or unexported fields
}

func EmptySchemas

func EmptySchemas() *Schemas

func NewSchemas

func NewSchemas(schemas ...*Schemas) (*Schemas, error)

func (*Schemas) AddFieldMapper

func (s *Schemas) AddFieldMapper(name string, factory FieldMapperFactory) *Schemas

func (*Schemas) AddMapper

func (s *Schemas) AddMapper(schemaID string, mapper Mapper) *Schemas

func (*Schemas) AddMapperForType

func (s *Schemas) AddMapperForType(obj interface{}, mapper ...Mapper) *Schemas

func (*Schemas) AddSchema

func (s *Schemas) AddSchema(schema Schema) error

func (*Schemas) AddSchemas

func (s *Schemas) AddSchemas(schema *Schemas) (*Schemas, error)

func (*Schemas) Import

func (s *Schemas) Import(obj interface{}, externalOverrides ...interface{}) (*Schema, error)

func (*Schemas) Init

func (s *Schemas) Init(initFunc SchemasInitFunc) *Schemas

func (*Schemas) MustAddSchema

func (s *Schemas) MustAddSchema(schema Schema) *Schemas

func (*Schemas) MustAddSchemas

func (s *Schemas) MustAddSchemas(schema *Schemas) *Schemas

func (*Schemas) MustCustomizeType

func (s *Schemas) MustCustomizeType(obj interface{}, f func(*Schema)) *Schemas

func (*Schemas) MustImport

func (s *Schemas) MustImport(obj interface{}, externalOverrides ...interface{}) *Schemas

func (*Schemas) MustImportAndCustomize

func (s *Schemas) MustImportAndCustomize(obj interface{}, f func(*Schema), externalOverrides ...interface{}) *Schemas

func (*Schemas) RemoveSchema

func (s *Schemas) RemoveSchema(schema Schema) *Schemas

func (*Schemas) Schema

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

func (*Schemas) SchemaFor

func (s *Schemas) SchemaFor(t reflect.Type) *Schema

func (*Schemas) Schemas

func (s *Schemas) Schemas() []*Schema

func (*Schemas) SchemasByID

func (s *Schemas) SchemasByID() map[string]*Schema

func (*Schemas) TypeName

func (s *Schemas) TypeName(name string, obj interface{}) *Schemas

type SchemasInitFunc

type SchemasInitFunc func(*Schemas) *Schemas

type Sort

type Sort struct {
	Name    string            `json:"name,omitempty"`
	Order   SortOrder         `json:"order,omitempty"`
	Reverse string            `json:"reverse,omitempty"`
	Links   map[string]string `json:"links,omitempty"`
}

type SortOrder

type SortOrder string

type Store

type Store interface {
	ByID(apiOp *APIRequest, schema *Schema, id string) (APIObject, error)
	List(apiOp *APIRequest, schema *Schema, opt *QueryOptions) (APIObject, error)
	Create(apiOp *APIRequest, schema *Schema, data APIObject) (APIObject, error)
	Update(apiOp *APIRequest, schema *Schema, data APIObject, id string) (APIObject, error)
	Delete(apiOp *APIRequest, schema *Schema, id string) (APIObject, error)
	Watch(apiOp *APIRequest, schema *Schema, w WatchRequest) (chan APIEvent, error)
}

type URLBuilder

type URLBuilder interface {
	Current() string

	Collection(schema *Schema) string
	CollectionAction(schema *Schema, action string) string
	ResourceLink(schema *Schema, id string) string
	Link(schema *Schema, id string, linkName string) string
	FilterLink(schema *Schema, fieldName string, value string) string
	Action(schema *Schema, id string, action string) string

	RelativeToRoot(path string) string
	Marker(marker string) string
	ReverseSort(order SortOrder) string
	Sort(field string) string
}

type Validator

type Validator func(request *APIRequest, schema *Schema, data APIObject) error

type WatchRequest

type WatchRequest struct {
	Revision string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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