coerce

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 10 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 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 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 struct {
	// Tags holds the owning field's declared custom tags (see
	// conga.TagSpecProvider); nil for fields that declare no registered tags.
	Tags spec.FieldTags

	// Stdin is the engine-configured standard input (App.Stdin); nil selects
	// os.Stdin for types implementing "-" conventions.
	Stdin io.Reader

	// Stdout is the engine-configured standard output (App.Stdout); nil
	// selects os.Stdout.
	Stdout io.Writer
}

DecodeContext carries the ambient input for coercing one raw value into its target type: the owning field's declared custom tags and the engine-configured standard streams. The zero value selects no tags and process defaults (os.Stdin/os.Stdout), so values decoded outside an App run behave identically to direct UnmarshalText calls. It is a plain value struct: unlike context.Context it carries no cancellation or deadlines.

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