resource

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: MIT Imports: 18 Imported by: 203

Documentation

Index

Constants

View Source
const CompositePrimaryKeyFieldName = "CompositePrimaryKeyField"

CompositePrimaryKey the string that represents the composite primary key

View Source
const CompositePrimaryKeySeparator = "^|^"

CompositePrimaryKeySeparator to separate composite primary keys like ID and version_name

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

func GenCompositePrimaryKey

func GenCompositePrimaryKey(id interface{}, versionName string) string

GenCompositePrimaryKey generates composite primary key in a specific format

func HandleBelongsTo

func HandleBelongsTo(context *qor.Context, record reflect.Value, field reflect.Value, relationship *gorm.Relationship, primaryKeys []string)

func HandleManyToMany

func HandleManyToMany(context *qor.Context, scope *gorm.Scope, meta *Meta, record interface{}, metaValue *MetaValue, field reflect.Value, fieldHasVersion bool)

func HandleNormalManyToMany

func HandleNormalManyToMany(context *qor.Context, field reflect.Value, metaValue *MetaValue, fieldHasVersion bool, compositePKeyConvertErr error)

HandleNormalManyToMany not only handle normal many_to_many relationship, it also handled the situation that user set the association to blank

func HandleVersionedManyToMany

func HandleVersionedManyToMany(context *qor.Context, field reflect.Value, compositePKeys []CompositePrimaryKeyStruct)

HandleVersionedManyToMany handle id+version_name composite primary key, query and set the correct result to the "Many" field e.g. Collection.Products This doesn't handle compositePKeys is blank logic, if it is, this function should not be invoked

func HandleVersioningBelongsTo

func HandleVersioningBelongsTo(context *qor.Context, record reflect.Value, field reflect.Value, relationship *gorm.Relationship, primaryKeys []string, fieldHasVersion bool)

Types

type CompositePrimaryKeyField

type CompositePrimaryKeyField struct {
	CompositePrimaryKey string `gorm:"-"`
}

CompositePrimaryKeyField to embed into the struct that requires composite primary key in select many

type CompositePrimaryKeyStruct

type CompositePrimaryKeyStruct struct {
	ID          uint   `json:"id"`
	VersionName string `json:"version_name"`
}

CompositePrimaryKeyStruct container for store id & version combination temporarily

func CollectPrimaryKeys

func CollectPrimaryKeys(metaValueForCompositePrimaryKeys []string) (compositePKeys []CompositePrimaryKeyStruct, compositePKeyConvertErr error)

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