Documentation ¶
Index ¶
- func DeepEqual(x, y interface{}) bool
- func Diff(expected interface{}, actual interface{}) string
- func EveryField(i interface{}, f func(t *StructField, v *Value) (bool, error)) error
- func GetTag(structValue interface{}, fieldName string, tagName string) string
- func Implements(subject interface{}, interfaceValue interface{}) bool
- func IsFunction(arg interface{}) bool
- func IsIndirectStruct(i interface{}) bool
- func IsPointer(i interface{}) bool
- func IsStruct(i interface{}) bool
- func LookupTag(structValue interface{}, fieldName string, tagName string) (string, bool)
- func ObjectsAreEqual(expected interface{}, actual interface{}) bool
- func TypeAndKind(v interface{}) (Type, Kind)
- type Kind
- type StructField
- type StructFieldPair
- type StructTag
- type Type
- type TypeData
- func (typeData TypeData) AnyField(f func(i int, field StructField) bool) bool
- func (typeData TypeData) ConvertibleTo(u Type) bool
- func (typeData TypeData) Elem() Type
- func (typeData TypeData) Field(i int) StructField
- func (typeData TypeData) Implements(u Type) bool
- func (typeData TypeData) IsInterface() bool
- func (typeData TypeData) IsStruct() bool
- func (typeData TypeData) Key() Type
- func (typeData TypeData) Kind() Kind
- func (typeData TypeData) KindName() string
- func (typeData TypeData) Len() int
- func (typeData TypeData) Name() string
- func (typeData TypeData) NumField() int
- func (typeData TypeData) SelectField(f func(i int, field StructField) bool) []StructField
- func (typeData TypeData) Size() uintptr
- type Value
- func (v Value) Addr() Value
- func (v Value) Bool() bool
- func (v Value) Bytes() []byte
- func (v Value) Call(in []Value) []Value
- func (v Value) CallSlice(in []Value) []Value
- func (v Value) CanAddr() bool
- func (v Value) CanInterface() bool
- func (v Value) CanSet() bool
- func (v Value) Cap() int
- func (v Value) Close()
- func (v Value) Complex() complex128
- func (v Value) Convert(t Type) Value
- func (v Value) Elem() Value
- func (v Value) Field(i int) Value
- func (v Value) FieldByIndex(index []int) Value
- func (v Value) FieldByName(name string) Value
- func (v Value) FieldByNameFunc(match func(string) bool) Value
- func (v Value) Float() float64
- func (v Value) Index(i int) Value
- func (v Value) Interface() interface{}
- func (v Value) InterfaceData() [2]uintptr
- func (v Value) IsNil() bool
- func (v Value) IsValid() bool
- func (v Value) Kind() Kind
- func (v Value) Len() int
- func (v Value) MapKeys() []Value
- func (v Value) NumField() int
- func (v Value) Set(x Value)
- func (v Value) SetMapIndex(key, val Value)
- func (v Value) String() string
- func (v Value) Type() Type
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 ¶
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 ¶
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.
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 ¶
Get returns the associated value of a tag.
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 ¶
SliceOf returns the slice type with element type t. For example, if t represents int, SliceOf(t) represents []int.
type TypeData ¶
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 ¶
ConvertibleTo reports whether a value of the type is convertible to type u.
func (TypeData) Elem ¶
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 ¶
Implements reports whether the type implements the interface type u.
func (TypeData) IsInterface ¶
IsInterface returns true if the value is an interface.
func (TypeData) IsStruct ¶
IsStruct returns true if the value is a struct.
func (TypeData) Key ¶
Key returns a map type's key type. It panics if the type's Kind is not Map.
func (TypeData) Kind ¶
Kind returns the Kind value of a typeData.
func (TypeData) KindName ¶
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 ¶
Len returns an array type's length. It panics if the type's Kind is not Array.
func (TypeData) Name ¶
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 ¶
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.
type 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 ¶
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 ¶
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 ¶
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 MakeSlice ¶
MakeSlice creates a new zero-initialized slice value for the specified slice type, length, and capacity.
func New ¶
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 (Value) Addr ¶
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 ¶
Bool returns v's underlying value. It panics if v's kind is not Bool.
func (Value) Bytes ¶
Bytes returns v's underlying value. It panics if v's underlying value is not a slice of bytes.
func (Value) Call ¶
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 ¶
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 ¶
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 ¶
CanInterface reports whether Interface can be used without panicking.
func (Value) CanSet ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
FieldByIndex returns the nested field corresponding to index. It panics if v's Kind is not struct.
func (Value) FieldByName ¶
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 ¶
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 ¶
Float returns v's underlying value, as a float64. It panics if v's Kind is not Float32 or Float64
func (Value) Index ¶
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 ¶
InterfaceData returns the interface v's value as a uintptr pair. It panics if v's Kind is not Interface.
func (Value) IsNil ¶
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 ¶
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 ¶
Kind returns v's Kind. If v is the zero Value (IsValid returns false), Kind returns Invalid.
func (Value) Len ¶
Len returns v's length. It panics if v's Kind is not Array, Chan, Map, Slice, or String.
func (Value) MapKeys ¶
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 ¶
NumField returns the number of fields in the struct v. It panics if v's Kind is not Struct.
func (Value) Set ¶
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 ¶
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 ¶
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.