coerce

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package coerce converts raw CLI values to Go types using built-in and registered parsers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCoerce indicates a raw value could not be converted to its target type.
	ErrCoerce = errors.New("coercion error")

	// ErrUnsupportedType indicates a value could not be coerced to its target type.
	ErrUnsupportedType = fmt.Errorf("%w: unsupported type", ErrCoerce)
)

Functions

func IsPlainStringMap added in v0.10.0

func IsPlainStringMap(mapType reflect.Type, reg Registry) bool

IsPlainStringMap reports whether mapType's keys and values are both the string type with no registered parser. Such a map, or a named map type with that underlying type, binds without per-entry decoding.

func Map

func Map(raws []string, sep string) (map[string]string, error)

Map converts a slice of raw string tokens into a map[string]string.

func Scalar

func Scalar(raw string, targetType reflect.Type) (any, error)

Scalar converts a raw string value into the target type represented by targetType.

func ScalarWith

func ScalarWith(raw string, targetType reflect.Type, reg Registry, decodeContext DecodeContext) (any, error)

ScalarWith converts a raw string value into the target type represented by targetType, first checking if a custom parser is registered for targetType in reg. decodeContext carries the owning field's declared custom tags and the engine-configured standard streams for context-aware text unmarshaling.

func SliceItems added in v0.9.0

func SliceItems(raws []string, sep string, elemType reflect.Type) []string

SliceItems splits raw slice tokens into the element items SliceWith coerces: each token is split by sep, and items are whitespace-trimmed unless splitting is disabled for a string element. Callers that inspect items before coercion (the binder's stdin check) use it so both agree on the items.

func SliceWith

func SliceWith(raws []string, sliceType reflect.Type, sep string, reg Registry, decodeContext DecodeContext) (any, error)

SliceWith converts a slice of raw string tokens into a slice of targetType using the supplied custom registry. decodeContext carries the owning field's declared custom tags and the engine-configured standard streams for context-aware text unmarshaling of each element.

func SplitMapItems

func SplitMapItems(raws []string, sep string) []string

SplitMapItems splits raw string tokens into trimmed KEY=VALUE items using sep.

func Supports added in v0.10.0

func Supports(targetType reflect.Type, reg Registry) bool

Supports reports whether ScalarWith can decode a value of targetType with the registry reg: a registered parser for the type, its pointer, or (for a pointer type) its element; a text unmarshaler on the type's pointer (or, for a pointer type, on the type itself); or a kind with built-in parsing. It follows ScalarWith's dispatch so schema build can reject a type before any value reaches it.

func Value

func Value[T any](raw string) (T, error)

Value converts a raw string value into the target type T.

Types

type DecodeContext

type DecodeContext = spec.DecodeContext

DecodeContext carries the ambient input for coercing one raw value into its target type. It is defined in internal/spec so schema build can recognise context-aware unmarshalers (see spec.ParsesItself).

type MapEntry

type MapEntry struct {
	Key   string
	Value string
}

MapEntry is a key-value pair parsed from raw CLI tokens.

func MapEntries

func MapEntries(raws []string, sep string) ([]MapEntry, error)

MapEntries converts a slice of raw string tokens into an ordered slice of MapEntry pairs, preserving input token sequence.

type Registry

type Registry map[reflect.Type]TypeParser[any]

Registry maps reflect.Type to registered parsers, erased to TypeParser[any].

type TypeParser

type TypeParser[U any] = func(raw string) (U, error)

TypeParser parses a raw string token into a value of type U.

Jump to

Keyboard shortcuts

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