Documentation
¶
Index ¶
- func AddFallbacks[From any, To any](registry Registry, fromToFunc func(From, *To) error)
- func AddHandler[From any, To any](registry Registry, fromToFunc func(From, *To) error)
- func AddHandlerWithReturn[From any, To any](registry Registry, toFunc func(From) (To, error))
- func AddHandlerWithReturnNoError[From any, To any](registry Registry, toFunc func(From) To)
- func CastOrErr[T any](value any) (result T, err error)
- func To[T any](registry Converters, f any) (T, error)
- type Converters
- type ConvertersList
- type FromToFunc
- type FromToTypedFunc
- type Handler
- type Registry
- type ToFunc
- type ToTypedFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFallbacks ¶
func AddHandler ¶
func AddHandlerWithReturn ¶
Types ¶
type Converters ¶
type Converters interface { Convert(from any, to any) error ConvertToType(from any, toType reflect.Type) (any, error) }
Converters is an entity that knows how to convert one value type into another value type.
func NewConvertersList ¶
func NewConvertersList(converters ...Converters) Converters
NewConvertersList returns a Converters implementation that uses the given list of converters to find a function to convert values. If no Converters is provided, the the converter.Defauldefault is used
type ConvertersList ¶
type ConvertersList []Converters
ConvertersList when you have more than one registry and want to use the first one that works
func (ConvertersList) ConvertToType ¶
type FromToFunc ¶
FromToFunc is a dynamic type conversion function
func MakeFromToFunc ¶
type FromToTypedFunc ¶
FromToTypedFunc is a strongly typed conversion function from a given F type value to the given T type that should be assigned to the given pointer to T. The given pointer cannot be nil since it should have to receive the converted value.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a data structure that provides input type definitions to invoke a FromToFunc function
func CreateHandlerWithReturn ¶
func (Handler) GetFromToFunc ¶
func (h Handler) GetFromToFunc() FromToFunc
func (Handler) GetFromType ¶
type Registry ¶
type Registry interface { // Add adds a new converter to the registry. If there is already one, then the old one is returned and replaced by the new one. Add(handler Handler) *Handler // GetConverter returns the FromToFunc to be used to convert a `from` value to a `to` value pointer GetConverter(from, to reflect.Type) FromToFunc // GetConverterTo returns the ToFunc to be used to convert a `from` value to a `to` value GetConverterTo(from, to reflect.Type) ToFunc }
Registry is the entity responsible for keep the FromToFunc instances classified by the `from` and `to` types. Because Golang only supports the Generic feature per structure and not per structure method, we will use functions to register the FromToFunc instances ensuring that Registry knows the `from` and `to` types.
type ToFunc ¶
ToFunc is a dynamic type conversion function
func MakeToFunc ¶
func MakeToFuncWithArg ¶
type ToTypedFunc ¶
ToTypedFunc is a partially typed conversion function from any type to a specific type