resource

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 16 Imported by: 28

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrProcessorSkipLeft = errors.New("resource: skip left")

ErrProcessorSkipLeft skip left processors error, if returned this error in validation, before callbacks, then qor will stop process following processors

Functions

func Decode

func Decode(context *qor.Context, result interface{}, res Resourcer) error

Decode decode context to result according to resource definition

func DecodeToResource

func DecodeToResource(res Resourcer, result interface{}, metaValues *MetaValues, context *qor.Context) *processor

DecodeToResource decode meta values to resource result

Types

type ConfigureMetaBeforeInitializeInterface

type ConfigureMetaBeforeInitializeInterface interface {
	ConfigureQorMetaBeforeInitialize(Metaor)
}

ConfigureMetaBeforeInitializeInterface if a struct's field's type implemented this interface, it will be called when initializing a meta

type ConfigureMetaInterface

type ConfigureMetaInterface interface {
	ConfigureQorMeta(Metaor)
}

ConfigureMetaInterface if a struct's field's type implemented this interface, it will be called after configed

type ConfigureResourceBeforeInitializeInterface

type ConfigureResourceBeforeInitializeInterface interface {
	ConfigureQorResourceBeforeInitialize(Resourcer)
}

ConfigureResourceBeforeInitializeInterface if a struct implemented this interface, it will be called before everything when create a resource with the struct

type ConfigureResourceInterface

type ConfigureResourceInterface interface {
	ConfigureQorResource(Resourcer)
}

ConfigureResourceInterface if a struct implemented this interface, it will be called after configured by user

type Meta

type Meta struct {
	Name            string
	FieldName       string
	FieldStruct     *gorm.StructField
	Setter          func(resource interface{}, metaValue *MetaValue, context *qor.Context)
	Valuer          func(interface{}, *qor.Context) interface{}
	FormattedValuer func(interface{}, *qor.Context) interface{}
	Config          MetaConfigInterface
	BaseResource    Resourcer
	Resource        Resourcer
	Permission      *roles.Permission
}

Meta meta struct definition

func (Meta) GetBaseResource

func (meta Meta) GetBaseResource() Resourcer

GetBaseResource get base resource from meta

func (Meta) GetFieldName

func (meta Meta) GetFieldName() string

GetFieldName get meta's field name

func (*Meta) GetFormattedValuer

func (meta *Meta) GetFormattedValuer() func(interface{}, *qor.Context) interface{}

GetFormattedValuer get formatted valuer from meta

func (Meta) GetName

func (meta Meta) GetName() string

GetName get meta's name

func (Meta) GetSetter

func (meta Meta) GetSetter() func(resource interface{}, metaValue *MetaValue, context *qor.Context)

GetSetter get setter from meta

func (Meta) GetValuer

func (meta Meta) GetValuer() func(interface{}, *qor.Context) interface{}

GetValuer get valuer from meta

func (Meta) HasPermission

func (meta Meta) HasPermission(mode roles.PermissionMode, context *qor.Context) bool

HasPermission check has permission or not

func (*Meta) Initialize

func (meta *Meta) Initialize() error

Initialize initialize meta, will set valuer, setter if haven't configure it

func (*Meta) PreInitialize

func (meta *Meta) PreInitialize() error

PreInitialize when will be run before initialize, used to fill some basic necessary information

func (*Meta) SetFieldName

func (meta *Meta) SetFieldName(name string)

SetFieldName set meta's field name

func (*Meta) SetFormattedValuer

func (meta *Meta) SetFormattedValuer(fc func(interface{}, *qor.Context) interface{})

SetFormattedValuer set formatted valuer for meta

func (*Meta) SetPermission

func (meta *Meta) SetPermission(permission *roles.Permission)

SetPermission set permission for meta

func (*Meta) SetSetter

func (meta *Meta) SetSetter(fc func(resource interface{}, metaValue *MetaValue, context *qor.Context))

SetSetter set setter to meta

func (*Meta) SetValuer

func (meta *Meta) SetValuer(fc func(interface{}, *qor.Context) interface{})

SetValuer set valuer for meta

type MetaConfig

type MetaConfig struct {
}

MetaConfig base meta config struct

func (MetaConfig) ConfigureQorMeta

func (MetaConfig) ConfigureQorMeta(Metaor)

ConfigureQorMeta implement the MetaConfigInterface

type MetaConfigInterface

type MetaConfigInterface interface {
	ConfigureMetaInterface
}

MetaConfigInterface meta configuration interface

type MetaValue

type MetaValue struct {
	Name       string
	Value      interface{}
	Index      int
	Meta       Metaor
	MetaValues *MetaValues
}

MetaValue a struct used to hold information when convert inputs from HTTP form, JSON, CSV files and so on to meta values It will includes field name, field value and its configured Meta, if it is a nested resource, will includes nested metas in its MetaValues

