xform

package
v0.0.0-...-a14d088 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package xform provides type-safe transformation functions for converting and validating data. These transformers are designed to be composable and work seamlessly with the envutil package's fluent API.

Each transformer function follows the pattern: func(Input) (Output, error) This allows them to be chained together for complex transformations:

envutil.String("PORT",
    envutil.Transform(xform.Int64),
    envutil.Transform(xform.CastNumeric[int64, int]),
    envutil.Transform(xform.Positive[int]),
).Value()

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidChoice is returned when a value is not one of the allowed choices.
	ErrInvalidChoice = errors.New("invalid choice")

	// ErrNonPositive is returned when a numeric value is not positive (i.e., <= 0).
	ErrNonPositive = errors.New("value must be positive")

	// ErrBadPort is returned when a port number is invalid (< 0 or > 65535).
	ErrBadPort = errors.New("invalid port number")

	// ErrBadHostAndPort is returned when a host:port pair cannot be parsed.
	ErrBadHostAndPort = errors.New("invalid host:port pair")

	// ErrNotAFile is returned when a path exists but is not a file.
	ErrNotAFile = errors.New("not a file")

	// ErrNotADir is returned when a path exists but is not a directory.
	ErrNotADir = errors.New("not a directory")

	// ErrEmptyFile is returned when a file exists but has zero size.
	ErrEmptyFile = errors.New("empty file")
)
View Source
var ErrInvalidGzipLevel = errors.New("invalid gzip level")

ErrInvalidGzipLevel is returned when a gzip compression level string cannot be parsed or is not a valid compression level constant.

View Source
var ErrInvalidLogLevel = errors.New("invalid log level")

ErrInvalidLogLevel is returned when a log level string is not recognized.

Functions

func Bool

func Bool(value string) (bool, error)

Bool parses a string as a boolean value. Accepts: "1", "t", "T", "true", "TRUE", "True", "0", "f", "F", "false", "FALSE", "False".

func Bytes

func Bytes(value string) ([]byte, error)

Bytes converts a string to a byte slice.

func CastNumeric

func CastNumeric[A Numeric, B Numeric](value A) (B, error)

CastNumeric converts a numeric value from one type to another. Example: CastNumeric[int64, int32] converts int64 to int32. Note: This may truncate or lose precision depending on the types involved.

func Duration

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

Duration parses a string as a time.Duration. Accepts formats like "1h30m", "5s", "100ms", etc. as defined by time.ParseDuration.

func Float32

func Float32(value float64) (float32, error)

Float32 converts a float64 to a float32. Note: This may lose precision for values outside the float32 range.

func Float64

func Float64(value string) (float64, error)

Float64 parses a string as a float64.

func GzipLevel

func GzipLevel(value string) (int, error)

GzipLevel represents a transformer that parses the given string as a gzip compression level. The string can be one of the following:

  • "default"
  • "best-speed" or "best_speed"
  • "best-compression" or "best_compression"
  • "no-compression" or "no_compression" or "none"
  • "huffman-only" or "huffman_only"
  • the numbers 0, 1, 9, -1, -2 (constants from the underlying library which correspond to the above options)

func HostAndPort

func HostAndPort(value string) (envtypes.HostPort, error)

HostAndPort parses a string as a host:port pair. The input must be in the format "host:port" where port is a valid port number (0-65535). Returns ErrBadHostAndPort if the format is invalid or the port is out of range.

func Int64

func Int64(value string) (int64, error)

Int64 parses a string as a base-10 int64.

func Keyify

func Keyify(strs []string) (map[string]struct{}, error)

Keyify converts a slice of strings to a map where the keys are the strings from the slice and values are empty structs. This provides an efficient way to create a set for membership testing.

func OneOf

func OneOf[A comparable](choices ...A) func(A) (A, error)

OneOf returns a transformer that validates a value is one of the allowed choices. Returns ErrInvalidChoice if the value doesn't match any of the choices.

func OpenFile

func OpenFile(value envtypes.LocalPath) (*os.File, error)

OpenFile opens the file at the given path for reading. The caller is responsible for closing the returned file.

func Path

func Path(value string) (envtypes.LocalPath, error)

