reflect

package module
v0.0.0-...-437f4da Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2019 License: GPL-3.0 Imports: 5 Imported by: 0

README

pipeline status coverage report

Reflect

A reflection utility library for common reflection operations.

Why?

The standard library reflect package requires a lot of repetitive code to get useful information. This reflect utility library acts as a wrapper around the standard reflect package while also adding additional functionality.

Specific wrappers are added as they are needed as deemed by other projects.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual

func DeepEqual(x, y interface{}) bool

DeepEqual reports whether x and y are “deeply equal,” defined as follows. Two values of identical type are deeply equal if one of the following cases applies. Values of distinct types are never deeply equal.

Array values are deeply equal when their corresponding elements are deeply equal.

Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.

Func values are deeply equal if both are nil; otherwise they are not deeply equal.

Interface values are deeply equal if they hold deeply equal concrete values.

Map values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they are the same map object or their corresponding keys (matched using Go equality) map to deeply equal values.

Pointer values are deeply equal if they are equal using Go's == operator or if they point to deeply equal values.

Slice values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.

Other values - numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.

In general DeepEqual is a recursive relaxation of Go's == operator. However, this idea is impossible to implement without some inconsistency. Specifically, it is possible for a value to be unequal to itself, either because it is of func type (uncomparable in general) or because it is a floating-point NaN value (not equal to itself in floating-point comparison), or because it is an array, struct, or interface containing such a value. On the other hand, pointer values are always equal to themselves, even if they point at or contain such problematic values, because they compare equal using Go's == operator, and that is a sufficient condition to be deeply equal, regardless of content. DeepEqual has been defined so that the same short-cut applies to slices and maps: if x and y are the same slice or the same map, they are deeply equal regardless of content.

As DeepEqual traverses the data values it may find a cycle. The second and subsequent times that DeepEqual compares two pointer values that have been compared before, it treats the values as equal rather than examining the values to which they point. This ensures that DeepEqual terminates.

func Diff

func Diff(expected interface{}, actual interface{}) string

Diff returns a string describing what is different between two values.

func EveryField

func EveryField(i interface{}, f func(t *StructField, v *Value) (bool, error)) error

EveryField calls function f for every fields in the passed structure. F returns a booleaning indication wether to continue to iterate and an error. If an error occurs iteration over the structure fields ceases.

func GetTag

func GetTag(structValue interface{}, fieldName string, tagName string) string

GetTag returns the tag value of the specified fieldName in structValue that has the tag tagName. If the tag was not able to found, an empty string is returned.

func Implements

func Implements(subject interface{}, interfaceValue interface{}) bool

Implements returns true if subject implements the interface interfaceValue

func IsFunction

func IsFunction(arg interface{}) bool

IsFunction returns true if the passed value is a function.

func IsIndirectStruct

func IsIndirectStruct(i interface{}) bool

IsIndirectStruct determines whether or not a value is a pointer to a struct or a struct

func IsPointer

func IsPointer(i interface{}) bool

IsPointer returns true if the passed interface is a pointer value

func IsStruct

func IsStruct(i interface{}) bool

IsStruct returns true if the passed interface is a struct

func LookupTag

func LookupTag(structValue interface{}, fieldName string, tagName string) (string, bool)

LookupTag returns the tag value of the specified fieldName in structValue that has the tag tagName and a boolean denoting whether the value was found.

func ObjectsAreEqual

func ObjectsAreEqual(expected interface{}, actual interface{}) bool

ObjectsAreEqual compares two values for whether they are equal, traversing the data structure as per the rules of reflect.DeepEqual. in some instances reflect.DeepEqual is not used in favor of a more direct approach.

func TypeAndKind

func TypeAndKind(v interface{}) (Type, Kind)

TypeAndKind returns the type and kind of a value. If the value is a pointer, it is resolved to the type and kind of what it points to.

Types

type Kind

type Kind uint

Kind is a wrapper around the standard library kind (reflect.Kind) A Kind represents the specific kind of type that a Type represents. The zero Kind is not a valid kind.

const (

	// Invalid type
	Invalid Kind = iota

	// Bool type
	Bool

	// Int type
	Int

	// Int8 type
	Int8

	// Int16 type
	Int16

	// Int32 type
	Int32

	//Int64 type
	Int64

	// Uint type
	Uint

	//Uint8 type
	Uint8

	//Uint16 type
	Uint16

	//Uint32 type
	Uint32

	//Uint64 type
	Uint64

	//Uintptr type
	Uintptr

	//Float32 type
	Float32

	//Float64 type
	Float64

	//Complex64 type
	Complex64

	//Complex128 type
	Complex128

	//Array type
	Array

	//Chan type
	Chan

	//Func type
	Func

	//Interface type
	Interface

	//Map type
	Map

	//Ptr type
	Ptr

	//Slice type
	Slice

	//String type
	String

	//Struct type
	Struct

	//UnsafePointer type
	UnsafePointer
)

