Documentation
¶
Overview ¶
Helpers for type inflection and simplifying working with Golang generic interface types
Index ¶
- func Auto(in any) any
- func Bool(in any) bool
- func Bytes(in any) []byte
- func Dump(in1 any, in ...any) string
- func Dumpf(format string, in ...any) string
- func Duration(in any) time.Duration
- func Err(in any) error
- func Float(in any) float64
- func FunctionArity(fn any) (int, int, error)
- func FunctionMatchesSignature(fn any, signature string) error
- func Int(in any) int64
- func IsArray(in any) bool
- func IsDuration(in any) bool
- func IsEmpty(value any) bool
- func IsFloat(in any) bool
- func IsFunction(in any) bool
- func IsFunctionArity(in any, inParams int, outParams int) bool
- func IsInteger(in any) bool
- func IsKind(in any, kinds ...reflect.Kind) bool
- func IsKindOfBool(in any) bool
- func IsKindOfFloat(in any) bool
- func IsKindOfInteger(in any) bool
- func IsKindOfString(in any) bool
- func IsLessThan(a any, b any) bool
- func IsMap(in any) bool
- func IsNumeric(in any) bool
- func IsScalar(in any) bool
- func IsStruct(in any) bool
- func IsTime(in any) bool
- func IsZero(value any) bool
- func JSON(in any, indent ...string) string
- func Len(in any) int
- func Map(in any, tagName ...string) map[Variant]Variant
- func MapNative(in any, tagName ...string) map[string]any
- func NInt(in any) int
- func OrAuto(first any, rest ...any) any
- func OrBool(first any, rest ...any) bool
- func OrBytes(first []byte, rest ...[]byte) []byte
- func OrDuration(first any, rest ...any) time.Duration
- func OrFloat(first any, rest ...any) float64
- func OrInt(first any, rest ...any) int64
- func OrNInt(first any, rest ...any) int
- func OrString(first any, rest ...any) string
- func OrTime(first any, rest ...any) time.Time
- func ParseSignatureString(signature string) (ident string, args []TypeDeclaration, returns []TypeDeclaration, perr error)
- func Pretty(pretty bool) func(*typeutilFunctionSignatureSpec) error
- func RegisterTypeHandler(handler TypeConvertFunc, types ...string)
- func ResolveValue(in any) any
- func SetValue(target any, value any) error
- func Size(size int) func(*typeutilFunctionSignatureSpec) error
- func Split(in any, on string) []string
- func String(in any) string
- func Strings(in any) []string
- func Time(in any) time.Time
- type TypeConvertFunc
- type TypeDeclaration
- type Variant
- func (self *Variant) Append(values ...any) error
- func (self Variant) Auto() any
- func (self Variant) Bool() bool
- func (self Variant) Bytes() []byte
- func (self Variant) Duration() time.Duration
- func (self Variant) Err() error
- func (self Variant) Float() float64
- func (self Variant) Int() int64
- func (self Variant) Interface() any
- func (self *Variant) IsArray() bool
- func (self *Variant) IsDuration() bool
- func (self Variant) IsFunction(signature ...string) bool
- func (self *Variant) IsKind(kind reflect.Kind) bool
- func (self Variant) IsLessThan(j any) bool
- func (self *Variant) IsMap() bool
- func (self Variant) IsNil() bool
- func (self *Variant) IsNumeric() bool
- func (self *Variant) IsScalar() bool
- func (self *Variant) IsTime() bool
- func (self Variant) IsZero() bool
- func (self Variant) Map(tagName ...string) map[Variant]Variant
- func (self Variant) MapNative(tagName ...string) map[string]any
- func (self Variant) MarshalJSON() ([]byte, error)
- func (self Variant) NInt() int
- func (self Variant) Or(or ...any) any
- func (self Variant) OrAuto(or ...any) any
- func (self Variant) OrBool(or ...any) bool
- func (self Variant) OrBytes(or ...[]byte) []byte
- func (self Variant) OrDuration(or ...any) time.Duration
- func (self Variant) OrFloat(or ...any) float64
- func (self Variant) OrInt(or ...any) int64
- func (self Variant) OrNInt(or ...any) int
- func (self Variant) OrString(or ...any) string
- func (self Variant) OrTime(or ...any) time.Time
- func (self Variant) Slice() []Variant
- func (self Variant) Split(on string) []string
- func (self Variant) String() string
- func (self Variant) Strings() []string
- func (self Variant) Time() time.Time
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FunctionArity ¶ added in v1.7.6
Returns the number of input and return arguments a given function has.
func FunctionMatchesSignature ¶ added in v1.10.2
Returns whether the given function's actual signature matches the given spec string (as parsed by ParseSignatureString).
func IsDuration ¶ added in v1.9.12
Return whether the value can be interpreted as a duration.
func IsEmpty ¶
Returns whether the given value is "empty" in the semantic sense. Zero values are considered empty, as are arrays, slices, and maps containing only empty values (called recursively). Finally, strings are trimmed of whitespace and considered empty if the result is zero-length.
func IsFunction ¶
Returns whether the given value is a function of any kind
func IsFunctionArity ¶
Returns whether the given value is a function. If inParams is not -1, the function must accept that number of arguments. If outParams is not -1, the function must return that number of values.
func IsKind ¶
Dectect whether the concrete underlying value of the given input is one or more Kinds of value.
func IsKindOfBool ¶
func IsKindOfFloat ¶
func IsKindOfInteger ¶
func IsKindOfString ¶
func IsLessThan ¶ added in v1.9.13
func IsScalar ¶
Return whether the given input is a discrete scalar value (ints, floats, bools, strings), otherwise known as "primitive types" in some other languages.
func JSON ¶ added in v1.9.11
Provide a variable to encode as JSON, and an optional indent string. If no indent argument is provided, the default indent is " " (two spaces). If an empty string is explcitly provided for the indent argument, the output will not be indented (single line).
func Len ¶
Returns the length of the given value that could have a length (strings, slices, arrays, maps, and channels). If the value is not a type that has a length, -1 is returned.
func ParseSignatureString ¶ added in v1.10.2
func ParseSignatureString(signature string) (ident string, args []TypeDeclaration, returns []TypeDeclaration, perr error)
Parse the given function signature string and return the function name, input, and output arguments. Example: "helloWorld(string) error" would return an ident of "helloWorld", a 1-element type declaration representing the "string" argument, and a 1-element returns array with the "error" return parameter.
func RegisterTypeHandler ¶ added in v1.5.55
func RegisterTypeHandler(handler TypeConvertFunc, types ...string)
Register's a handler used for converting one type to another. Type are checked in the following manner: The input value's reflect.Type String() value is matched, falling back to its reflect.Kind String() value, finally checking for a special "*" value that matches any type. If the handler function returns nil, its value replaces the input value. If the special error type PassthroughType is returned, the original value is returned unmodified.
func ResolveValue ¶
Return the concrete value pointed to by a pointer type, or within an interface type. Allows functions receiving pointers to supported types to work with those types without doing reflection.
Types ¶
type TypeConvertFunc ¶ added in v1.5.55
type TypeConvertFunc = utils.TypeConvertFunc
type TypeDeclaration ¶ added in v1.10.2
type TypeDeclaration string
func (TypeDeclaration) IsSameTypeAs ¶ added in v1.10.2
func (self TypeDeclaration) IsSameTypeAs(value any) bool
func (TypeDeclaration) String ¶ added in v1.10.2
func (self TypeDeclaration) String() string
type Variant ¶
type Variant struct {
Value any
}
Represents an interface type with helper functions for making it easy to do type conversions.
func (Variant) Bool ¶
Return true if the value can be interpreted as a boolean true value, or false otherwise.
func (Variant) Duration ¶
Return the value as a time.Duration if it can be interpreted as such, or zero otherwise.
func (Variant) Err ¶ added in v1.7.35
Return the value converted to an error, or nil if it is not an error.
func (Variant) Float ¶
Return the value as a float if it can be interpreted as such, or 0 otherwise.
func (Variant) Int ¶
Return the value as an integer if it can be interpreted as such, or 0 otherwise. Float values will be truncated to integers.
func (*Variant) IsDuration ¶ added in v1.9.12
Return whether the value can be interpreted as a duration.
func (Variant) IsFunction ¶ added in v1.10.3
func (Variant) IsLessThan ¶ added in v1.9.12
IsLessThan reports whether the given value should sort before the current variant value, taking special care to compare like types appropriately, such as detecting numbers and performing a numeric comparison, or detecting dates, times, and durations and comparing them temporally.
func (*Variant) IsNumeric ¶ added in v1.9.12
Return whether the value can be interpreted as a real number.
func (Variant) Map ¶
Return the value as a map[Variant]Variant if it can be interpreted as such, or an empty map otherwise. If the variant contains a struct, and a tagName is specified, the key names of the output map will be taken from the struct field's tag value, consistent with the rules used in encoding/json.
func (Variant) MapNative ¶ added in v1.6.0
Return the value as a map[string]any if it can be interpreted as such, or an empty map otherwise.
func (Variant) MarshalJSON ¶
Satisfy the json.Marshaler interface
func (Variant) NInt ¶ added in v1.8.91
Return the value as a native integer if it can be interpreted as such, or 0 otherwise. Float values will be truncated to integers.
func (Variant) Slice ¶
Return the value as a slice of Variants. Scalar types will return a slice containing a single Variant element representing the value.
func (Variant) Split ¶ added in v1.7.50
Converts the value to a string, then splits on the given delimiter.
func (Variant) String ¶
Return the value as a string, or an empty string if the value could not be converted.