Documentation
¶
Overview ¶
Package conversion provides small, strongly typed, reflection-free value conversion contracts for application, configuration, and transport layers.
Index ¶
- Variables
- func ParseBoolean(value string) (bool, error)
- func ParseDuration(value string) (time.Duration, error)
- func ParseSignedInteger(value string, bitSize int) (int64, error)
- type Codec
- func Boolean() Codec[bool]
- func Duration() Codec[time.Duration]
- func Float(bitSize int) (Codec[float64], error)
- func NewCodec[T any](name string, parse func(string) (T, error), format func(T) (string, error)) (Codec[T], error)
- func SignedInteger(bitSize int) (Codec[int64], error)
- func String() Codec[string]
- func Time(layout string, location *time.Location) (Codec[time.Time], error)
- func URL() Codec[*url.URL]
- func UnsignedInteger(bitSize int) (Codec[uint64], error)
- type Converter
- type ConverterFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidValue identifies input that cannot be represented by the // requested target type. Built-in converters never include the raw input // in returned errors. ErrInvalidValue = errors.New("value is invalid") )
Functions ¶
func ParseBoolean ¶
ParseBoolean parses a Boolean with safe diagnostics.
func ParseDuration ¶
ParseDuration parses a canonical Go duration with safe diagnostics.
Types ¶
type Codec ¶
type Codec[T any] struct { // contains filtered or unexported fields }
Codec provides an exact bidirectional string representation for T.
func NewCodec ¶
func NewCodec[T any]( name string, parse func(string) (T, error), format func(T) (string, error), ) (Codec[T], error)
NewCodec validates and constructs a custom deterministic codec.
func SignedInteger ¶
SignedInteger returns a base-10 signed integer codec for a Go bit width.
func UnsignedInteger ¶
UnsignedInteger returns a base-10 unsigned integer codec for a Go bit width.
type Converter ¶
Converter transforms one exact source type into one exact target type. Implementations should be deterministic and must not perform hidden I/O.
func Then ¶
func Then[Source, Intermediate, Target any]( first Converter[Source, Intermediate], second Converter[Intermediate, Target], ) Converter[Source, Target]
Then composes two typed converters without a runtime registry or type lookup.
Example ¶
integer, err := SignedInteger(64)
if err != nil {
fmt.Println("conversion unavailable")
return
}
label := ConverterFunc[int64, string](func(value int64) (string, error) {
return fmt.Sprintf("order-%d", value), nil
})
value, err := Then[string, int64, string](
integer,
label,
).Convert("42")
if err != nil {
fmt.Println("conversion failed")
return
}
fmt.Println(value)
Output: order-42
type ConverterFunc ¶
ConverterFunc adapts an ordinary function to Converter.
func (ConverterFunc[Source, Target]) Convert ¶
func (convert ConverterFunc[Source, Target]) Convert( source Source, ) (Target, error)
Convert invokes the adapted function.