v0.0.0-...-3bb70b7 Latest Latest

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

Go to latest
Published: Feb 7, 2024 License: Apache-2.0 Imports: 21 Imported by: 3,583




View Source
const (
	ResourceFieldID = "id"


View Source
var (
	ASC  = SortOrder("asc")
	DESC = SortOrder("desc")


func GenerateName

func GenerateName(typeName string) string

func GenerateTypePrefix

func GenerateTypePrefix(typeName string) string

func JSONEncoder

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

func NewErrors

func NewErrors(inErrors ...error) error

func ValidMod

func ValidMod(mod ModifierType) bool

func YAMLEncoder

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


type APIContext

type APIContext struct {
	Action                      string
	ID                          string
	Type                        string
	Link                        string
	Method                      string
	Schema                      *Schema
	Schemas                     *Schemas
	Version                     *APIVersion
	SchemasVersion              *APIVersion
	Query                       url.Values
	ResponseFormat              string
	ReferenceValidator          ReferenceValidator
	ResponseWriter              ResponseWriter
	QueryFilter                 QueryFilter
	SubContextAttributeProvider SubContextAttributeProvider
	URLBuilder                  URLBuilder
	AccessControl               AccessControl
	SubContext                  map[string]string
	Pagination                  *Pagination

	Request  *http.Request
	Response http.ResponseWriter

func GetAPIContext

func GetAPIContext(ctx context.Context) *APIContext

func NewAPIContext

func NewAPIContext(req *http.Request, resp http.ResponseWriter, schemas *Schemas) *APIContext

func (*APIContext) ExpireAccessControl

func (r *APIContext) ExpireAccessControl(schema *Schema)

func (*APIContext) Filter

func (r *APIContext) Filter(opts *QueryOptions, schema *Schema, obj interface{}) interface{}

func (*APIContext) FilterList

func (r *APIContext) FilterList(opts *QueryOptions, schema *Schema, obj []map[string]interface{}) []map[string]interface{}

func (*APIContext) FilterObject

func (r *APIContext) FilterObject(opts *QueryOptions, schema *Schema, obj map[string]interface{}) map[string]interface{}

func (*APIContext) Option

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

func (*APIContext) WriteResponse

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

type APIVersion

type APIVersion struct {
	Group            string `json:"group,omitempty"`
	Version          string `json:"version,omitempty"`
	Path             string `json:"path,omitempty"`
	SubContext       bool   `json:"subContext,omitempty"`
	SubContextSchema string `json:"filterField,omitempty"`

func (*APIVersion) Equals

func (v *APIVersion) Equals(other *APIVersion) bool

type AccessControl

type AccessControl interface {
	CanCreate(apiContext *APIContext, schema *Schema) error
	CanList(apiContext *APIContext, schema *Schema) error
	CanGet(apiContext *APIContext, schema *Schema) error
	CanUpdate(apiContext *APIContext, obj map[string]interface{}, schema *Schema) error
	CanDelete(apiContext *APIContext, obj map[string]interface{}, schema *Schema) error
	// CanDo function should not yet be used if a corresponding specific method exists. It has been added to
	// satisfy a specific usecase for the short term until full-blown dynamic RBAC can be implemented.
	CanDo(apiGroup, resource, verb string, apiContext *APIContext, obj map[string]interface{}, schema *Schema) error

	Filter(apiContext *APIContext, schema *Schema, obj map[string]interface{}, context map[string]string) map[string]interface{}
	FilterList(apiContext *APIContext, schema *Schema, obj []map[string]interface{}, context map[string]string) []map[string]interface{}

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 *APIContext) error

type BackReference

type BackReference struct {
	FieldName string
	Schema    *Schema

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"`

func (*Collection) AddAction

func (c *Collection) AddAction(apiContext *APIContext, name string)

type CollectionFormatter

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

type Condition

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

type ErrorHandler

type ErrorHandler func(request *APIContext, 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 Expire

type Expire interface {
	Expire(apiContext *APIContext, schema *Schema)

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"`
	CodeName     string      `json:"-"`
	DynamicField bool        `json:"dynamicField,omitempty"`
	Pointer      bool        `json:"pointer,omitempty"`

type Filter

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

type Formatter

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

type GenericCollection

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

type InputFormatter

type InputFormatter func(request *APIContext, schema *Schema, data map[string]interface{}, create bool) error

type ListOpts

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

type Mapper

type Mapper interface {
	FromInternal(data map[string]interface{})
	ToInternal(data map[string]interface{}) error
	ModifySchema(schema *Schema, schemas *Schemas) error

type Mappers

type Mappers []Mapper

func (Mappers) FromInternal

func (m Mappers) FromInternal(data map[string]interface{})

func (Mappers) ModifySchema

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

func (Mappers) ToInternal

func (m Mappers) ToInternal(data map[string]interface{}) error

type MappersFactory

type MappersFactory func() []Mapper

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 {
	Name        string `json:"name"`
	Description string `json:"description"`

type NamedResourceCollection

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

type Namespaced

type Namespaced struct{}

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 []map[string]interface{}) []map[string]interface{}

type QueryOptions

type QueryOptions struct {
	Sort       Sort
	Pagination *Pagination
	Conditions []*QueryCondition
	Options    map[string]string
	// Set namespaces to an empty array will result in an empty response
	Namespaces []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(apiContext *APIContext, 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 *APIContext, next RequestHandler) 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 {
	Data []Resource `json:"data,omitempty"`

type ResponseWriter

type ResponseWriter interface {
	Write(apiContext *APIContext, code int, obj interface{})

type Schema

type Schema struct {
	ID                   string            `json:"id,omitempty"`
	Embed                bool              `json:"embed,omitempty"`
	EmbedType            string            `json:"embedType,omitempty"`
	CodeName             string            `json:"-"`
	CodeNamePlural       string            `json:"-"`
	PkgName              string            `json:"-"`
	Type                 string            `json:"type,omitempty"`
	BaseType             string            `json:"baseType,omitempty"`
	Links                map[string]string `json:"links"`
	Version              APIVersion        `json:"version"`
	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"`
	DynamicSchemaVersion string            `json:"dynamicSchemaVersion,omitempty"`
	Scope                TypeScope         `json:"-"`
	Enabled              func() bool       `json:"-"`

	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 *APIContext) error

func (*Schema) CanDelete

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

func (*Schema) CanGet

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

func (*Schema) CanList

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

func (*Schema) CanUpdate

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

func (*Schema) MustCustomizeField

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

type SchemaCollection

type SchemaCollection struct {
	Data []Schema

type SchemaHook

type SchemaHook func(*Schema)

type Schemas

type Schemas struct {

	DefaultMappers     MappersFactory
	DefaultPostMappers MappersFactory

	AddHook SchemaHook
	// contains filtered or unexported fields

func NewSchemas

func NewSchemas() *Schemas

func (*Schemas) AddMapper

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

func (*Schemas) AddMapperForType

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

func (*Schemas) AddSchema

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

func (*Schemas) AddSchemas

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

func (*Schemas) Err

func (s *Schemas) Err() error

func (*Schemas) ForceAddSchema

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

func (*Schemas) Import

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

func (*Schemas) Init

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

func (*Schemas) MustCustomizeType

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

func (*Schemas) MustImport

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

func (*Schemas) MustImportAndCustomize

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

func (*Schemas) References

func (s *Schemas) References(schema *Schema) []BackReference

func (*Schemas) RemoveSchema

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

func (*Schemas) Schema

func (s *Schemas) Schema(version *APIVersion, name string) *Schema

func (*Schemas) Schemas

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

func (*Schemas) SchemasForVersion

func (s *Schemas) SchemasForVersion(version APIVersion) map[string]*Schema

func (*Schemas) SubContextVersionForSchema

func (s *Schemas) SubContextVersionForSchema(schema *Schema) *APIVersion

func (*Schemas) TypeName

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

func (*Schemas) Versions

func (s *Schemas) Versions() []APIVersion

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 StorageContext

type StorageContext string
var DefaultStorageContext StorageContext

type Store

type Store interface {
	Context() StorageContext
	ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
	List(apiContext *APIContext, schema *Schema, opt *QueryOptions) ([]map[string]interface{}, error)
	Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
	Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
	Delete(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
	Watch(apiContext *APIContext, schema *Schema, opt *QueryOptions) (chan map[string]interface{}, error)

type SubContextAttributeProvider

type SubContextAttributeProvider interface {
	Query(apiContext *APIContext, schema *Schema) []*QueryCondition
	Create(apiContext *APIContext, schema *Schema) map[string]interface{}

type TypeScope

type TypeScope string
var NamespaceScope TypeScope = "namespace"

type URLBuilder

type URLBuilder interface {
	Current() string
	Collection(schema *Schema, versionOverride *APIVersion) string
	CollectionAction(schema *Schema, versionOverride *APIVersion, action string) string
	SubContextCollection(subContext *Schema, contextName string, schema *Schema) string
	SchemaLink(schema *Schema) string
	ResourceLink(resource *RawResource) string
	Link(linkName string, resource *RawResource) string
	RelativeToRoot(path string) string
	Version(version APIVersion) string
	Marker(marker string) string
	ReverseSort(order SortOrder) string
	Sort(field string) string
	SetSubContext(subContext string)
	FilterLink(schema *Schema, fieldName string, value string) string
	Action(action string, resource *RawResource) string
	ResourceLinkByID(schema *Schema, id string) string
	ActionLinkByID(schema *Schema, id string, action string) string

type Validator

type Validator func(request *APIContext, schema *Schema, data map[string]interface{}) error

type ValuesMap

type ValuesMap struct {
	Foo map[string]interface{}


Path Synopsis

Jump to

Keyboard shortcuts

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