value

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package value defines helper functions to compare numeric and other value types as either ordered or unordered. It also contains helpers to perform set operations on collections, and find sub-sequences in sequences.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsFloat

func AsFloat(value interface{}) (float64, bool)

AsFloat return the value as a flaot64 if possible

func AsInt

func AsInt(value interface{}) (int64, bool)

AsInt return the value as an int64 if possible

func AsUInt

func AsUInt(value interface{}) (uint64, bool)

AsUInt return the value as an uint64 if possible

func CompareOrdered

func CompareOrdered(lhs, rhs interface{}) (int, error)

CompareOrdered returns the result of the comparison of two values of compatible type that can be ordered, including ints, floats, strings, byte slices, and slices of comparable orderable values. Values of different type or values of the same type that cannot be ordered return an error.

func CompareUnordered

func CompareUnordered(lhs, rhs interface{}) (bool, error)

CompareUnordered returns the result of the equality comparison of two values of compatible type, including ints, floats, strings and slices of comparable values. Other values are compared with reflect.DeepEqual(). Values of different tyype that cannot be compared return an error.

func Field

func Field(value interface{}, keypath string) interface{}

Field takes a root value or an array of root values, navigates through the data tree according to a keypath, and returns the targeted values. `keypath` is a dot-separated list of keys, each used as either field name in a struct, a key in a map, or a niladic method name. Any error during key evaluation results in a nil value. If a method invocation yields multiple return values, only the first one is captured.

When the root is a single object and all the fields along the keypath are scalar types, the result is a scalar value. For each array or slice type along the path, the result become a slice collecting the result of evaluating the sub-path on each individual element. The shape of the result is then a N-dimensional array, where N is the number of arrays traversed along the path.

Because the implementation is using the reflect package and is mostly type agnostic, the resulting arrays are always of type []interface{}, even if the field types are consistent across values.

func FormatSetValues

func FormatSetValues(s Set) string

FormatSetValues returns a textual representation of the receiving set, potentially abbreviated, for the purpose of reporting discrepancies during unit-test.

func IndexOfSubsequence

func IndexOfSubsequence(seq, sub reflect.Value) int

IndexOfSubsequence finds the index of a sub-sequence `sub` in the sequence `seq`, or returns -1. Both arguments must be suitable sequences.

func IsSequenceType

func IsSequenceType(v reflect.Value) bool

IsSequenceType checks if a value is a kind of sequence

func MaxAbsoluteDifference

func MaxAbsoluteDifference(lhs, rhs interface{}) (float64, error)

MaxAbsoluteDifference returns the maximum absolute difference of all the components. Both values must have the same shape and must be composed of values convertible to float64 for comparison

func PreCheckSubsequence

func PreCheckSubsequence(v1, v2 reflect.Value) error

PreCheckSubsequence checks if a pair of values are suitable for use with IndexOfSubsequence().

Types

type Set

type Set map[interface{}]struct{}

Set represents a unique set of values of any kind, and provides methods to performat standard set operation.

func ReflectSet

func ReflectSet(value interface{}) (s Set, err error)

ReflectSet returns a newly constructed set from a collection type, or an error if the value is not a collection.

func (Set) Intersect

func (lhs Set) Intersect(rhs Set) (r Set)

Intersect returns a new set that is the intersection between the receiver and another set.

func (Set) Minus

func (lhs Set) Minus(rhs Set) (r Set)

Minus returns a new set that is the result of subtracting another set from the receiver.

func (Set) Union

func (lhs Set) Union(rhs Set) (r Set)

Union returns a new set that is the union of the receiver with another set.

Jump to

Keyboard shortcuts

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