typeutil

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Helpers for type inflection and simplifying working with Golang generic interface types

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auto

func Auto(in interface{}) interface{}

Package-level auto converter

func Bool

func Bool(in interface{}) bool

Package-level bool converter

func Bytes

func Bytes(in interface{}) []byte

Package-level bytes converter

func Dump

func Dump(in1 interface{}, in ...interface{}) string

Returns a pretty-printed string representation of the given values.

func Dumpf

func Dumpf(format string, in ...interface{}) string

Returns a pretty-printed string representation of the given values.

func Duration

func Duration(in interface{}) time.Duration

Package-level duration converter

func Err

func Err(in interface{}) error

Package-level error converter

func Float

func Float(in interface{}) float64

Package-level float converter

func FunctionArity

func FunctionArity(fn interface{}) (int, int, error)

Returns the number of input and return arguments a given function has.

func FunctionMatchesSignature

func FunctionMatchesSignature(fn interface{}, signature string) error

Returns whether the given function's actual signature matches the given spec string (as parsed by ParseSignatureString).

func Int

func Int(in interface{}) int64

Package-level int64 converter

func IsArray

func IsArray(in interface{}) bool

Returns whether the given value is a slice or array.

func IsDuration

func IsDuration(in interface{}) bool

Return whether the value can be interpreted as a duration.

func IsEmpty

func IsEmpty(value interface{}) bool

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 IsFloat

func IsFloat(in interface{}) bool

Returns whether the given value represents a floating point value.

func IsFunction

func IsFunction(in interface{}) bool

Returns whether the given value is a function of any kind

func IsFunctionArity

func IsFunctionArity(in interface{}, inParams int, outParams int) bool

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 IsInteger

func IsInteger(in interface{}) bool

Returns whether the given value represents an integer value.

func IsKind

func IsKind(in interface{}, kinds ...reflect.Kind) bool

Dectect whether the concrete underlying value of the given input is one or more Kinds of value.

func IsKindOfBool

func IsKindOfBool(in interface{}) bool

func IsKindOfFloat

func IsKindOfFloat(in interface{}) bool

func IsKindOfInteger

func IsKindOfInteger(in interface{}) bool

func IsKindOfString

func IsKindOfString(in interface{}) bool

func IsLessThan

func IsLessThan(a interface{}, b interface{}) bool

func IsMap

func IsMap(in interface{}) bool

Returns whether the given value is a map.

func IsNumeric

func IsNumeric(in interface{}) bool

Returns whether the given value represents a numeric value.

func IsScalar

func IsScalar(in interface{}) bool

Return whether the given input is a discrete scalar value (ints, floats, bools, strings), otherwise known as "primitive types" in some other languages.

func IsStruct

func IsStruct(in interface{}) bool

Returns whether the given value is a struct.

func IsTime

func IsTime(in interface{}) bool

Return whether the value can be interpreted as a time.

func IsZero

func IsZero(value interface{}) bool

Returns whether the given value represents the underlying type's zero value

func JSON

func JSON(in interface{}, indent ...string) string

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

func Len(in interface{}) int

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 Map

func Map(in interface{}, tagName ...string) map[Variant]Variant

Package-level map converter

func MapNative

func MapNative(in interface{}, tagName ...string) map[string]interface{}

Package-level map[string]interface{} converter

func NInt

func NInt(in interface{}) int

Package-level native int converter

func OrAuto

func OrAuto(first interface{}, rest ...interface{}) interface{}

func OrBool

func OrBool(first interface{}, rest ...interface{}) bool

func OrBytes

func OrBytes(first []byte, rest ...[]byte) []byte

func OrDuration

func OrDuration(first interface{}, rest ...interface{}) time.Duration

func OrFloat

func OrFloat(first interface{}, rest ...interface{}) float64

func OrInt

func OrInt(first interface{}, rest ...interface{}) int64

func OrNInt

func OrNInt(first interface{}, rest ...interface{}) int

func OrString

func OrString(first interface{}, rest ...interface{}) string

func OrTime

func OrTime(first interface{}, rest ...interface{}) time.Time

func ParseSignatureString

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 Pretty

func Pretty(pretty bool) func(*typeutilFunctionSignatureSpec) error

func RegisterTypeHandler

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

func ResolveValue(in interface{}) interface{}

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.

func SetValue

func SetValue(target interface{}, value interface{}) error

Attempts to set the given reflect.Value to the given interface value

func Size

func Size(size int) func(*typeutilFunctionSignatureSpec) error

func Split

func Split(in interface{}, on string) []string

Package-level string splitter.

func String

func String(in interface{}) string

Package-level string converter

func Strings

func Strings(in interface{}) []string

Package-level string slice converter

func Time

func Time(in interface{}) time.Time

Package-level time converter

