Documentation ¶
Index ¶
- Constants
- Variables
- func As[T any](v any) (T, error)
- func AsBool(v any) bool
- func AsByteArray(v any) ([]byte, bool)
- func AsFloat(v any) (float64, bool)
- func AsInt(v any) (int64, bool)
- func AsNumber(v any) (any, bool)
- func AsString(v any) (string, bool)
- func AsUint(v any) (uint64, bool)
- func Assign(dst, src any) error
- func AssignReflect(vdst, vsrc reflect.Value) error
- func BaseType(v any) any
- func Call[T any](s *Callable, ctx context.Context, arg ...any) (T, error)
- func DeepClone[T any](v T) T
- func DeepCloneReflect(src reflect.Value) reflect.Value
- func Equal(a, b any) bool
- func Flatten(a any) any
- func FormatSize(x uint64) string
- func IsNil(v any) bool
- func Math(mathop string, a, b any) (any, bool)
- func OffsetGet(ctx context.Context, v any, offset string) (any, error)
- func SetValidator[T any](validator string, fnc func(T) error)
- func SetValidatorArgs(validator string, fnc any)
- func StrictArgs(c *Callable)
- func ToType(ref, v any) (any, bool)deprecated
- func Validate(obj any) error
- type AssignableTo
- type Callable
- func (s *Callable) ArgKind(n int) reflect.Kind
- func (s *Callable) Call(ctx context.Context) (any, error)
- func (s *Callable) CallArg(ctx context.Context, arg ...any) (any, error)
- func (s *Callable) IsStringArg(n int) bool
- func (s *Callable) String() string
- func (s *Callable) WithDefaults(args ...any) *Callable
- type RawJsonMessage
- type Signed
- type Unsigned
Constants ¶
const Required requiredArg = 1 // Required denotes a default argument that must be specified
Variables ¶
var ( ErrAssignDestNotPointer = errors.New("assign destination must be a pointer") ErrAssignImpossible = errors.New("the requested assign is not possible") ErrNilPointerRead = errors.New("attempt to read from a nil pointer") ErrEmptyValue = errors.New("validator: value must not be empty") ErrDestinationNotAddressable = errors.New("assign: destination cannot be addressed") ErrStructPtrRequired = errors.New("parameter must be a pointer to a struct") ErrBadOffset = errors.New("bad offset type") ErrMissingArgs = errors.New("missing arguments") ErrTooManyArgs = errors.New("too many arguments") ErrDifferentType = errors.New("wrong type in function call") ErrInvalidSource = errors.New("assign source is not valid") )
Functions ¶
func AsByteArray ¶ added in v0.1.14
AsByteArray will loosely convert the given value to a byte array
func AsNumber ¶
AsNumber will loosely convert n in one of int64, uint64 or float64 depending on what feels best
func Assign ¶ added in v0.1.5
Assign sets dst to the value of src. In case src is any kind of container, a shallow copy is performed
func AssignReflect ¶ added in v0.1.19
func BaseType ¶ added in v0.1.4
BaseType attempts to convert v into its base type, that is if v is a type that is defined as `type foo string`, a simple string will be returned.
func Call ¶ added in v0.2.19
Call will call the callable with the provided arguments, and cast the return type to the specified type automatically. If the return type is not of the correct type, an error will be returned, unless there was already an error.
func DeepClone ¶ added in v0.2.9
func DeepClone[T any](v T) T
DeepClone performs a deep duplication of the provided argument, and returns the newly created object
func DeepCloneReflect ¶ added in v0.2.9
DeepCloneReflect performs a deep duplication of the provided reflect.Value
func Flatten ¶ added in v0.2.8
Flatten transforms a into a simple interface, even if it is contains multiple levels. *string becomes string (or nil if it's nil), etc
func FormatSize ¶ added in v0.2.22
FormatSize is a simple method that will format a given integer value into something easier to read as a human
func IsNil ¶ added in v0.2.8
IsNil recursively checks if v is nil, even if it is a pointer of an interface of a ...
func Math ¶ added in v0.1.16
Math performs a mathematical operation on two variables of any type and returns a numeric type and true if everything went fine.
For example Math("+", 40.000, "2") will return float64(42)
For now if passed any float value, the function will always return a float value in the end. This may change in the future.
func OffsetGet ¶ added in v0.2.6
OffsetGet returns v[offset] dealing with various case of figure. ctx will be passed to some methods handling it
func SetValidator ¶ added in v0.1.6
SetValidator sets the given function as validator with the given name. This should be typically called in init()
func SetValidatorArgs ¶ added in v0.1.13
func StrictArgs ¶ added in v0.2.24
func StrictArgs(c *Callable)
StrictArgs, when passed to Func, forces that function's arguments to be of the right type
Types ¶
type AssignableTo ¶ added in v0.2.18
AssignableTo is an interface that can be implemented by objects able to assign themselves to values. Do not use Assign() inside AssignTo or you risk generating an infinite loop.
This looks a bit like sql's Valuer, except instead of a returning a any interface, the parameter is a pointer to the target type. Typically, Unmarshal will be used in here.
type Callable ¶ added in v0.2.19
type Callable struct {
// contains filtered or unexported fields
}
func Func ¶ added in v0.2.19
Func returns a Callable object for a func that accepts a context.Context and/or any number of arguments
func (*Callable) ArgKind ¶ added in v0.2.19
ArgKind returns the kind for the nth argument. reflect.Invalid will be returned if there is no such argument
func (*Callable) Call ¶ added in v0.2.19
Call invokes the func without any argument. If the func expects some kind of argument, Call will attempt to get input_json from the context, and if obtained it will be parsed and passed as argument to the method.
func (*Callable) CallArg ¶ added in v0.2.19
CallArg calls the method with the specified arguments. If less arguments are provided than required, an error will be raised.
func (*Callable) IsStringArg ¶ added in v0.2.19
IsStringArg returns true if the nth argument of the callable is a string, or a type related to string
func (*Callable) String ¶ added in v0.2.25
String returns a string representation of the function and its arguments
func (*Callable) WithDefaults ¶ added in v0.2.23
WithDefaults sets default arguments for the given callable that will be used when a call doesn't have enough arguments to call the method. It will panic if there aren't enough arguments provided or if a value is not compatible with the function's expected argument type.
Example:
func myFunc(a, b, c int) { ... } f := Func(myFunc).WithDefaults(typutil.Required, typutil.Required, 42) // c defaults to 42 f.CallArg(context.Background(), 10, 20) // equivalent to myFunc(10, 20, 42)
type RawJsonMessage ¶ added in v0.2.18
type RawJsonMessage []byte
RawJsonMessage is similar to json.RawMessage, but also implements
func (RawJsonMessage) AssignTo ¶ added in v0.2.18
func (m RawJsonMessage) AssignTo(v any) error
AssignTo will unmarshal the raw json message into v
func (RawJsonMessage) MarshalJSON ¶ added in v0.2.18
func (m RawJsonMessage) MarshalJSON() ([]byte, error)
func (*RawJsonMessage) UnmarshalJSON ¶ added in v0.2.18
func (m *RawJsonMessage) UnmarshalJSON(data []byte) error