Documentation ¶
Index ¶
- Variables
- func MustParsePath(path string, options ...option) (string, error)
- func ParseAbsoluteTime(in interface{}) (time.Time, error)
- func ParseAddrs(addrs interface{}) ([]*sockaddr.SockAddrMarshaler, error)
- func ParseBool(in interface{}) (bool, error)
- func ParseCapacityString(in interface{}) (uint64, error)
- func ParseCommaStringSlice(in interface{}) ([]string, error)
- func ParseDirectIntSlice(in interface{}) ([]int64, error)
- func ParseDurationSecond(in interface{}) (time.Duration, error)
- func ParseInt(in interface{}) (int64, error)
- func ParseIntSlice(in interface{}) ([]int64, error)
- func ParsePath(path string, options ...option) (string, error)
- func ParseString(in interface{}) (string, error)
- func SafeParseInt(in interface{}) (int, error)
- func SafeParseIntRange(in interface{}, min int64, max int64) (int64, error)
- func SafeParseIntSlice(in interface{}, elements int) ([]int, error)
- func SafeParseIntSliceRange(in interface{}, minValue int64, maxValue int64, elements int) ([]int64, error)
- func WithErrorOnMissingEnv(errorOnMissingEnv bool) option
- func WithNoTrimSpaces(noTrim bool) option
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotAUrl = errors.New("not a url") ErrNotParsed = errors.New("not a parsed value") )
var (
ErrDurationMultiplicationOverflow = errors.New("multiplication of durations resulted in overflow, one operand may be too large")
)
Functions ¶
func MustParsePath ¶ added in v0.1.7
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 ¶
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 ParseCapacityString ¶
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 ¶
Parses the provided string-like value as a comma-separated list of values.
func ParseDirectIntSlice ¶ added in v0.1.3
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 ¶
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 ¶
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
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 ¶
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 ¶
Parses the provided arbitrary value as a string.
func SafeParseInt ¶ added in v0.1.5
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
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
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).
func WithErrorOnMissingEnv ¶ added in v0.1.9
func WithErrorOnMissingEnv(errorOnMissingEnv bool) option
When true, if an environment variable is unset, an error will be returned rather than the empty string.
func WithNoTrimSpaces ¶ added in v0.1.9
func WithNoTrimSpaces(noTrim bool) option
When true, values returned from ParsePath won't have leading/trailing spaces trimmed.
Types ¶
This section is empty.