Documentation
¶
Overview ¶
Package values provides arbitrary variable type utilities
Index ¶
- func Compare(a, b interface{}) (same bool, err error)
- func DeepCopy(src interface{}) interface{}
- func ExtractIntValue(key string, data map[string]interface{}) (value int, ok bool)
- func GetKeyedBool(name string, data interface{}) (value bool, ok bool)
- func GetKeyedType(kind reflect.Kind, name string, data interface{}) (value reflect.Value, ok bool)
- func GetKeyedValue(name string, data interface{}) (value reflect.Value, ok bool)
- func IsBoolFalse[V ~string | ~[]byte | ~[]rune](value V) bool
- func IsBoolTrue[V ~string | ~[]byte | ~[]rune](value V) bool
- func IsEmpty(value interface{}) (empty bool)
- func IsNil(value interface{}) bool
- func IsTrue[V ~bool | maths.Number | ~string | ~[]byte | ~[]rune](v V) bool
- func IsTruthy[V ~bool | maths.Number | ~string | ~[]byte | ~[]rune](v V) (state, ok bool)
- func RecastValue[V interface{}](input interface{}) (value V, ok bool)
- func Ref[V interface{}](v V) (pointer *V)
- func ToBoolValue(v interface{}) (state, ok bool)
- func ToIndirect(value interface{}) (v interface{})
- func ToString(value interface{}) (v string)
- func TypeOf(value interface{}) (name string)
- type Comparable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶ added in v1.2.0
Compare ensures the given values are consistent and if so, uses the following table of type checking and operation:
| type | operation | |---------------|--------------------------| | Comparable | a.Equals(b) | | time.Time | a.Unix() == b.Unix() | | time.Duration | a.String() == b.String() | | default | reflect.DeepEqual |
func DeepCopy ¶ added in v1.2.0
func DeepCopy(src interface{}) interface{}
DeepCopy create a deep copy of src
func ExtractIntValue ¶ added in v1.2.0
ExtractIntValue checks if the key is present and if so, tries to convert the interface{} value into an int
func GetKeyedBool ¶ added in v1.1.0
GetKeyedBool uses GetKeyedType to retrieve a bool value
func GetKeyedType ¶ added in v1.1.0
GetKeyedType uses GetKeyedValue and only returns the value if it is of the specified `kind`
func GetKeyedValue ¶ added in v1.1.0
GetKeyedValue uses reflection to inspect the data given and if it's a struct or map, check for the named key and return reflect.Value and ok equals true if the value is a valid reflect.Value
func IsBoolFalse ¶ added in v1.2.0
IsBoolFalse returns true if the (case-insensitive) value given is one of the following: "false", "no", "off", "-1", "0", "f" or "n", and returns false for everything else
func IsBoolTrue ¶ added in v1.2.0
IsBoolTrue returns true if the (case-insensitive) value given is one of the following: "", "true", "yes", "on" "1", "t" or "y", and returns false for everything else
func IsEmpty ¶
func IsEmpty(value interface{}) (empty bool)
IsEmpty return true if the arbitrary value is empty (or zero)
func IsNil ¶ added in v1.2.0
func IsNil(value interface{}) bool
IsNil returns true if the given value is actually nil, even if the type is defined with a nil state
IsNil uses reflection
func IsTrue ¶ added in v1.2.0
IsTrue is a convenience wrapper around IsTruthy, discarding the "ok" return value
func IsTruthy ¶ added in v1.2.0
IsTruthy will return true if the given value correlates to a "known true" value (case-insensitive).
In the cases of stringy-type values, the string is compared using IsBoolTrue and IsBoolFalse. See the following table for the specific boolean values:
| Returns | Strings | +---------+-------------------+ | TRUE | true yes on t y | | FALSE | false no off f n | (or an empty string)
If none of those cases match, and the string parses to an int (or float), then the following numerical comparisons are used:
| Returns | Conditional Case | +---------+----------------------------| | TRUE | greater than zero | | FALSE | less than or equal to zero |
In all other cases, IsTruthy returns a false state with a not ok
func RecastValue ¶ added in v1.2.0
func RecastValue[V interface{}](input interface{}) (value V, ok bool)
RecastValue attempts to re-cast the `input` as a generic value type and if the re-casting fails, checks if the input is actually a pointer to the generic value type and derives the correct type by de-referencing the pointer
Example:
var input interface{} = int64(10) v, ok := RecastValue[int64](input) // v=int64(10), ok=true
func Ref ¶ added in v1.2.0
func Ref[V interface{}](v V) (pointer *V)
Ref returns a pointer to the given value, useful when dealing with testing with Go structures that require pointers to strings.
Example:
There is a need to test using the following structure:
type This struct { Thing *string } // Go doesn't allow doing: test := This{Thing: &"not a thing"} // instead, Go requires something like: nothing := "not a thing" test := This{Thing: ¬hing} // using Ref instead: test := This{Thing: values.Ref("not a thing")}
func ToBoolValue ¶ added in v1.2.0
func ToBoolValue(v interface{}) (state, ok bool)
ToBoolValue tries to derive a boolean state from the given interface{} value
| Input type | Return state | |---------------|--------------| | bool | (as-is) | | string|[]byte | IsTruthy | | maths.Number | int > 0 |
func ToIndirect ¶ added in v1.2.0
func ToIndirect(value interface{}) (v interface{})
ToIndirect checks if the value is a pointer type that fulfills the sqldriver.Valuer interface and if not, returns a reflect.Indirect interface of the value
Types ¶
type Comparable ¶ added in v1.2.0
type Comparable interface {
Equals(other interface{}) bool
}