untext

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Unlicense Imports: 6 Imported by: 1

README

Moved to https://github.com/mitranim/gg. This repo is usable but frozen.

Overview

"Missing feature" of the Go packages encoding and fmt: unmarshal arbitrary text into an arbitrary value. Counterpart to the marshaling functionality of fmt.Sprintf("%v").

Docs

See the full documentation at https://godoc.org/github.com/mitranim/untext.

Example

var num int64
err := untext.Parse(`10`, &num)

var inst time.Time
err = untext.Parse(`0001-02-03T04:05:06Z`, &inst)

Changelog

v0.1.3

Breaking: use terms "unmarshal" for []byte inputs and "parse" for string inputs. This conforms to the standard library naming conventions.

License

https://unlicense.org

Misc

I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts

Documentation

Overview

"Missing feature" of the Go packages `encoding` and `fmt`: unmarshal arbitrary text into an arbitrary value. Counterpart to the marshaling functionality of `fmt.Sprint`.

Examples

Decode individual values:

var num int64
err := untext.Parse(`10`, &num)

var inst time.Time
err = untext.Parse(`0001-02-03T04:05:06Z`, &inst)

Decode slices:

var nums []int64
err = untext.ParseSlice([]string{`10`, `20`}, &nums)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse added in v0.1.3

func Parse(input string, dest interface{}) error

Variant of `Unmarshal` that accepts a string as input.

func ParseSlice added in v0.1.3

func ParseSlice(inputs []string, dest interface{}) error

Parses a slice of strings. The destination must be a non-nil pointer to a slice. Allocates a slice of the appropriate type and calls `Parse` for each element, passing the corresponding string from the input slice.

func Unmarshal added in v0.1.3

func Unmarshal(input []byte, dest interface{}) error

Unmarshals arbitrary text into an arbitrary destination pointer. Supports a variety of "well-known" types out of the box, and falls back on `encoding.TextUnmarshaler`.

Types

type Parser added in v0.1.4

type Parser interface{ Parse(string) error }

Missing part of the "encoding" package. Commonly implemented by various types across various libraries. Automatically used by `Parse` if possible.

Jump to

Keyboard shortcuts

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