Documentation ¶
Index ¶
- Variables
- func HasValue(field reflect.Value) bool
- type ErrInvalidTag
- type ErrInvalidTransformValue
- type ErrInvalidTransformation
- type ErrUndefinedTag
- type FieldLevel
- type Func
- type InterceptorFunc
- type StructLevel
- type StructLevelFunc
- type Transform
- type Transformer
- func (t *Transformer) Field(ctx context.Context, v interface{}, tags string) (err error)
- func (t *Transformer) Register(tag string, fn Func)
- func (t *Transformer) RegisterAlias(alias, tags string)
- func (t *Transformer) RegisterInterceptor(fn InterceptorFunc, types ...interface{})
- func (t *Transformer) RegisterStructLevel(fn StructLevelFunc, types ...interface{})
- func (t *Transformer) SetTagName(tagName string)
- func (t *Transformer) Struct(ctx context.Context, v interface{}) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidDive describes an invalid dive tag configuration ErrInvalidDive = errors.New("invalid dive tag configuration") // ErrUndefinedKeysTag describes an undefined keys tag when and endkeys tag defined ErrUndefinedKeysTag = errors.New("'" + endKeysTag + "' tag encountered without a corresponding '" + keysTag + "' tag") // ErrInvalidKeysTag describes a misuse of the keys tag ErrInvalidKeysTag = errors.New("'" + keysTag + "' tag must be immediately preceeded by the '" + diveTag + "' tag") )
Functions ¶
Types ¶
type ErrInvalidTag ¶
type ErrInvalidTag struct {
// contains filtered or unexported fields
}
ErrInvalidTag defines a bad value for a tag being used
func (*ErrInvalidTag) Error ¶
func (e *ErrInvalidTag) Error() string
Error returns the InvalidTag error text
type ErrInvalidTransformValue ¶
type ErrInvalidTransformValue struct {
// contains filtered or unexported fields
}
An ErrInvalidTransformValue describes an invalid argument passed to Struct or Var. (The argument passed must be a non-nil pointer.)
func (*ErrInvalidTransformValue) Error ¶
func (e *ErrInvalidTransformValue) Error() string
type ErrInvalidTransformation ¶
type ErrInvalidTransformation struct {
// contains filtered or unexported fields
}
ErrInvalidTransformation describes an invalid argument passed to `Struct` or `Field`
func (*ErrInvalidTransformation) Error ¶
func (e *ErrInvalidTransformation) Error() string
Error returns ErrInvalidTransformation message
type ErrUndefinedTag ¶
type ErrUndefinedTag struct {
// contains filtered or unexported fields
}
ErrUndefinedTag defines a tag that does not exist
func (*ErrUndefinedTag) Error ¶
func (e *ErrUndefinedTag) Error() string
Error returns the UndefinedTag error text
type FieldLevel ¶
type FieldLevel interface { // Transformer represents a subset of the current *Transformer that is executing the current transformation. Transformer() Transform // // Parent returns the top level parent of the current value return by Field() // // This is used primarily for having the ability to nil out pointer type values. // // NOTE: that is there are several layers of abstractions eg. interface{} of interface{} of interface{} this // function returns the first interface{} // Parent() reflect.Value // Field returns the current field value being modified. Field() reflect.Value // Param returns the param associated wth the given function modifier. Param() string }
FieldLevel represents the interface for field level modifier function
type Func ¶
type Func func(ctx context.Context, fl FieldLevel) error
Func defines a transform function for use.
type InterceptorFunc ¶ added in v4.1.0
InterceptorFunc is a way to intercept custom types to redirect the functions to be applied to an inner typ/value. eg. sql.NullString, the manipulation should be done on the inner string.
type StructLevel ¶
type StructLevel interface { // Transformer represents a subset of the current *Transformer that is executing the current transformation. Transformer() Transform // // Parent returns the top level parent of the current value return by Struct(). // // This is used primarily for having the ability to nil out pointer type values. // // NOTE: that is there are several layers of abstractions eg. interface{} of interface{} of interface{} this // function returns the first interface{}. // Parent() reflect.Value // Struct returns the value of the current struct being modified. Struct() reflect.Value }
StructLevel represents the interface for struct level modifier function
type StructLevelFunc ¶
type StructLevelFunc func(ctx context.Context, sl StructLevel) error
StructLevelFunc accepts all values needed for struct level manipulation.
Why does this exist? For structs for which you may not have access or rights to add tags too, from other packages your using.
type Transform ¶
type Transform interface { Struct(ctx context.Context, v interface{}) error Field(ctx context.Context, v interface{}, tags string) error }
Transform represents a subset of the current *Transformer that is executing the current transformation.
type Transformer ¶
type Transformer struct {
// contains filtered or unexported fields
}
Transformer is the base controlling object which contains all necessary information
func New ¶
func New() *Transformer
New creates a new Transform object with default tag name of 'mold'
func (*Transformer) Field ¶
func (t *Transformer) Field(ctx context.Context, v interface{}, tags string) (err error)
Field applies the provided transformations against the variable
func (*Transformer) Register ¶
func (t *Transformer) Register(tag string, fn Func)
Register adds a transformation with the given tag
NOTES: - if the key already exists, the previous transformation function will be replaced. - this method is not thread-safe it is intended that these all be registered before hand
func (*Transformer) RegisterAlias ¶
func (t *Transformer) RegisterAlias(alias, tags string)
RegisterAlias registers a mapping of a single transform tag that defines a common or complex set of transformations to simplify adding transforms to structs.
NOTE: this function is not thread-safe it is intended that these all be registered before hand
func (*Transformer) RegisterInterceptor ¶ added in v4.1.0
func (t *Transformer) RegisterInterceptor(fn InterceptorFunc, types ...interface{})
RegisterInterceptor registers a new interceptor functions agains one or more types. This InterceptorFunc allows one to intercept the incoming to to redirect the application of modifications to an inner type/value.
eg. sql.NullString
func (*Transformer) RegisterStructLevel ¶
func (t *Transformer) RegisterStructLevel(fn StructLevelFunc, types ...interface{})
RegisterStructLevel registers a StructLevelFunc against a number of types. Why does this exist? For structs for which you may not have access or rights to add tags too, from other packages your using.
NOTES: - this method is not thread-safe it is intended that these all be registered prior to any validation
func (*Transformer) SetTagName ¶
func (t *Transformer) SetTagName(tagName string)
SetTagName sets the given tag name to be used. Default is "trans"