type StructField

type StructField struct {
	greflect.StructField
	Type Type      // field type
	Tag  StructTag // field tag string
}

StructField is a wrapper around the standard library reflect.StructField, but with some enhancements that make reflection easier.

func (StructField) IsExported

func (structField StructField) IsExported() bool

IsExported returns true if a StructField is exported.

type StructFieldPair

type StructFieldPair struct {
	// The StructField of a structure as defined in the reflect package.
	Field *StructField

	// Value is the value of the struct field as define in the reflect package.
	Value *Value
}

StructFieldPair is a tuple value used to describe a field of a struct.

func SelectField

func SelectField(i interface{}, f func(t *StructField, v *Value) bool) []StructFieldPair

SelectField iterates through the struct fields of the provided structure, calling the function f on every field found. If f returns true, the field is return as in the result StructFields.

type StructTag

type StructTag string

StructTag represents a tag of a struct field.

func (StructTag) Get

func (tag StructTag) Get(key string) string

Get returns the associated value of a tag.

func (StructTag) Lookup

func (tag StructTag) Lookup(key string) (value string, ok bool)

Lookup returns the associated value of a tag, also returns a boolean indicated whether the tag value exists.

type Type

type Type interface {

	// ConvertibleTo reports whether a value of the type is convertible to type u.
	ConvertibleTo(u Type) bool

	// Elem returns a type's element type.
	// It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
	Elem() Type
	Field(i int) StructField
	//AnyField(f func(field StructField) bool)
	Implements(u Type) bool
	Key() Type
	Kind() Kind
	KindName() string
	Len() int
	NumField() int
	Name() string
	Size() uintptr

	// extentions
	SelectField(f func(i int, field StructField) bool) []StructField
	AnyField(f func(i int, field StructField) bool) bool
	IsStruct() bool
	IsInterface() bool
	// contains filtered or unexported methods
}

Type is a wrapper interface around the standard library that also add some extra methods.

func SliceOf

func SliceOf(t Type) Type

SliceOf returns the slice type with element type t. For example, if t represents int, SliceOf(t) represents []int.

func TypeOf

func TypeOf(i interface{}) Type

TypeOf returns the reflection Type that represents the dynamic type of i. If i is a nil interface value, TypeOf returns nil.

type TypeData

type TypeData struct {
	gReflect.Type
}

TypeData wraps the standard library type information.

func (TypeData) AnyField

func (typeData TypeData) AnyField(f func(i int, field StructField) bool) bool

AnyField visits all fields of the structure, passing in the position of the field in the struct and the StructField to f. If at any time f return true, AnyField returns true.

func (TypeData) ConvertibleTo

func (typeData TypeData) ConvertibleTo(u Type) bool

ConvertibleTo reports whether a value of the type is convertible to type u.

func (TypeData) Elem

func (typeData TypeData) Elem() Type

Elem returns a type's element type. It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.

func (TypeData) Field

func (typeData TypeData) Field(i int) StructField

Field returns a struct type's i'th field. It panics if the type's Kind is not Struct. It panics if i is not in the range [0, NumField()).

func (TypeData) Implements

func (typeData TypeData) Implements(u Type) bool

Implements reports whether the type implements the interface type u.

func (TypeData) IsInterface

func (typeData TypeData) IsInterface() bool

IsInterface returns true if the value is an interface.

func (TypeData) IsStruct

func (typeData TypeData) IsStruct() bool

IsStruct returns true if the value is a struct.

func (TypeData) Key

func (typeData TypeData) Key() Type

Key returns a map type's key type. It panics if the type's Kind is not Map.

func (TypeData) Kind

func (typeData TypeData) Kind() Kind

Kind returns the Kind value of a typeData.

func (TypeData) KindName

func (typeData TypeData) KindName() string

KindName returns the string representation of the kind value. Useful in debug situations where the kind is relevant, but its raw type is distracting or not useful (e.g. returns "Bool" instead of reflect.Bool, which is a number).

func (TypeData) Len

func (typeData TypeData) Len() int

Len returns an array type's length. It panics if the type's Kind is not Array.

