coerce

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2025 License: MIT Imports: 10 Imported by: 0

README

COERCE

Type-safe, panic-free conversion helpers for modern Go.

Usage Example

package main

import (
    "fmt"
    "log"
    "github.com/kaptinlin/gozod/pkg/coerce"
)

func main() {
    raw := map[string]any{"port": "8080", "debug": "on", "timeout": 2.5}

    // Integer conversion
    port, err := coerce.ToInteger[int](raw["port"])
    if err != nil { log.Fatal(err) }

    // Boolean conversion
    debug, _ := coerce.ToBool(raw["debug"])
    // Float conversion
    timeout, _ := coerce.ToFloat64(raw["timeout"])

    fmt.Printf("port=%d debug=%t timeout=%.1fs\n", port, debug, timeout)
}

Quick Reference

import "github.com/kaptinlin/gozod/pkg/coerce"

// Generic conversion
coerce.To[T](val)              // convert to type T
coerce.ToLiteral(val)           // literal value conversion

// String & time
coerce.ToString(val)            // string conversion
coerce.ToTime(val)              // time.Time conversion

// Boolean
coerce.ToBool(val)              // bool conversion

// Numeric (scalar)
coerce.ToInt64(val)             // int64 conversion
coerce.ToFloat64(val)           // float64 conversion
coerce.ToBigInt(val)            // *big.Int conversion

// Numeric (generic)
coerce.ToInteger[I](val)        // integer type I
coerce.ToFloat[F](val)          // float type F

// Complex
coerce.ToComplex64(val)         // complex64
coerce.ToComplex128(val)        // complex128
coerce.ToComplexFromString(str) // parse complex from string

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Type compatibility errors
	ErrUnsupported = errors.New("conversion not supported")
	ErrNilPointer  = errors.New("nil pointer")

	// Format and parsing errors
	ErrInvalidFormat = errors.New("invalid format")
	ErrEmptyInput    = errors.New("empty input")

	// Numeric conversion errors
	ErrOverflow     = errors.New("value overflow")
	ErrNegativeUint = errors.New("negative to unsigned")
	ErrNotWhole     = errors.New("not whole number")
)

Functions

func NewEmptyInputError

func NewEmptyInputError(targetType string) error

NewEmptyInputError creates a detailed empty input error for specific type

func NewFormatError

func NewFormatError(value, targetType string) error

NewFormatError creates a detailed format error with the problematic value

func NewNegativeUintError

func NewNegativeUintError(value any, targetType string) error

NewNegativeUintError creates a detailed negative to unsigned conversion error

func NewNotWholeError

func NewNotWholeError(value any) error

NewNotWholeError creates a detailed non-whole number error

func NewOverflowError

func NewOverflowError(value any, targetType string) error

NewOverflowError creates a detailed overflow error with the problematic value

func NewUnsupportedError

func NewUnsupportedError(from, to string) error

NewUnsupportedError creates a detailed unsupported conversion error

func To

func To[T any](v any) (T, error)

func ToBigInt

func ToBigInt(v any) (*big.Int, error)

func ToBool

func ToBool(v any) (bool, error)

ToBool converts any value to boolean with fast-path optimizations

func ToComplex64

func ToComplex64(v any) (complex64, error)

func ToComplex128

func ToComplex128(v any) (complex128, error)

func ToComplexFromString

func ToComplexFromString(s string) (complex128, error)

func ToFloat

func ToFloat[T ~float32 | ~float64](v any) (T, error)

func ToFloat64

func ToFloat64(v any) (float64, error)

func ToInt64

func ToInt64(v any) (int64, error)

func ToInteger

func ToInteger[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](v any) (T, error)

func ToLiteral

func ToLiteral(value any) (any, error)

func ToString

func ToString(v any) (string, error)

ToString converts any value to string with fast-path optimizations

func ToTime added in v0.3.0

func ToTime(value any) (time.Time, error)

ToTime converts various inputs to time.Time

Types

This section is empty.

Jump to

Keyboard shortcuts

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