typeconv

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package typeconv provides utilities for converting string values to various Go types using reflection. This is useful for configuration loading, CLI parsing, and other scenarios where string data needs to be converted to strongly typed values.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedType = errors.New("typeconv: unsupported type")
	ErrInvalidValue    = errors.New("typeconv: invalid value")
)
View Source
var Default = New()

Default is the default converter instance.

Functions

func ConvertTo

func ConvertTo[T any](value string) (T, error)

ConvertTo converts a string value to the specified type T. This is a generic helper that returns the converted value.

func MustConvertTo

func MustConvertTo[T any](value string) T

MustConvertTo is like ConvertTo but panics on error.

Types

type ByteSize added in v1.1.0

type ByteSize uint64

ByteSize represents a size in bytes that can be parsed from human-readable SI/IEC strings like "100K", "512Mi", "2G", "1.5TB".

const (
	KB ByteSize = 1000
	MB ByteSize = 1000 * KB
	GB ByteSize = 1000 * MB
	TB ByteSize = 1000 * GB
	PB ByteSize = 1000 * TB
	EB ByteSize = 1000 * PB

	KiB ByteSize = 1024
	MiB ByteSize = 1024 * KiB
	GiB ByteSize = 1024 * MiB
	TiB ByteSize = 1024 * GiB
	PiB ByteSize = 1024 * TiB
	EiB ByteSize = 1024 * PiB
)

func ParseByteSize added in v1.1.0

func ParseByteSize(input string) (ByteSize, error)

func (ByteSize) Int64 added in v1.2.1

func (b ByteSize) Int64() int64

Int64 returns the size as an int64. It returns math.MaxInt64 if the value overflows.

func (ByteSize) MarshalText added in v1.1.0

func (b ByteSize) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (ByteSize) String added in v1.1.0

func (b ByteSize) String() string

String returns a human-readable IEC representation (e.g. "1.5GiB").

func (ByteSize) Uint64 added in v1.2.1

func (b ByteSize) Uint64() uint64

Uint64 returns the size as a plain uint64.

func (*ByteSize) UnmarshalText added in v1.1.0

func (b *ByteSize) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler, which is used by JSON and YAML decoders to parse quoted strings.

type Converter

type Converter struct {
	// SliceSeparator is the string used to split slice values. Default is ",".
	SliceSeparator string

	// TimeLayout is the layout used for time.Time conversion. Default is time.RFC3339.
	TimeLayout string
}

Converter handles conversion of string values to various Go types.

func New

func New() *Converter

New creates a new Converter with default settings.

func (*Converter) Convert

func (c *Converter) Convert(target reflect.Value, value string) error

Jump to

Keyboard shortcuts

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