values

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 8 Imported by: 11

README

godoc codecov Go Report Card

values - arbitrary variable type utilities

A collection of utilities for working with interface{} type things.

Installation

> go get github.com/go-corelibs/values@latest

Examples

IsEmpty

func main() {
    empty := values.IsEmpty("") // empty == true
    empty = values.IsEmpty(0) // empty == true
    empty = values.IsEmpty(nil) // empty == true
    empty = values.IsEmpty(-1) // empty == false
    empty = values.IsEmpty(&struct{}{}) // empty == false
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package values provides arbitrary variable type utilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v1.2.0

func Compare(a, b interface{}) (same bool, err error)

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

func ExtractIntValue(key string, data map[string]interface{}) (value int, ok bool)

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

func GetKeyedBool(name string, data interface{}) (value bool, ok bool)

GetKeyedBool uses GetKeyedType to retrieve a bool value

func GetKeyedType added in v1.1.0

func GetKeyedType(kind reflect.Kind, name string, data interface{}) (value reflect.Value, ok bool)

GetKeyedType uses GetKeyedValue and only returns the value if it is of the specified `kind`

func GetKeyedValue added in v1.1.0

func GetKeyedValue(name string, data interface{}) (value reflect.Value, ok bool)

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

func IsBoolFalse[V ~string | ~[]byte | ~[]rune](value V) bool

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

func IsBoolTrue[V ~string | ~[]byte | ~[]rune](value V) bool

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

func IsTrue[V ~bool | maths.Number | ~string | ~[]byte | ~[]rune](v V) bool

IsTrue is a convenience wrapper around IsTruthy, discarding the "ok" return value

func IsTruthy added in v1.2.0

func IsTruthy[V ~bool | maths.Number | ~string | ~[]byte | ~[]rune](v V) (state, ok bool)

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: &nothing}

// 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

func ToString

func ToString(value interface{}) (v string)

ToString returns the string representation of an arbitrary value

func TypeOf

func TypeOf(value interface{}) (name string)

TypeOf is a convenience wrapper around `fmt.Sprintf("%T", value)`

Types

type Comparable added in v1.2.0

type Comparable interface {
	Equals(other interface{}) bool
}

Jump to

Keyboard shortcuts

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