func (TypeData) Name

func (typeData TypeData) Name() string

Name returns the type's name within its package for a defined type. For other (non-defined) types it returns the empty string.

func (TypeData) NumField

func (typeData TypeData) NumField() int

NumField returns a struct type's field count. It panics if the type's Kind is not Struct.

func (TypeData) SelectField

func (typeData TypeData) SelectField(f func(i int, field StructField) bool) []StructField

SelectField calls f on every field in the type. If f returns true, the StructField is appended to a working list. After visiting all fields in the struct the working list is returned.

func (TypeData) Size

func (typeData TypeData) Size() uintptr

Size returns the number of bytes needed to store a value of the given type; it is analogous to unsafe.Sizeof.

type Value

type Value struct {
	gReflect.Value
}

Value is wrapper for the standard library reflect.Value Value is the reflection interface to a Go value.

Not all methods apply to all kinds of values. Restrictions, if any, are noted in the documentation for each method. Use the Kind method to find out the kind of value before calling kind-specific methods. Calling a method inappropriate to the kind of type causes a run time panic.

The zero Value represents no value. Its IsValid method returns false, its Kind method returns Invalid, its String method returns "<invalid Value>", and all other methods panic. Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.

A Value can be used concurrently by multiple goroutines provided that the underlying Go value can be used concurrently for the equivalent direct operations.

To compare two Values, compare the results of the Interface method. Using == on two Values does not compare the underlying values they represent.

func Append

func Append(s Value, x ...Value) Value

Append appends the values x to a slice s and returns the resulting slice. As in Go, each x's value must be assignable to the slice's element type.

func AppendSlice

func AppendSlice(s, t Value) Value

AppendSlice appends a slice t to a slice s and returns the resulting slice. The slices s and t must have the same element type.

func Indirect

func Indirect(v Value) Value

Indirect returns the value that v points to. If v is a nil pointer, Indirect returns a zero Value. If v is not a pointer, Indirect returns v.

func MakeMap

func MakeMap(t Type) Value

MakeMap creates a new map with the specified type.

func MakeSlice

func MakeSlice(t Type, len, cap int) Value

MakeSlice creates a new zero-initialized slice value for the specified slice type, length, and capacity.

func New

func New(typ Type) Value

New returns a Value representing a pointer to a new zero value for the specified type. That is, the returned Value's Type is PtrTo(typ).

func ValueOf

func ValueOf(i interface{}) Value

ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value.

func Zero

func Zero(typ Type) Value

Zero returns the zero value of the given type.

func (Value) Addr

func (v Value) Addr() Value

Addr returns a pointer value representing the address of v. It panics if CanAddr() returns false. Addr is typically used to obtain a pointer to a struct field or slice element in order to call a method that requires a pointer receiver.

func (Value) Bool

func (v Value) Bool() bool

Bool returns v's underlying value. It panics if v's kind is not Bool.

func (Value) Bytes

func (v Value) Bytes() []byte

Bytes returns v's underlying value. It panics if v's underlying value is not a slice of bytes.

func (Value) Call

func (v Value) Call(in []Value) []Value

Call calls the function v with the input arguments in. For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]). Call panics if v's Kind is not Func. It returns the output results as Values. As in Go, each input argument must be assignable to the type of the function's corresponding input parameter. If v is a variadic function, Call creates the variadic slice parameter itself, copying in the corresponding values.

func (Value) CallSlice

func (v Value) CallSlice(in []Value) []Value

CallSlice calls the variadic function v with the input arguments in, assigning the slice in[len(in)-1] to v's final variadic argument. For example, if len(in) == 3, v.CallSlice(in) represents the Go call v(in[0], in[1], in[2]...). CallSlice panics if v's Kind is not Func or if v is not variadic. It returns the output results as Values. As in Go, each input argument must be assignable to the type of the function's corresponding input parameter.

func (Value) CanAddr

func (v Value) CanAddr() bool

CanAddr reports whether the value's address can be obtained with Addr. Such values are called addressable. A value is addressable if it is an element of a slice, an element of an addressable array, a field of an addressable struct, or the result of dereferencing a pointer. If CanAddr returns false, calling Addr will panic.

func (Value) CanInterface

func (v Value) CanInterface() bool

CanInterface reports whether Interface can be used without panicking.

func (Value) CanSet

func (v Value) CanSet() bool

CanSet reports whether the value of v can be changed. A Value can be changed only if it is addressable and was not obtained by the use of unexported struct fields. If CanSet returns false, calling Set or any type-specific setter (e.g., SetBool, SetInt) will panic.