Path treats the input as a local filesystem path and returns a LocalPath struct. The path is stat'ed to gather file information. If the path doesn't exist, the LocalPath will have a nil Info field but no error is returned. Other stat errors (permission denied, etc.) are returned as errors.

func PathExists

func PathExists(value envtypes.LocalPath) (envtypes.LocalPath, error)

PathExists validates that a path exists on the filesystem. Returns os.ErrNotExist if the path does not exist.

func PathIsDir

func PathIsDir(value envtypes.LocalPath) (envtypes.LocalPath, error)

PathIsDir validates that a path exists and is a directory. Returns os.ErrNotExist if the path doesn't exist, or ErrNotADir if it's a file.

func PathIsFile

func PathIsFile(value envtypes.LocalPath) (envtypes.LocalPath, error)

PathIsFile validates that a path exists and is a regular file (not a directory). Returns os.ErrNotExist if the path doesn't exist, or ErrNotAFile if it's a directory.

func PathIsNonEmptyFile

func PathIsNonEmptyFile(value envtypes.LocalPath) (envtypes.LocalPath, error)

PathIsNonEmptyFile validates that a path exists, is a regular file, and has size > 0. Returns os.ErrNotExist if the path doesn't exist, ErrNotAFile if it's a directory, or ErrEmptyFile if the file size is zero.

func PathNotExists

func PathNotExists(value envtypes.LocalPath) (envtypes.LocalPath, error)

PathNotExists validates that a path does NOT exist on the filesystem. Returns os.ErrExist if the path already exists.

func Port

func Port(value string) (uint16, error)

Port parses a string as a TCP/UDP port number. Valid port numbers are in the range 0-65535. Returns ErrBadPort if the value is not a valid port number.

func Positive

func Positive[A Numeric](value A) (A, error)

Positive validates that a numeric value is greater than zero. Returns ErrNonPositive if the value is less than or equal to zero.

func ReadFile

func ReadFile(value envtypes.LocalPath) ([]byte, error)

ReadFile reads and returns the entire contents of the file at the given path.

func ReplaceAll

func ReplaceAll(oldStr, newStr string) func(string) (string, error)

ReplaceAll returns a transformer that replaces all occurrences of oldStr with newStr in the input string.

func SlogLevel

func SlogLevel(value string) (slog.Level, error)

SlogLevel parses a string as a slog.Level. Accepts: "debug", "info", "warn", "error" (case-sensitive). Returns ErrInvalidLogLevel for unrecognized values.

func SplitString

func SplitString(sep string) func(string) ([]string, error)

SplitString returns a transformer that splits a string by the given separator. The separator can be any string, including multi-character separators.

func String

func String(value []byte) (string, error)

String converts a byte slice to a string.

func Time

func Time(layout string) func(string) (time.Time, error)

Time returns a transformer that parses a string as a time.Time using the given layout. The layout uses Go's reference time format (Mon Jan 2 15:04:05 MST 2006).

func ToLower

func ToLower(s string) (string, error)

ToLower converts a string to lowercase.

func ToUpper

func ToUpper(s string) (string, error)

ToUpper converts a string to uppercase.

func TrimString

func TrimString(s string) (string, error)

TrimString removes leading and trailing whitespace from a string.

func URL

func URL(value string) (*url.URL, error)

URL parses a string as a URL using Go's standard url.Parse. The URL may be relative or absolute.

func UUID

func UUID(value string) (uuid.UUID, error)

UUID parses a string as a UUID in any of the formats accepted by github.com/google/uuid. Accepts formats like: "6ba7b810-9dad-11d1-80b4-00c04fd430c8" or "6ba7b8109dad11d180b400c04fd430c8".

func Uint64

func Uint64(value string) (uint64, error)

Uint64 parses a string as a base-10 uint64.

Types

type Intish

type Intish interface {
	int | int8 | int16 | int32 | int64 | time.Duration
}

Intish is a constraint for signed integer types and time.Duration.

type Numeric

type Numeric interface {
	int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64 | int | uint | time.Duration
}

Numeric is a constraint for all numeric types including integers, floats, and time.Duration.

type Uintish

type Uintish interface {
	uint | uint8 | uint16 | uint32 | uint64
}

Uintish is a constraint for unsigned integer types.

Jump to

Keyboard shortcuts

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