Types

type TypeConvertFunc

type TypeConvertFunc = utils.TypeConvertFunc

type TypeDeclaration

type TypeDeclaration string

func (TypeDeclaration) IsSameTypeAs

func (self TypeDeclaration) IsSameTypeAs(value interface{}) bool

func (TypeDeclaration) String

func (self TypeDeclaration) String() string

type Variant

type Variant struct {
	Value interface{}
}

Represents an interface type with helper functions for making it easy to do type conversions.

func Nil

func Nil() Variant

Return a new Variant with a nil value.

func Slice

func Slice(in interface{}) []Variant

Package-level slice converter

func V

func V(value interface{}) Variant

Shortcut for creating a Variant.

func VV

func VV(value interface{}) *Variant

Returns a pointer to a variant.

func (*Variant) Append

func (self *Variant) Append(values ...interface{}) error

func (Variant) Auto

func (self Variant) Auto() interface{}

Return the value automatically converted to the appropriate type.

func (Variant) Bool

func (self Variant) Bool() bool

Return true if the value can be interpreted as a boolean true value, or false otherwise.

func (Variant) Bytes

func (self Variant) Bytes() []byte

Return the value at key as a byte slice.

func (Variant) Duration

func (self Variant) Duration() time.Duration

Return the value as a time.Duration if it can be interpreted as such, or zero otherwise.

func (Variant) Err

func (self Variant) Err() error

Return the value converted to an error, or nil if it is not an error.

func (Variant) Float

func (self Variant) Float() float64

Return the value as a float if it can be interpreted as such, or 0 otherwise.

func (Variant) Int

func (self Variant) Int() int64

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) Interface

func (self Variant) Interface() interface{}

func (*Variant) IsArray

func (self *Variant) IsArray() bool

Return whether the value is an array/slice type.

func (*Variant) IsDuration

func (self *Variant) IsDuration() bool

Return whether the value can be interpreted as a duration.

func (Variant) IsFunction

func (self Variant) IsFunction(signature ...string) bool

func (*Variant) IsKind

func (self *Variant) IsKind(kind reflect.Kind) bool

Return whether the value is of the given reflect.Kind

func (Variant) IsLessThan

func (self Variant) IsLessThan(j interface{}) bool

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) IsMap

func (self *Variant) IsMap() bool

Return whether the value is a map type.

func (Variant) IsNil

func (self Variant) IsNil() bool

Returns whether the underlying value is nil.

func (*Variant) IsNumeric

func (self *Variant) IsNumeric() bool

Return whether the value can be interpreted as a real number.

func (*Variant) IsScalar

func (self *Variant) IsScalar() bool

Return whether the value is a scalar type.

func (*Variant) IsTime

func (self *Variant) IsTime() bool

Return whether the value can be interpreted as a time.

func (Variant) IsZero

func (self Variant) IsZero() bool

Returns whether the underlying value is a zero value.

func (Variant) Map

func (self Variant) Map(tagName ...string) map[Variant]Variant

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

func (self Variant) MapNative(tagName ...string) map[string]interface{}

Return the value as a map[string]interface{} if it can be interpreted as such, or an empty map otherwise.

func (Variant) MarshalJSON

func (self Variant) MarshalJSON() ([]byte, error)

Satisfy the json.Marshaler interface

func (Variant) NInt

func (self Variant) NInt() int

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) Or

func (self Variant) Or(or ...interface{}) interface{}

func (Variant) OrAuto

func (self Variant) OrAuto(or ...interface{}) interface{}

func (Variant) OrBool

func (self Variant) OrBool(or ...interface{}) bool

func (Variant) OrBytes

func (self Variant) OrBytes(or ...[]byte) []byte

func (Variant) OrDuration

func (self Variant) OrDuration(or ...interface{}) time.Duration

func (Variant) OrFloat

func (self Variant) OrFloat(or ...interface{}) float64

func (Variant) OrInt

func (self Variant) OrInt(or ...interface{}) int64

func (Variant) OrNInt

func (self Variant) OrNInt(or ...interface{}) int

func (Variant) OrString

func (self Variant) OrString(or ...interface{}) string

func (Variant) OrTime

func (self Variant) OrTime(or ...interface{}) time.Time

func (Variant) Slice

func (self Variant) Slice() []Variant

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

func (self Variant) Split(on string) []string

Converts the value to a string, then splits on the given delimiter.

func (Variant) String

func (self Variant) String() string

Return the value as a string, or an empty string if the value could not be converted.

func (Variant) Strings

func (self Variant) Strings() []string

Same as Slice(), but returns a []string.

func (Variant) Time

func (self Variant) Time() time.Time

Return the value as a time.Time if it can be interpreted as such, or zero time otherwise.

Jump to

Keyboard shortcuts

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