reflectutil

package
v0.0.0-...-8cba18c Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 7 Imported by: 0

README

go-common/reflectutil

This repository contains the go-common/reflectutil library.

To install:

go get github.com/ugorji/go-common/reflectutil

Package Documentation

Package reflectutil provides reflection utilities.

Exported Package API

const MaxArrayLen = 1<<((32<<(^uint(0)>>63))-1) - 1
func AllFieldNames(rt reflect.Type, exported bool, names map[string]reflect.StructField) map[string]reflect.StructField
func ApproxDataSize(rv reflect.Value) (sum int)
func Coerce(val interface{}, typ interface{}) (interface{}, error)
func CoerceRV(rval reflect.Value, rtyp reflect.Type) (reflect.Value, error)
func DeepEqual(v1, v2 interface{}, strict bool) (err error)
func ExpandSliceValue(s reflect.Value, num int) reflect.Value
func GrowCap(oldCap, unit, num uint) (newCap uint)
func ImplementsInterface(typ, iTyp reflect.Type) (success bool, indir int8)
func Indir(rv reflect.Value, finalTyp reflect.Type, maxDepth int) reflect.Value
func IndirIntf(v interface{}, finalTyp reflect.Type, maxDepth int) interface{}
func IsEmptyValue(v reflect.Value, deref, checkStruct bool) bool
func ToGeneric(in interface{}) (out interface{})

Documentation

Overview

Package reflectutil provides reflection utilities.

Index

Constants

View Source
const MaxArrayLen = 1<<((32<<(^uint(0)>>63))-1) - 1

MaxArrayLen is the size of uint, which determines the maximum length of any array.

Variables

This section is empty.

Functions

func AllFieldNames

func AllFieldNames(rt reflect.Type, exported bool, names map[string]reflect.StructField,
) map[string]reflect.StructField

Return all the field names, mapped to their struct fields. It does this recursively, getting for anonymous fields also.

func ApproxDataSize

func ApproxDataSize(rv reflect.Value) (sum int)

func Coerce

func Coerce(val interface{}, typ interface{}) (interface{}, error)

Coerce a value into one of a different type. Returns the coerced value. Delegates to CoerceRV. (Wrapper of CoerceRV, without need to import reflect) Example:

Coerce(int(0), bool(false)) ==> returns a bool
Coerce([]interface{}{"a", "b", "c"}, []string(nil)) ==> []string{"a", "b", "c"}

func CoerceRV

func CoerceRV(rval reflect.Value, rtyp reflect.Type) (reflect.Value, error)

Coerce a value into one of a different type. See table below for source and possible destinations.

  SOURCE    POSSIBLE DESTINATIONS
  ===================================
- intXXX:   floatXXX, intXXX, uintXXX
- floatXXX: floatXXX, intXXX, uintXXX
- uintXXX:  floatXXX, intXXX, uintXXX
- string:   ANY (using fmt.Sprintf)
- bool:     ANY (using strconv.Atob)
- slice:    slice of any of above
- map:      mapping of any of above, or struct
- struct:   set fields based on field names of struct, or of passed map

func DeepEqual

func DeepEqual(v1, v2 interface{}, strict bool) (err error)

This checks 2 interfaces to see if they are the same. If strict=true, then checks that they are also same type (while walking the interfaces). It skips functions, channels, and non-exported fields in structs. It is better than the DeepEqual in reflect because it gives contextual information back in the error on what was wrong.

TODO: let error be a multi error of all the differences in the value (not just first one).

func ExpandSliceValue

func ExpandSliceValue(s reflect.Value, num int) reflect.Value

func GrowCap

func GrowCap(oldCap, unit, num uint) (newCap uint)

GrowCap will return a new capacity for a slice, given the following:

  • oldCap: current capacity
  • unit: in-memory size of an element
  • num: number of elements to add

func ImplementsInterface

func ImplementsInterface(typ, iTyp reflect.Type) (success bool, indir int8)

func Indir

func Indir(rv reflect.Value, finalTyp reflect.Type, maxDepth int) reflect.Value

IndirIntf will take an interface and loop indirections on it till you get the real value. It is more critical for nil interfaces, as we can easily get lost in the indirection hell.

func IndirIntf

func IndirIntf(v interface{}, finalTyp reflect.Type, maxDepth int) interface{}

IndirIntf will take an interface and loop indirections on it till you get the real value.

func IsEmptyValue

func IsEmptyValue(v reflect.Value, deref, checkStruct bool) bool

func ToGeneric

func ToGeneric(in interface{}) (out interface{})

ToGeneric takes an interface{} and returns a Value interface{} with all references to custom types removed. The returned value contains only built-in data primitives (intX, uintX, floatX, bool, String, Slice, Map).

Types

This section is empty.

Jump to

Keyboard shortcuts

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