func (Value) Cap

func (v Value) Cap() int

Cap returns v's capacity. It panics if v's Kind is not Array, Chan, or Slice.

func (Value) Close

func (v Value) Close()

Close closes the channel v. It panics if v's Kind is not Chan.

func (Value) Complex

func (v Value) Complex() complex128

Complex returns v's underlying value, as a complex128. It panics if v's Kind is not Complex64 or Complex128.

func (Value) Convert

func (v Value) Convert(t Type) Value

Convert returns the value v converted to type t. If the usual Go conversion rules do not allow conversion of the value v to type t, Convert panics.

func (Value) Elem

func (v Value) Elem() Value

Elem returns the value that the interface v contains or that the pointer v points to. It panics if v's Kind is not Interface or Ptr. It returns the zero Value if v is nil.

func (Value) Field

func (v Value) Field(i int) Value

Field returns the i'th field of the struct v. It panics if v's Kind is not Struct or i is out of range.

func (Value) FieldByIndex

func (v Value) FieldByIndex(index []int) Value

FieldByIndex returns the nested field corresponding to index. It panics if v's Kind is not struct.

func (Value) FieldByName

func (v Value) FieldByName(name string) Value

FieldByName returns the struct field with the given name. It returns the zero Value if no field was found. It panics if v's Kind is not struct.

func (Value) FieldByNameFunc

func (v Value) FieldByNameFunc(match func(string) bool) Value

FieldByNameFunc returns the struct field with a name that satisfies the match function. It panics if v's Kind is not struct. It returns the zero Value if no field was found.

func (Value) Float

func (v Value) Float() float64

Float returns v's underlying value, as a float64. It panics if v's Kind is not Float32 or Float64

func (Value) Index

func (v Value) Index(i int) Value

Index returns v's i'th element. It panics if v's Kind is not Array, Slice, or String or i is out of range.

func (Value) Interface

func (v Value) Interface() interface{}

Interface returns v's current value as an interface{}. It is equivalent to:

var i interface{} = (v's underlying value)

It panics if the Value was obtained by accessing unexported struct fields.

func (Value) InterfaceData

func (v Value) InterfaceData() [2]uintptr

InterfaceData returns the interface v's value as a uintptr pair. It panics if v's Kind is not Interface.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil reports whether its argument v is nil. The argument must be a chan, func, interface, map, pointer, or slice value; if it is not, IsNil panics. Note that IsNil is not always equivalent to a regular comparison with nil in Go. For example, if v was created by calling ValueOf with an uninitialized interface variable i, i==nil will be true but v.IsNil will panic as v will be the zero Value.

func (Value) IsValid

func (v Value) IsValid() bool

IsValid reports whether v represents a value. It returns false if v is the zero Value. If IsValid returns false, all other methods except String panic. Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.

func (Value) Kind

func (v Value) Kind() Kind

Kind returns v's Kind. If v is the zero Value (IsValid returns false), Kind returns Invalid.

func (Value) Len

func (v Value) Len() int

Len returns v's length. It panics if v's Kind is not Array, Chan, Map, Slice, or String.

func (Value) MapKeys

func (v Value) MapKeys() []Value

MapKeys returns a slice containing all the keys present in the map, in unspecified order. It panics if v's Kind is not Map. It returns an empty slice if v represents a nil map.

func (Value) NumField

func (v Value) NumField() int

NumField returns the number of fields in the struct v. It panics if v's Kind is not Struct.

func (Value) Set

func (v Value) Set(x Value)

Set assigns x to the value v. It panics if CanSet returns false. As in Go, x's value must be assignable to v's type.

func (Value) SetMapIndex

func (v Value) SetMapIndex(key, val Value)

SetMapIndex sets the value associated with key in the map v to val. It panics if v's Kind is not Map. If val is the zero Value, SetMapIndex deletes the key from the map. Otherwise if v holds a nil map, SetMapIndex will panic. As in Go, key's value must be assignable to the map's key type, and val's value must be assignable to the map's value type.

func (Value) String

func (v Value) String() string

String returns the string v's underlying value, as a string. String is a special case because of Go's String method convention. Unlike the other getters, it does not panic if v's Kind is not String. Instead, it returns a string of the form "<T value>" where T is v's type. The fmt package treats Values specially. It does not call their String method implicitly but instead prints the concrete values they hold.

func (Value) Type

func (v Value) Type() Type

Type returns v's type.

Jump to

Keyboard shortcuts

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