typutil

package module
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 19 Imported by: 11

README

GoDoc

type conversion utils

This is useful when dealing with json parsed types for example.

Assign

Assign is a tool that allows assigning any value to any other value and let the library handle the conversion in a somewhat intelligent way.

For example a map[string]any can be assigned to a struct (json tags will be taken into account for variable names) and values will be converted.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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")
)

Functions

func As added in v0.2.5

func As[T any](v any) (T, error)

func AsBool

func AsBool(v any) bool

AsBool loosely converts the value to a boolean

func AsByteArray added in v0.1.14

func AsByteArray(v any) ([]byte, bool)

AsByteArray will loosely convert the given value to a byte array

func AsFloat

func AsFloat(v any) (float64, bool)

AsFloat loosely converts the given value to a float64

func AsInt

func AsInt(v any) (int64, bool)

AsInt loosely converts the given value to a int64

func AsNumber

func AsNumber(v any) (any, bool)

AsNumber will loosely convert n in one of int64, uint64 or float64 depending on what feels best

func AsString added in v0.1.2

func AsString(v any) (string, bool)

AsString will loosely converts the given value to a string

func AsUint

func AsUint(v any) (uint64, bool)

AsUint loosely converts the given value to a uint64

func Assign added in v0.1.5

func Assign(dst, src any) error

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 AssignReflect(vdst, vsrc reflect.Value) error

func BaseType added in v0.1.4

func BaseType(v any) any

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 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

func DeepCloneReflect(src reflect.Value) reflect.Value

DeepCloneReflect performs a deep duplication of the provided reflect.Value

func Equal added in v0.1.17

func Equal(a, b any) bool

Equal returns true if a and b are somewhat equal

func Flatten added in v0.2.8

func Flatten(a any) any

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 IsNil added in v0.2.8

func IsNil(v any) bool

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

func Math(mathop string, a, b any) (any, bool)

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

func OffsetGet(ctx context.Context, v any, offset string) (any, error)

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

func SetValidator[T any](validator string, fnc func(T) error)

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 SetValidatorArgs(validator string, fnc any)

func ToType deprecated added in v0.1.17

func ToType(ref, v any) (any, bool)

ToType returns v converted to ref's type

Deprecated: Use As[T](v) instead

func Validate added in v0.1.10

func Validate(obj any) error

Validate accept any struct as argument and returns if the struct is valid. The parameter should be a pointer to the struct so validators can edit values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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