conv

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: GPL-3.0 Imports: 5 Imported by: 1

README

conv

A sring parsing library that can be extended at runtime.

Documentation

Overview

Package conv provides a unified string parsing function. Think of it like the inverse of fmt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(dest reflect.Value, source ...string) error

Parse parses source unto dest, which must be settable. The type of data expected from source depends on the type of dest. This is determined as follows:

  1. If dest implements encoding.TextUnmarshaler, unmarshal using source[0].
  2. Try registered multi-value parsers
  3. If dest implements a function Store[T any](T), parse into a new destination of T and then call Store with the resulting value.
  4. Try composite types: - Array - Slice - Map (keys are used like a set, values are zeroed)
  5. Try registered single-value parsers.
  6. Try basic single values.

When parsing into a single value, one source string is expected. When parsing into an aggregate type such as a slice or map, each source will be parsed into a different element.

ParsePtr will be sufficient for most use cases, and it does not require importing the reflection package.

func ParsePtr

func ParsePtr(dest any, source ...string) error

ParsePtr is like Parse, but takes in a regular pointer to a destination.

func Register

func Register[T any](parser func(source string) (T, error))

Register registers a parser for a particular type.

func RegisterMulti added in v0.4.0

func RegisterMulti[T any](parser func(source ...string) (T, error))

RegisterMulti registers a parser for a particular type that accepts multiple inputs.

Types

type IndexError added in v0.5.0

type IndexError struct {
	// Index is the index of the source.
	Index int
	// Err is the wrapped error.
	Err error
}

IndexError is returned when an error occurrs while parsing a specific source.

func (IndexError) Error added in v0.5.0

func (this IndexError) Error() string

Error fulfills error.

func (IndexError) Unwrap added in v0.5.0

func (this IndexError) Unwrap() error

Unwrap returns Err.

Source Files

  • builtin.go
  • conv.go

Jump to

Keyboard shortcuts

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