reflectex

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 7 Imported by: 1

README

reflectex

Description

Package reflectex provides various reflect based utils.

Status

Ever-evolving.

License

MIT. See included LICENSE file.

Documentation

Overview

Package reflectex provides various reflect based utils.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrReflectEx is the base error of reflectex package.
	ErrReflectEx = errorex.New("reflectex")
	// ErrInvalidParam is returned when an invalid param is passed to a func.
	ErrInvalidParam = ErrReflectEx.Wrap("invalid parameter")
	// ErrParse is returned when a parse error occurs.
	ErrParse = ErrReflectEx.Wrap("parse error")
	// ErrUnsupported is returned when an unsupported value is encountered.
	ErrUnsupported = ErrReflectEx.Wrap("unsupported value")
	// ErrConvert is returned when a conversion is unable to complete.
	ErrConvert = ErrReflectEx.WrapFormat("cannot convert '%s' to type '%s'")

	// ErrNotImplemented help.
	ErrNotImplemented = ErrReflectEx.WrapFormat("NOT IMPLEMENTED '%s'")
)

Functions

func CompareInterfaces

func CompareInterfaces(a, b interface{}) int

CompareInterfaces compares two interfaces for equality between the types contained within them. See CompareValue for details.

func CompareValues

func CompareValues(a, b reflect.Value) int

CompareValues recursively compares two possibly compound values a and b for equality. It returns:

a negative number (-1) if a is less than b. a zero (0) if a is equal to b. a positive number (1) if a is more than b.

Comparison is done using magic as described below:

Types are compared for reflect.Kind equality first and foremost. Comparison logic is taken as a and b's index in reflect.Kind enumeration.

For structs, only published fields are enumerated and compared. Private fields do not affect comparison. A struct with less public fields returns a less result. Structs with equal number of fields are compared alphabetically ascending comparing field value kinds, names and finally values.

Comparisons between two Arrays and/or slices return a less result for values with less dimensions. In equal dimensioned arrays or slices bytes are compared using bytes.Compare().

Maps with less elements return a less result. Maps with equal number of keys are compared for same kinds, then key names and finally equal values. Comparison is done in ascending order after converting keys to strings/bytes.

Pointer types are dereferenced do their values before comparison. Untyped pointers are compared by their address numerically.

Complex numbers are compared as strings.

Channel and func types are not supported, are ignored and will return 0.

If an error occurs it is returned with a compare value that should be disregarded.

func FilterStruct

func FilterStruct(in interface{}, filter ...string) interface{}

FilterStruct returns a copy of in struct with specified fields removed. In must be a pointer to a struct or a struct value. Values of non-filtered fields are not copied from the source to result. Returned value is a struct value or nil in case of an error.

func LazyStructCopy

func LazyStructCopy(src, dst interface{}) error

LazyStructCopy copies values from src fields that have a coresponding field in dst to that field in dst. Fields must have same name and type. Tags are ignored. src and dest must be of struct type and addressable.

func StringToArrayValue

func StringToArrayValue(in string, out reflect.Value) error

StringToArrayValue converts a string to an array.

func StringToBoolValue

func StringToBoolValue(in string, out reflect.Value) error

StringToBoolValue converts a string to a bool.

func StringToComplex128Value

func StringToComplex128Value(in string, out reflect.Value) error

StringToComplex128Value converts a string to a complex128.

func StringToComplex64Value

func StringToComplex64Value(in string, out reflect.Value) error

StringToComplex64Value converts a string to a complex64.

func StringToFloat32Value

func StringToFloat32Value(in string, out reflect.Value) error

StringToFloat32Value converts a string to a float32.

func StringToFloat64Value

func StringToFloat64Value(in string, out reflect.Value) error

StringToFloat64Value converts a string to a float64.

func StringToIntValue

func StringToIntValue(in string, out reflect.Value) error

StringToIntValue converts a string to a int of any width.

func StringToInterface

func StringToInterface(in string, out interface{}) error

StringToInterface converts string in to out which must be a pointer to an allocated memory defining a type compatible to data contained in string according to rules defined in description of StringToValue.

func StringToMapValue

func StringToMapValue(in string, out reflect.Value) error

StringToMapValue converts a string to a map.

func StringToPointerValue

func StringToPointerValue(in string, out reflect.Value) error

StringToPointerValue converts a string to a pointer.

func StringToSliceValue

func StringToSliceValue(in string, out reflect.Value) error

StringToSliceValue converts a string to a slice.

func StringToStringValue

func StringToStringValue(in string, out reflect.Value) error

StringToStringValue converts a string to a string. A real eye opener.

func StringToStructValue

func StringToStructValue(in string, out reflect.Value) error

StringToStructValue converts a string to a struct.

func StringToUintValue

func StringToUintValue(in string, out reflect.Value) error

StringToUintValue converts a string to an uint of any width.

func StringToValue

func StringToValue(in string, out reflect.Value) error

StringToValue intends to set out to a value parsed from in which must be convertible to out or an error is returned. If out is a compound type its' value(s) is replaced.

StringToValue tries to be a one call converter to many different value kinds in one unifying call, primarily for conversion of simple types such as bools and numbers and types which implement TextUnmarshaler. Parsing compound types like arrays, slices, maps and structs requires a defined syntax so a simple, possibly logical syntax is implemented for completeness sake, as described:

Array and Slice: Values delimited by comma. Example: 0,1,2,3,4

Map: Key=Value pairs delimited by comma. Example:key1=value1,key2=value2,keyN=valueN

Struct: TODO Example:TODO

Chans and func are unsupported.

If an error occurs it is returned.

func StructPartialEqual

func StructPartialEqual(x, y interface{}) bool

StructPartialEqual compares two structs and tells if there is at least one field in both that match both by name and type. Tags in both x and y are ignored.

Types

This section is empty.

Jump to

Keyboard shortcuts

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