conversion

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package conversion provides small, strongly typed, reflection-free value conversion contracts for application, configuration, and transport layers.

Index

Examples

Constants

This section is empty.

Variables

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

func ParseBoolean(value string) (bool, error)

ParseBoolean parses a Boolean with safe diagnostics.

func ParseDuration

func ParseDuration(value string) (time.Duration, error)

ParseDuration parses a canonical Go duration with safe diagnostics.

func ParseSignedInteger

func ParseSignedInteger(value string, bitSize int) (int64, error)

ParseSignedInteger parses a base-10 signed integer with a Go bit width.

Types

type Codec

type Codec[T any] struct {
	// contains filtered or unexported fields
}

Codec provides an exact bidirectional string representation for T.

func Boolean

func Boolean() Codec[bool]

Boolean returns the strconv-compatible Boolean codec.

func Duration

func Duration() Codec[time.Duration]

Duration returns the canonical Go duration codec.

func Float

func Float(bitSize int) (Codec[float64], error)

Float returns a decimal floating-point codec for a 32- or 64-bit Go value.

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

func SignedInteger(bitSize int) (Codec[int64], error)

SignedInteger returns a base-10 signed integer codec for a Go bit width.

func String

func String() Codec[string]

String returns the identity string codec.

func Time

func Time(
	layout string,
	location *time.Location,
) (Codec[time.Time], error)

Time returns a time codec with one explicit layout and location.

func URL

func URL() Codec[*url.URL]

URL returns a URL codec that requires an absolute HTTP or HTTPS URL.

func UnsignedInteger

func UnsignedInteger(bitSize int) (Codec[uint64], error)

UnsignedInteger returns a base-10 unsigned integer codec for a Go bit width.

func (Codec[T]) Convert

func (codec Codec[T]) Convert(source string) (T, error)

Convert parses a string using the codec.

func (Codec[T]) Format

func (codec Codec[T]) Format(value T) (string, error)

Format returns the canonical string representation.

func (Codec[T]) Name

func (codec Codec[T]) Name() string

Name returns the stable target type name used in safe diagnostics.

func (Codec[T]) Parse

func (codec Codec[T]) Parse(source string) (T, error)

Parse converts a string without retaining or reporting the raw input.

type Converter

type Converter[Source, Target any] interface {
	Convert(Source) (Target, error)
}

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

type ConverterFunc[Source, Target any] func(Source) (Target, error)

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.

Jump to

Keyboard shortcuts

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