parseutil

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: MPL-2.0 Imports: 14 Imported by: 109

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotAUrl   = errors.New("not a url")
	ErrNotParsed = errors.New("not a parsed value")
)

Functions

func MustParsePath added in v0.1.7

func MustParsePath(path string) (string, error)

MustParsePath behaves like ParsePath but will return ErrNotAUrl if the value is not a URL with a scheme that can be parsed by this function.

func ParseAbsoluteTime

func ParseAbsoluteTime(in interface{}) (time.Time, error)

Parse an absolute timestamp from the provided arbitrary value (string or numeric value). When an untyped numeric value is provided, it is assumed to be seconds from the Unix Epoch.

func ParseAddrs

func ParseAddrs(addrs interface{}) ([]*sockaddr.SockAddrMarshaler, error)

Parses the specified value as one or more addresses, separated by commas.

func ParseBool

func ParseBool(in interface{}) (bool, error)

Parses the provided arbitrary value as a boolean-like value.

func ParseCapacityString

func ParseCapacityString(in interface{}) (uint64, error)

ParseCapacityString parses a capacity string and returns the number of bytes it represents. Capacity strings are things like 5gib or 10MB. Supported prefixes are kb, kib, mb, mib, gb, gib, tb, tib, which are not case sensitive. If no prefix is present, the number is assumed to be in bytes already.

func ParseCommaStringSlice

func ParseCommaStringSlice(in interface{}) ([]string, error)

Parses the provided string-like value as a comma-separated list of values.

func ParseDirectIntSlice added in v0.1.3

func ParseDirectIntSlice(in interface{}) ([]int64, error)

ParseDirectIntSlice behaves similarly to ParseInt, but accepts typed slices, returning a slice of int64s.

If the starting value may not be in slice form (e.g.. a bare numeric value could be provided), it is suggested to call ParseIntSlice instead.

func ParseDurationSecond

func ParseDurationSecond(in interface{}) (time.Duration, error)

Parse a duration from an arbitrary value (a string or numeric value) into a time.Duration; when units are missing (such as when a numeric type is provided), the duration is assumed to be in seconds.

func ParseInt

func ParseInt(in interface{}) (int64, error)

ParseInt takes an arbitrary value (either a string or numeric type) and parses it as an int64 value. This value is assumed to be larger than the provided type, but cannot safely be cast.

When the end value is bounded (such as an int value), it is recommended to instead call SafeParseInt or SafeParseIntRange to safely cast to a more restrictive type.

func ParseIntSlice added in v0.1.3

func ParseIntSlice(in interface{}) ([]int64, error)

ParseIntSlice is a helper function for handling upgrades of optional slices; that is, if the API accepts a type similar to <int|[]int>, nicely handle the common cases of providing only an int-ish, providing an actual slice of int-ishes, or providing a comma-separated list of numbers.

When []int64 is not the desired final type (or the values should be range-bound), it is suggested to call SafeParseIntSlice or SafeParseIntSliceRange instead.

func ParsePath

func ParsePath(path string) (string, error)

ParsePath parses a URL with schemes file://, env://, or any other. Depending on the scheme it will return specific types of data:

* file:// will return a string with the file's contents

* env:// will return a string with the env var's contents

* Anything else will return the string as it was. Functionally this means anything for which Go's `url.Parse` function does not throw an error. If you want to ensure that this function errors if a known scheme is not found, use MustParsePath.

On error, we return the original string along with the error. The caller can switch on errors.Is(err, ErrNotAUrl) to understand whether it was the parsing step that errored or something else (such as a file not found). This is useful to attempt to read a non-URL string from some resource, but where the original input may simply be a valid string of that type.

func ParseString

func ParseString(in interface{}) (string, error)

Parses the provided arbitrary value as a string.

func SafeParseInt added in v0.1.5

func SafeParseInt(in interface{}) (int, error)

Parses the specified arbitrary value (see ParseInt), ensuring that the resulting value is within the range for an int value. If no error occurred, the caller knows no overflow occurred.

func SafeParseIntRange added in v0.1.5

func SafeParseIntRange(in interface{}, min int64, max int64) (int64, error)

Parses the provided arbitrary value (see ParseInt), ensuring it is within the specified range (inclusive of bounds). If this range corresponds to a smaller type, the returned value can then be safely cast without risking overflow.

func SafeParseIntSlice added in v0.1.5

func SafeParseIntSlice(in interface{}, elements int) ([]int, error)

Parses the provided arbitrary value (see ParseIntSlice) into a slice of int values, ensuring the each resulting value in the slice is within the range for an int value. If no error occurred, the caller knows no overflow occurred.

If elements is positive, it is used to ensure the resulting slice is bounded above by that many number of elements (inclusive).

func SafeParseIntSliceRange added in v0.1.5

func SafeParseIntSliceRange(in interface{}, minValue int64, maxValue int64, elements int) ([]int64, error)

Parses the provided arbitrary value (see ParseIntSlice) into a slice of int64 values, ensuring each is within the specified range (inclusive of bounds). If this range corresponds to a smaller type, the returned value can then be safely cast without risking overflow.

If elements is positive, it is used to ensure the resulting slice is bounded above by that many number of elements (inclusive).

Types

This section is empty.

Jump to

Keyboard shortcuts

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