Documentation
¶
Overview ¶
stringable is a tiny package that helps converting values from/to a string.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnsupportedType = errors.New("unsupported type") ErrTypeMismatch = errors.New("type mismatch") ErrNotStringMarshaler = errors.New("not a StringMarshaler") ErrNotStringUnmarshaler = errors.New("not a StringUnmarshaler") ErrNotPointer = errors.New("not a pointer") ErrNilPointer = errors.New("nil pointer") )
Functions ¶
This section is empty.
Types ¶
type AnyStringableAdaptor ¶
type AnyStringableAdaptor func(any) (Stringable, error)
func ToAnyStringableAdaptor ¶
func ToAnyStringableAdaptor[T any](adapt StringableAdaptor[T]) (reflect.Type, AnyStringableAdaptor)
type Namespace ¶
type Namespace struct {
// contains filtered or unexported fields
}
Namespace is the place to register type adaptors (of AnyStringableAdaptor).
func NewNamespace ¶
func NewNamespace() *Namespace
NewNamespace creates a namespace where you can register adaptors to override/adapt the converting behaviours of existing types.
func (*Namespace) Adapt ¶
func (c *Namespace) Adapt(typ reflect.Type, adaptor AnyStringableAdaptor)
Adapt registers a custom adaptor for the given type.
- You must create a Namespace instance and register the adaptor there.
- Call ToAnyStringableAdaptor to create an adaptor of a specific type.
Example:
ns := stringable.NewNamespace() typ, adaptor := stringable.ToAnyStringableAdaptor[bool](func(b *bool) (stringable.Stringable, error) { // todo }) ns.Adapt(typ, adaptor)
func (*Namespace) New ¶
func (c *Namespace) New(v any, opts ...Option) (Stringable, error)
New creates a Stringable instance from the given value. If the given value itself is already a Stringable, it will return directly. Otherwise, it will try to create a Stringable instance by trying the following approaches:
- check if there's a custom adaptor for the type of the given value, if so, use it to adapt the given value to a Stringable.
- same as above, but check the builtin adaptors, which support the builtin types, e.g. int, string, float64, etc.
- try to create a "hybrid" instance, which makes use of the methods FromString, ToString, MarshalText and UnmarshalText to fullfill the Stringable interface.
It has three options:
New(v)
1. with only default options, it will try all the 3 ways as listed above to create a Stringable.
New(v, NoHybrid())
2. without hybrid, i.e. won't try the 3rd method, returns an ErrUnsupportedType error.
New(v, CompleteHybrid())
3. the hybrid must be a "complete" hybrid, which means it has to implement both FromString and ToString method that the Stringable interface requires, while a "partial"/"incomplete" hybrid, one of theses two methods can be absent, and the absent one always returns an error, either ErrNotStringMarshaler or ErrNotStringUnmarshaler.
type StringMarshaler ¶
StringMarshaler defines a type to be able to convert to a string.
type StringUnmarshaler ¶
StringUnmarshaler defines a type to be able to convert from a string.
type Stringable ¶
type Stringable interface { StringMarshaler StringUnmarshaler }
Stringable defines a type to be able to convert from/to a string.
func New ¶
func New(v any) (Stringable, error)
New creates a Stringable instance from the given value. Note that this method is a wrapper around the default namespace's New method. Which means it doesn't support override/adapt existing types. Please read Namespace.New to learn more.
type StringableAdaptor ¶
type StringableAdaptor[T any] func(*T) (Stringable, error)