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 ¶
- Variables
- func Bool(value string) (bool, error)
- func Bytes(value string) ([]byte, error)
- func CastNumeric[A Numeric, B Numeric](value A) (B, error)
- func Duration(value string) (time.Duration, error)
- func Float32(value float64) (float32, error)
- func Float64(value string) (float64, error)
- func GzipLevel(value string) (int, error)
- func HostAndPort(value string) (envtypes.HostPort, error)
- func Int64(value string) (int64, error)
- func Keyify(strs []string) (map[string]struct{}, error)
- func OneOf[A comparable](choices ...A) func(A) (A, error)
- func OpenFile(value envtypes.LocalPath) (*os.File, error)
- func Path(value string) (envtypes.LocalPath, error)
- func PathExists(value envtypes.LocalPath) (envtypes.LocalPath, error)
- func PathIsDir(value envtypes.LocalPath) (envtypes.LocalPath, error)
- func PathIsFile(value envtypes.LocalPath) (envtypes.LocalPath, error)
- func PathIsNonEmptyFile(value envtypes.LocalPath) (envtypes.LocalPath, error)
- func PathNotExists(value envtypes.LocalPath) (envtypes.LocalPath, error)
- func Port(value string) (uint16, error)
- func Positive[A Numeric](value A) (A, error)
- func ReadFile(value envtypes.LocalPath) ([]byte, error)
- func ReplaceAll(oldStr, newStr string) func(string) (string, error)
- func SlogLevel(value string) (slog.Level, error)
- func SplitString(sep string) func(string) ([]string, error)
- func String(value []byte) (string, error)
- func Time(layout string) func(string) (time.Time, error)
- func ToLower(s string) (string, error)
- func ToUpper(s string) (string, error)
- func TrimString(s string) (string, error)
- func URL(value string) (*url.URL, error)
- func UUID(value string) (uuid.UUID, error)
- func Uint64(value string) (uint64, error)
- type Intish
- type Numeric
- type Uintish
Constants ¶
This section is empty.
Variables ¶
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") )
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.
var ErrInvalidLogLevel = errors.New("invalid log level")
ErrInvalidLogLevel is returned when a log level string is not recognized.
Functions ¶
func Bool ¶
Bool parses a string as a boolean value. Accepts: "1", "t", "T", "true", "TRUE", "True", "0", "f", "F", "false", "FALSE", "False".
func CastNumeric ¶
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 ¶
Duration parses a string as a time.Duration. Accepts formats like "1h30m", "5s", "100ms", etc. as defined by time.ParseDuration.
func Float32 ¶
Float32 converts a float64 to a float32. Note: This may lose precision for values outside the float32 range.
func GzipLevel ¶
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 ¶
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 Keyify ¶
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 ¶
OpenFile opens the file at the given path for reading. The caller is responsible for closing the returned file.
func Path ¶
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 ¶
PathExists validates that a path exists on the filesystem. Returns os.ErrNotExist if the path does not exist.
func PathIsDir ¶
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 ¶
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 ¶
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 ¶
PathNotExists validates that a path does NOT exist on the filesystem. Returns os.ErrExist if the path already exists.
func Port ¶
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 ¶
Positive validates that a numeric value is greater than zero. Returns ErrNonPositive if the value is less than or equal to zero.
func ReplaceAll ¶
ReplaceAll returns a transformer that replaces all occurrences of oldStr with newStr in the input string.
func SlogLevel ¶
SlogLevel parses a string as a slog.Level. Accepts: "debug", "info", "warn", "error" (case-sensitive). Returns ErrInvalidLogLevel for unrecognized values.
func SplitString ¶
SplitString returns a transformer that splits a string by the given separator. The separator can be any string, including multi-character separators.
func Time ¶
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 TrimString ¶
TrimString removes leading and trailing whitespace from a string.
func URL ¶
URL parses a string as a URL using Go's standard url.Parse. The URL may be relative or absolute.