linject

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FieldName = lerr.Must(filter.Regex(`\p{Lu}(\p{L}|\p{N}|_)*`))
)

Functions

This section is empty.

Types

type Callback

type Callback func([]reflect.Value)

type Field

type Field struct {
	FieldName string
	FieldInitilizer
	FuncType, FieldType filter.Type
}

FieldInitilizer fulfills Initilizer. It contains the logic for validating the FieldName exists and getting that field from the magic data type. This simplifies the process of creating midware that sets a value on one field.

func NewField

func NewField(fi FieldInitilizer, fieldName string) Field

NewField with the given values.

func NewFieldSetter

func NewFieldSetter(setter any, fieldName string, funcType, fieldType filter.Type) Field

func (Field) Initilize

func (fs Field) Initilize(fn FuncType) Injector

Initilize fulfills Initilizer and validates the FieldName is not blank and checks if magic data type 't' has the field. If the field exists a DataInserter is created that will invoke the FieldSetter on that field.

type FieldInitilizer

type FieldInitilizer interface {
	InitilizeField(FuncType, reflect.Type) FieldInjector
}

FieldInitilizer is called by FieldInitilizer to handle any Type Reflection necessary in creating a FieldSetter. If no Type Reflection is necessary, a single type can fulfill FieldSetter, then FieldSetterInitilizer can just return itself.

type FieldInjector

type FieldInjector interface {
	InjectField(args []reflect.Value, field reflect.Value) (callback func([]reflect.Value), err error)
}

FieldInjector will set the value on 'set' generally from the request. This abstracts away the process of extracting a field from the magic data type.

func NewFieldInjector

func NewFieldInjector(fn any) FieldInjector

NewFieldInjector takes a function and converts it to a field setter. The function must have 3 returns. The first is the value the field will be set to. The second is the callback function and the third is an error.

The arguments can either have a single argument of []reflect.Value in which case the arguments will be passed along. Or if it expects a specific argument pattern for the function, it can match the leading arguments. For instance, a FieldSetter on an HttpHandler could have arguments of (w http.ResponseWriter, r *http.Request)

type FuncType

type FuncType interface {
	// Fn is the function type
	Fn() reflect.Type
	// DataType is a helper. The last argument of the function is a pointer
	// to a struct, DataType is that struct.
	Target() reflect.Type
	// contains filtered or unexported methods
}

FuncType is a helper when invoking a FuncInitilizer. All that is necessisary for a FuncInitilizer is the function type but so much logic is dedicated to inspecting the Target that it's convenient to provide that seperatly.

type InitilizedFunc

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

InitilizedFunc wraps a function so that a set of FuncInjector will run before calling the function and any callbacks will run after it returns.

func (*InitilizedFunc) Call

func (ifn *InitilizedFunc) Call(args []reflect.Value) []reflect.Value

Call the InitilizedFunc, running the each FuncInjector before the wrapped function is called and then invoking the callbacks when the wrapped function returns.

func (*InitilizedFunc) Fn

func (ft *InitilizedFunc) Fn() reflect.Type

func (*InitilizedFunc) Interface

func (ifn *InitilizedFunc) Interface() any

Interface uses reflection to create a function without the target argument. This can be type cast to the function type and invoked as a normal function.

func (*InitilizedFunc) Target

func (ft *InitilizedFunc) Target() reflect.Type

type Initilizer

type Initilizer interface {
	Initilize(FuncType) Injector
}

Initilizer represents a single injector. It inspects the FuncType to correctly apply the Injector. If the injection logic does not apply to the particular FuncType then nil is returned and the injector is not used with the given function. The injector may also use this initilization stage to dynamically change the logic in the returned FuncInjector rather than performing repetitive inspections on each invocation.

type Initilizers

type Initilizers []Initilizer

FuncInitilizers allows a slice of FuncInitilizer to be applied to a function. All FuncInitilizer that return a non-nil value will be wrapped with the function to create an InitilizedFunc.

func (Initilizers) Apply

func (fis Initilizers) Apply(fn any) *InitilizedFunc

Apply the FuncInitilizers to fn. If fn is not a valid function, nil is returned.

type Injector

type Injector interface {
	Inject(args []reflect.Value) (callback func(rets []reflect.Value), err error)
}

Injector holds injection logic that is invoked before a function is called. The args argument holds the values that will be passed into the function that is the subject of the injection. If a callback is returned, then the functions return values will be passed into the callback.

type Injectors

type Injectors []Injector

func (Injectors) Inject

func (injs Injectors) Inject(args []reflect.Value) (callbacks []Callback, err error)

Jump to

Keyboard shortcuts

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