type MetaValues

type MetaValues struct {
	Values []*MetaValue
}

MetaValues is slice of MetaValue

func ConvertFormToMetaValues

func ConvertFormToMetaValues(request *http.Request, metaors []Metaor, prefix string) (*MetaValues, error)

ConvertFormToMetaValues convert form to meta values

func ConvertJSONToMetaValues

func ConvertJSONToMetaValues(reader io.Reader, metaors []Metaor) (*MetaValues, error)

ConvertJSONToMetaValues convert json to meta values

func (MetaValues) Get

func (mvs MetaValues) Get(name string) *MetaValue

Get get meta value from MetaValues with name

type Metaor

type Metaor interface {
	GetName() string
	GetFieldName() string
	GetSetter() func(resource interface{}, metaValue *MetaValue, context *qor.Context)
	GetFormattedValuer() func(interface{}, *qor.Context) interface{}
	GetValuer() func(interface{}, *qor.Context) interface{}
	GetResource() Resourcer
	GetMetas() []Metaor
	SetPermission(*roles.Permission)
	HasPermission(roles.PermissionMode, *qor.Context) bool
}

Metaor interface

type Processor

type Processor struct {
	Name    string
	Handler func(interface{}, *MetaValues, *qor.Context) error
}

Processor processor struct

type Resource

type Resource struct {
	Name            string
	Value           interface{}
	PrimaryFields   []*gorm.StructField
	FindManyHandler func(interface{}, *qor.Context) error
	FindOneHandler  func(interface{}, *MetaValues, *qor.Context) error
	SaveHandler     func(interface{}, *qor.Context) error
	DeleteHandler   func(interface{}, *qor.Context) error
	Permission      *roles.Permission
	Validators      []*Validator
	Processors      []*Processor
	// contains filtered or unexported fields
}

Resource is a struct that including basic definition of qor resource

func New

func New(value interface{}) *Resource

New initialize qor resource

func (*Resource) AddProcessor

func (res *Resource) AddProcessor(processor *Processor)

AddProcessor add processor to resource, it is used to process data before creating, updating, will rollback the change if it return any error

func (*Resource) AddValidator

func (res *Resource) AddValidator(validator *Validator)

AddValidator add validator to resource, it will invoked when creating, updating, and will rollback the change if validator return any error

func (*Resource) CallDelete

func (res *Resource) CallDelete(result interface{}, context *qor.Context) error

CallDelete call delete method

func (*Resource) CallFindMany

func (res *Resource) CallFindMany(result interface{}, context *qor.Context) error

CallFindMany call find many method

func (*Resource) CallFindOne

func (res *Resource) CallFindOne(result interface{}, metaValues *MetaValues, context *qor.Context) error

CallFindOne call find one method

func (*Resource) CallSave

func (res *Resource) CallSave(result interface{}, context *qor.Context) error

CallSave call save method

func (*Resource) GetMetas

func (res *Resource) GetMetas([]string) []Metaor

GetMetas get defined metas, to match interface `Resourcer`

func (*Resource) GetResource

func (res *Resource) GetResource() *Resource

GetResource return itself to match interface `Resourcer`

func (*Resource) HasPermission

func (res *Resource) HasPermission(mode roles.PermissionMode, context *qor.Context) bool

HasPermission check permission of resource

func (*Resource) NewSlice

func (res *Resource) NewSlice() interface{}

NewSlice initialize a slice of struct for the Resource

func (*Resource) NewStruct

func (res *Resource) NewStruct() interface{}

NewStruct initialize a struct for the Resource

func (*Resource) SetPrimaryFields

func (res *Resource) SetPrimaryFields(fields ...string) error

SetPrimaryFields set primary fields

func (*Resource) ToPrimaryQueryParams

func (res *Resource) ToPrimaryQueryParams(primaryValue string, context *qor.Context) (string, []interface{})

ToPrimaryQueryParams generate query params based on primary key, multiple primary value are linked with a comma

func (*Resource) ToPrimaryQueryParamsFromMetaValue

func (res *Resource) ToPrimaryQueryParamsFromMetaValue(metaValues *MetaValues, context *qor.Context) (string, []interface{})

ToPrimaryQueryParamsFromMetaValue generate query params based on MetaValues

type Resourcer

type Resourcer interface {
	GetResource() *Resource
	GetMetas([]string) []Metaor
	CallFindMany(interface{}, *qor.Context) error
	CallFindOne(interface{}, *MetaValues, *qor.Context) error
	CallSave(interface{}, *qor.Context) error
	CallDelete(interface{}, *qor.Context) error
	NewSlice() interface{}
	NewStruct() interface{}
}

Resourcer interface

type Validator

type Validator struct {
	Name    string
	Handler func(interface{}, *MetaValues, *qor.Context) error
}

Validator validator struct

Jump to

Keyboard shortcuts

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