coerce

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 9 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

Overview

Package coerce provides type coercion utilities for converting values between different Go types with proper error handling and overflow detection.

Key features:

  • Safe type conversions with detailed error messages
  • Support for primitive types, big.Int, complex numbers, and time.Time
  • Generic To[T] function for unified coercion API
  • Overflow detection for numeric conversions

Usage:

val, err := coerce.To[int64]("123")
if err != nil {
    // handle error
}

result, err := coerce.ToBool("true")  // true, nil
result, err := coerce.ToString(123)  // "123", nil

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupported = errors.New("gozod: conversion not supported")
	ErrNilPointer  = errors.New("gozod: nil pointer")
	ErrInvalidFmt  = errors.New("gozod: invalid format")
	ErrEmptyInput  = errors.New("gozod: empty input")
	ErrOverflow    = errors.New("gozod: value overflow")
	ErrNegative    = errors.New("gozod: negative to unsigned")
	ErrNotWhole    = errors.New("gozod: not whole number")
)

Functions

func NewEmptyInputError

func NewEmptyInputError(target string) error

NewEmptyInputError creates an error for empty input during conversion.

func NewFormatError

func NewFormatError(value, target string) error

NewFormatError creates an error for invalid format during parsing.

func NewNegativeError added in v0.6.0

func NewNegativeError(value any, target string) error

NewNegativeError creates an error for negative to unsigned conversion.

func NewNilPointerError added in v0.6.0

func NewNilPointerError(target string) error

NewNilPointerError creates an error for nil pointer conversion.

func NewNotWholeError

func NewNotWholeError(value any) error

NewNotWholeError creates an error for non-whole number conversion.

func NewOverflowError

func NewOverflowError(value any, target string) error

NewOverflowError creates an error for value overflow during conversion.

func NewUnsupportedError

func NewUnsupportedError(from, to string) error

NewUnsupportedError creates an error for unsupported type conversions.

func To

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

To converts any value to the specified type T using the appropriate converter.

func ToBigInt

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

ToBigInt converts any value to *big.Int with hex string support.

func ToBool

func ToBool(v any) (bool, error)

ToBool converts any value to boolean.

func ToComplex64

func ToComplex64(v any) (complex64, error)

ToComplex64 converts any value to complex64.

func ToComplex128

func ToComplex128(v any) (complex128, error)

ToComplex128 converts any value to complex128.

func ToComplexFromString

func ToComplexFromString(s string) (complex128, error)

ToComplexFromString parses a complex number from its string representation.

func ToFloat

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

ToFloat converts any value to the specified float type T with overflow checking.

func ToFloat64

func ToFloat64(v any) (float64, error)

ToFloat64 converts any value to float64 with NaN detection.

func ToInt64

func ToInt64(v any) (int64, error)

ToInt64 converts any value to int64 with overflow detection.

func ToInteger

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

ToInteger converts any value to the specified integer type T with bounds checking.

func ToLiteral

func ToLiteral(v any) (any, error)

ToLiteral converts a value to its most natural Go literal representation. The error return is reserved for future use and is currently always nil.

func ToString

func ToString(v any) (string, error)

ToString converts any value to its string representation.

func ToTime added in v0.3.0

func ToTime(v 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