parse

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

README

parse

// TODO

Documentation

Overview

autogenerated: DO NOT EDIT

net covers the types in net.

time covers the package 'time'.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(s string) (b bool, err error)

Bool wraps strconv.ParseBool. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.

func Complex128

func Complex128(s string) (c complex128, err error)

Complex128 wraps strconv.ParseComplex(s, 128)

func Duration

func Duration(s string) (time.Duration, error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

func Float64

func Float64(s string) (x float64, err error)

Float64 wraps strconv.ParseFloat(s, 64).

func FromBinary

func FromBinary[T any, PT interface {
	*T
	encoding.BinaryUnmarshaler
}](b []byte,
) (T, error)

FromBinary parses an encoding.BinaryUnmarshaler from a []byte by using its UnmarshalBytes() method.

func FromJSONString

func FromJSONString[T any](s string) (t T, err error)

FromJSON attempts to unmarshal s to a *T using json.Unmarshal([]byte(s), &t)

func FromText

func FromText[T any, PT interface {
	*T
	encoding.TextUnmarshaler
}](s string,
) (T, error)

FromTextBytes parses an encoding.TextUnmarshaler from a []byte by using its UnmarshalText() method.

func FromTextBytes

func FromTextBytes[T any, PT interface {
	*T
	encoding.TextUnmarshaler
}](b []byte,
) (T, error)

FromTextBytes parses an encoding.TextUnmarshaler from a []byte by using its UnmarshalText() method.

func HSet

func HSet[K comparable](
	parse func(string) (K, error),
	raw string,
) (set.HSet[K], error)

HSet parses text as a series of keys separated by whitespace and/or commas. The rules are identical to Slice[K]. Duplicate keys are NOT an error.

func IP

func IP(s string) (net.IP, error)
// wrapper for net.ParseIP

IP parses the string as a net.IP. The following forms are OK:

IPv4 dotted decimal ("192.0.2.1")
IPv4-mapped IPv6 ("::ffff:192.0.2.1")
IPv6 ("2001:db8::68")

func IPv4

func IPv4(s string) (net.IP, error)

IPv4 parses the string as an IPv4. The following forms are OK:

IPv4 dotted decimal ("192.0.2.1")
IPv4-mapped IPv6 ("::ffff:192.0.2.1")

func Int

func Int(s string) (int, error)

Int is equivalent to strconv.ParseInt(s, 0, 0), but gives more informative error messages.

func Int16 added in v0.1.2

func Int16(s string) (int16, error)

Int16 is equivalent to strconv.ParseInt(s, 0, 16), but gives more informative error messages.

func Int16Base added in v0.1.2

func Int16Base(s string, base int) (int16, error)

Int16Base is equivalent to strconv.ParseInt(s, base, 16), but gives more informative error messages.

func Int32 added in v0.1.2

func Int32(s string) (int32, error)

Int32 is equivalent to strconv.ParseInt(s, 0, 32), but gives more informative error messages.

func Int32Base added in v0.1.2

func Int32Base(s string, base int) (int32, error)

Int32Base is equivalent to strconv.ParseInt(s, base, 32), but gives more informative error messages.

func Int64 added in v0.1.2

func Int64(s string) (int64, error)

Int64 is equivalent to strconv.ParseInt(s, 0, 64), but gives more informative error messages.

func Int64Base added in v0.1.2

func Int64Base(s string, base int) (int64, error)

Int64Base is equivalent to strconv.ParseInt(s, base, 64), but gives more informative error messages.

func Int8 added in v0.1.2

func Int8(s string) (int8, error)

Int8 is equivalent to strconv.ParseInt(s, 0, 8), but gives more informative error messages.

func Int8Base added in v0.1.2

func Int8Base(s string, base int) (int8, error)

Int8Base is equivalent to strconv.ParseInt(s, base, 8), but gives more informative error messages.

func IntBase

func IntBase(s string, base int) (int, error)

IntBase is equivalent to strconv.ParseInt(s, base, 0), but gives more informative error messages.

func KeyVal

func KeyVal[K any, V any](
	kv string,
	sep byte,
	parseKey func(string) (K, error),
	parseVal func(string) (V, error),
) (k K, v V, err error)

KeyVal parses a 'sep'-separated key-value pair k<SEP>v by calling fk on k and fv on v. eg, KeyVal(`"expires":10m", ':', strconv.Unquote, ParseDuration) should return ("expires", 10*time.Minute, nil) You probably don't want to use this directly unless you're implementing parsing of a data-structure directly.

func MAC

func MAC(s string) (net.HardwareAddr, error)
// wrapper for net.ParseMAC

MAC parses the string as a net.HardwareAddr ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address using one of the following formats:

00:00:5e:00:53:01 02:00:5e:10:00:00:00:01 00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01 00-00-5e-00-53-01 02-00-5e-10-00-00-00-01 00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01 0000.5e00.5301 0200.5e10.0000.0001 0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001

func Map

func Map[K comparable, V any](
	parseK func(rawKey string) (K, error),
	parseV func(rawVal string) (V, error),
	raw string,
) (map[K]V, error)

Map parses a string in the form "{k0:v0, k1:v1}" by calling parseK on the keys and parseV on the vals. The map must have unique keys: duplicate fields are an error. Non-nil errors are of type *MapError.

func MapFunc

func MapFunc[K comparable, V any](
	parseK func(string) (K, error),
	parseV func(string) (V, error),
) func(s string) (map[K]V, error)

MapFunc(parseK, parseV) creates a closure f such that f(s) = Map(parseK, parseV, s)

func Port

func Port(s string) (uint16, error)

Port parses a port, with or without the leading ":".

func Slice

func Slice[T any](parse func(s string) (T, error), s string) (a []T, err error)

Slice parses text as a series of values separated by commas (and optional whitespace), optionally enclosed in a [] or {}. An error will be of type *ParseError[T].

func SliceFunc

func SliceFunc[T any](parse func(s string) (T, error)) func(string) ([]T, error)

SliceFunc returns a closure f such that f(s) = Slice(parse, s).

func TimeRFC3339

func TimeRFC3339(s string) (time.Time, error)

TimeRFC3339 is as time.Parse(time.RFC3339, s).

func Trace

func Trace(s string) (trace.Trace, error)

Trace parses a slice of *trace.Node.

func TraceNode

func TraceNode(s string) (*trace.Node, error)

TraceNode parses a *trace.Node from the form "<label>!<id>!<time>"

func UUID

func UUID(s string) (uuid.UUID, error)

UUID decodes s into a UUID or returns an error. The following forms are OK:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  // standard
urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  // also standard
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}  // microsoft
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. // raw hex

This is just a wrapper for uuid.Parse.

func Uint

func Uint(s string) (uint, error)

Uint is equivalent to strconv.ParseUint(s, 0, 0), but gives more informative error messages.

func Uint16 added in v0.1.2

func Uint16(s string) (uint16, error)

Uint16 is equivalent to strconv.ParseUint(s, 0, 16), but gives more informative error messages.

func Uint16Base added in v0.1.2

func Uint16Base(s string, base int) (uint16, error)

Uint16Base is equivalent to strconv.ParseUint(s, base, 16), but gives more informative error messages.

func Uint32 added in v0.1.2

func Uint32(s string) (uint32, error)

Uint32 is equivalent to strconv.ParseUint(s, 0, 32), but gives more informative error messages.

func Uint32Base added in v0.1.2

func Uint32Base(s string, base int) (uint32, error)

Uint32Base is equivalent to strconv.ParseUint(s, base, 32), but gives more informative error messages.

func Uint64 added in v0.1.2

func Uint64(s string) (uint64, error)

Uint64 is equivalent to strconv.ParseUint(s, 0, 64), but gives more informative error messages.

func Uint64Base added in v0.1.2

func Uint64Base(s string, base int) (uint64, error)

Uint64Base is equivalent to strconv.ParseUint(s, base, 64), but gives more informative error messages.

func Uint8 added in v0.1.2

func Uint8(s string) (uint8, error)

Uint8 is equivalent to strconv.ParseUint(s, 0, 8), but gives more informative error messages.

func Uint8Base added in v0.1.2

func Uint8Base(s string, base int) (uint8, error)

Uint8Base is equivalent to strconv.ParseUint(s, base, 8), but gives more informative error messages.

func UintBase

func UintBase(s string, base int) (uint, error)

UintBase is equivalent to strconv.ParseUint(s, base, 0), but gives more informative error messages.

Types

type Error

type Error[T any] struct {
	Raw string
	Err error
}

Error is a generic parsing error, used for primitives.

func (*Error[T]) Error

func (err *Error[T]) Error() string

func (*Error[T]) Unwrap

func (err *Error[T]) Unwrap() error

Unwrap() returns the underlying error that caused the parsing failure.

type MapError

type MapError[K comparable, V any] struct {
	// The text that failed parsing.
	Raw string
	// Underlying error that caused the failure. Returned by Unwrap().
	Err error
	// Index of the comma-separated item where parsing failed, if any.
	I int
	// Number of elements
	N int
	// The item at index I, in the form key:value
	Sub string
}

MapError is returned from a failed call to Slice(parseK, parseV, s). It will Unwrap() to the error of parse(), if any.

func (*MapError[K, V]) Error

func (err *MapError[K, V]) Error() string

func (*MapError[K, V]) Unwrap

func (err *MapError[K, V]) Unwrap() error

Unwrap() returns the underlying error that caused the parsing failure.

type SliceError

type SliceError[T any] struct {
	// The text that failed parsing.
	Raw string
	// Underlying error that caused the failure. Returned by Unwrap().
	Err error
	// Index of the comma-separated item where parsing failed, if any.
	I int
	// The item at index I.
	Sub string
}

SliceError is returned from a failed call to Slice(parse, s). It will Unwrap() to the error of parse(), if any.

func (*SliceError[T]) Error

func (err *SliceError[T]) Error() string

Unwrap() returns the underlying error that caused the parsing failure.

func (*SliceError[T]) Unwrap

func (err *SliceError[T]) Unwrap() error

Unwrap() returns the underlying error that caused the parsing failure.

Jump to

Keyboard shortcuts

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