Documentation ¶
Index ¶
- Variables
- func CanStringToValueOfType(t reflect.Type) bool
- func CheckFunctionSignature(f interface{}, args, results []reflect.Type) error
- func CheckFunctionSignatureKind(f interface{}, args, results []reflect.Kind) error
- func CopyExportedStructFields(src, dstPtr interface{}) (copied int)
- func CopyExportedStructFieldsVal(src, dst reflect.Value) (copied int)
- func DereferenceValue(v reflect.Value) (result reflect.Value, ok bool)
- func ExportedStructFields(v reflect.Value) map[string]reflect.Value
- func FindFlattenedStructField(t reflect.Type, matchFunc MatchStructFieldFunc) *reflect.StructField
- func GenericSlice(sliceOrArray interface{}) []interface{}
- func GetStruct(s interface{}) reflect.Value
- func IsDefaultValue(value interface{}) bool
- func IsExportedField(structField reflect.StructField) bool
- func IsExportedName(name string) bool
- func IsNilOrWrappedNil(i interface{}) bool
- func NewInstance(prototype interface{}) interface{}
- func Reset(resultRef interface{})
- func SetStructZero(structVal reflect.Value)
- func SmartCopy(source, resultRef interface{})
- func Sort(slice, compareFunc interface{})
- func StringToValueOfType(s string, t reflect.Type) (interface{}, error)
- func VisitStruct(strct interface{}, visitor StructVisitor) error
- func VisitStructDepth(strct interface{}, visitor StructVisitor, maxDepth int) error
- type LogStructVisitor
- func (self *LogStructVisitor) ArrayField(depth int, v reflect.Value, index int) error
- func (self *LogStructVisitor) BeginArray(depth int, v reflect.Value) error
- func (self *LogStructVisitor) BeginMap(depth int, v reflect.Value) error
- func (self *LogStructVisitor) BeginSlice(depth int, v reflect.Value) error
- func (self *LogStructVisitor) BeginStruct(depth int, v reflect.Value) error
- func (self *LogStructVisitor) EndArray(depth int, v reflect.Value) error
- func (self *LogStructVisitor) EndMap(depth int, v reflect.Value) error
- func (self *LogStructVisitor) EndSlice(depth int, v reflect.Value) error
- func (self *LogStructVisitor) EndStruct(depth int, v reflect.Value) error
- func (self *LogStructVisitor) MapField(depth int, v reflect.Value, key string, index int) error
- func (self *LogStructVisitor) SliceField(depth int, v reflect.Value, index int) error
- func (self *LogStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error
- type MatchStructFieldFunc
- type ModifySliceStructVisitor
- func (self ModifySliceStructVisitor) ArrayField(depth int, v reflect.Value, index int) error
- func (self ModifySliceStructVisitor) BeginArray(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) BeginMap(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) BeginSlice(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) BeginStruct(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) EndArray(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) EndMap(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) EndSlice(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) EndStruct(depth int, v reflect.Value) error
- func (self ModifySliceStructVisitor) MapField(depth int, v reflect.Value, key string, index int) error
- func (self ModifySliceStructVisitor) ModifySlice(depth int, v reflect.Value) (reflect.Value, error)
- func (self ModifySliceStructVisitor) SliceField(depth int, v reflect.Value, index int) error
- func (self ModifySliceStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error
- type SortCompareFunc
- type StructVisitor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // TypeOfError is the built-in error type TypeOfError = reflect.TypeOf((*error)(nil)).Elem() // TypeOfInterface is the type of an empty interface{} TypeOfInterface = reflect.TypeOf((*interface{})(nil)).Elem() )
Built-in types
Functions ¶
func CanStringToValueOfType ¶
func CheckFunctionSignature ¶
func CopyExportedStructFields ¶
func CopyExportedStructFields(src, dstPtr interface{}) (copied int)
CopyExportedStructFields copies all exported struct fields from src that are assignable to their name siblings at dstPtr to dstPtr. src can be a struct or a pointer to a struct, dstPtr must be a pointer to a struct.
func DereferenceValue ¶
DereferenceValue recursively dereferences v if it is a pointer or interface. It will return ok == false if nil is encountered.
func ExportedStructFields ¶
ExportedStructFields returns a map from exported struct field names to values, inlining anonymous sub-structs so that their field names are available at the base level. Example:
type A struct { X int } type B Struct { A Y int } // Yields X and Y instead of A and Y: InlineAnonymousStructFields(reflect.ValueOf(B{}))
func FindFlattenedStructField ¶
func FindFlattenedStructField(t reflect.Type, matchFunc MatchStructFieldFunc) *reflect.StructField
func GenericSlice ¶
func GenericSlice(sliceOrArray interface{}) []interface{}
func GetStruct ¶
GetStruct returns reflect.Value for the struct found in s. s can be a struct, a struct pointer or a reflect.Value of a struct or struct pointer.
func IsDefaultValue ¶
func IsDefaultValue(value interface{}) bool
func IsExportedField ¶
func IsExportedField(structField reflect.StructField) bool
func IsExportedName ¶
func IsNilOrWrappedNil ¶
func IsNilOrWrappedNil(i interface{}) bool
IsNilOrWrappedNil returns if i is nil, or wraps a nil pointer in a non nil interface.
func NewInstance ¶
func NewInstance(prototype interface{}) interface{}
Creates a new zero valued instance of prototype
func Reset ¶
func Reset(resultRef interface{})
Reset sets all elements of the object pointed to by resultRef to their default or zero values. But it works different from simply zeroing out everything, here are the exceptions: If resultRef is a pointer to a pointer, then the pointed to pointer will be reset to a new instance If resultRef is a pointer to a map, then the map will be reset to a new empty one. All other types pointed to by resultRef will be set to their default zero values.
func SetStructZero ¶
SetStructZero sets all elements of a struct to their zero values.
func SmartCopy ¶
func SmartCopy(source, resultRef interface{})
SmartCopy copies struct or map fields from source to equally named struct or map fields of resultRef, by dereferencing source and resultRef if necessary to find a matching assignable type. All fields of the object referenced by resultPtr will be set to their default values before copying from source by calling Reset(). SmartCopy is typically used for iterators with a method Next(resultRef interface{}) bool.
func Sort ¶
func Sort(slice, compareFunc interface{})
Sort will sort slice according to compareFunc using reflection. slice can be a slice of any element type including interface{}. compareFunc must have two arguments that are assignable from the slice element type or pointers to such a type. The result of compareFunc must be a bool indicating if the first argument is less than the second. If the element type of slice is interface{}, then the type of the compareFunc arguments can be any type and dynamic casting from the interface value or its address will be attempted.
func VisitStruct ¶
func VisitStruct(strct interface{}, visitor StructVisitor) error
VisitStruct visits recursively all exported fields of a struct and reports them via StructVisitor methods. If a StructVisitor method returns an error, the visitation is aborted and the error returned as result. Pointers and interfaces are dereferenced silently until a non nil value is found. Structs that are embedded anonymously are inlined so that their fields are reported as fields of the embedding struct at the same depth. Anonymous struct fields that are not structs themselves are omitted. Struct fields with the tag gostart:"-" are ignored.
Example (DeepAnonymousStructFields) ¶
type A struct { A0 interface{} A1 int } type B struct { B0 bool A B1 bool } type C struct { A C0 string *B C1 string } type exampleStruct struct { C } val := &exampleStruct{ C: C{ A: A{A0: 0, A1: 1}, C0: "C0", B: &B{ B0: false, A: A{A0: 0, A1: 1}, B1: true, }, C1: "C1", }, } VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: A0 int = 0) StructField(1: A1 int = 1) StructField(2: C0 string = "C0") StructField(3: B0 bool = false) StructField(4: A0 int = 0) StructField(5: A1 int = 1) StructField(6: B1 bool = true) StructField(7: C1 string = "C1") EndStruct(reflection.exampleStruct)
Example (DeepStructFields) ¶
type A struct { A0 interface{} A1 int } type B struct { B0 bool A A B1 bool } type C struct { A A C0 string B *B C1 string } type exampleStruct struct { C C } val := &exampleStruct{ C: C{ A: A{A0: 0, A1: 1}, C0: "C0", B: &B{ B0: false, A: A{A0: 0, A1: 1}, B1: true, }, C1: "C1", }, } VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: C reflection.C) BeginStruct(reflection.C) StructField(0: A reflection.A) BeginStruct(reflection.A) StructField(0: A0 int = 0) StructField(1: A1 int = 1) EndStruct(reflection.A) StructField(1: C0 string = "C0") StructField(2: B reflection.B) BeginStruct(reflection.B) StructField(0: B0 bool = false) StructField(1: A reflection.A) BeginStruct(reflection.A) StructField(0: A0 int = 0) StructField(1: A1 int = 1) EndStruct(reflection.A) StructField(2: B1 bool = true) EndStruct(reflection.B) StructField(3: C1 string = "C1") EndStruct(reflection.C) EndStruct(reflection.exampleStruct)
Example (FlatAnonymousStructFields) ¶
type A struct { A0 int A1 interface{} A2 int } type B struct { B0 bool B1 bool } type exampleStruct struct { A *B } val := &exampleStruct{ A: A{A0: 0, A1: 1, A2: 2}, B: &B{B0: false, B1: true}, } VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: A0 int = 0) StructField(1: A1 int = 1) StructField(2: A2 int = 2) StructField(3: B0 bool = false) StructField(4: B1 bool = true) EndStruct(reflection.exampleStruct)
Example (FlatStructWithSliceAndArray) ¶
type exampleStruct struct { X int hidden bool Slice []int Array [3]int } val := &exampleStruct{ X: 9, Slice: []int{1, 2}, Array: [...]int{1, 2, 3}, } VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: X int = 9) StructField(1: Slice []int) BeginSlice([]int) SliceField(0: int = 1) SliceField(1: int = 2) EndSlice([]int) StructField(2: Array [3]int) BeginArray([3]int) ArrayField(0: int = 1) ArrayField(1: int = 2) ArrayField(2: int = 3) EndArray([3]int) EndStruct(reflection.exampleStruct)
Example (LimitDepth) ¶
type A struct { A0 interface{} A1 int } type B struct { B0 bool A A B1 bool } type C struct { A A C0 string B *B C1 string } type exampleStruct struct { C C } val := &exampleStruct{ C: C{ A: A{A0: 0, A1: 1}, C0: "C0", B: &B{ B0: false, A: A{A0: 0, A1: 1}, B1: true, }, C1: "C1", }, } VisitStructDepth(val, NewStdLogStructVisitor(), 2)
Output: BeginStruct(reflection.exampleStruct) StructField(0: C reflection.C) BeginStruct(reflection.C) StructField(0: A reflection.A) BeginStruct(reflection.A) EndStruct(reflection.A) StructField(1: C0 string = "C0") StructField(2: B reflection.B) BeginStruct(reflection.B) EndStruct(reflection.B) StructField(3: C1 string = "C1") EndStruct(reflection.C) EndStruct(reflection.exampleStruct)
Example (SimpleFlatPtrsStruct) ¶
type exampleStruct struct { X *int Y **float32 Z *string N **string I interface{} } val := &exampleStruct{ X: new(int), Y: new(*float32), Z: nil, N: new(*string), I: new(int), } *val.X = 1 *val.Y = new(float32) **val.Y = 2 VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: X int = 1) StructField(1: Y float32 = 2) StructField(2: I int = 0) EndStruct(reflection.exampleStruct)
Example (SimpleFlatStruct) ¶
type exampleStruct struct { X int Y float32 Z string hidden string } VisitStruct(&exampleStruct{X: 1}, NewStdLogStructVisitor()) VisitStruct(exampleStruct{Y: 2}, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: X int = 1) StructField(1: Y float32 = 0) StructField(2: Z string = "") EndStruct(reflection.exampleStruct) BeginStruct(reflection.exampleStruct) StructField(0: X int = 0) StructField(1: Y float32 = 2) StructField(2: Z string = "") EndStruct(reflection.exampleStruct)
Example (SliceOfStructInAnonymousStruct) ¶
type emailIdentity struct { Address string Description string } type user struct { Name string Email []emailIdentity } type person struct { user ExtraInfo string } val := &person{ user: user{ Name: "Erik Unger", Email: []emailIdentity{ emailIdentity{Address: "erik@erikunger.com", Description: "Test"}, }, }, ExtraInfo: "info", } VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.person) StructField(0: Name string = "Erik Unger") StructField(1: Email []reflection.emailIdentity) BeginSlice([]reflection.emailIdentity) SliceField(0: reflection.emailIdentity) BeginStruct(reflection.emailIdentity) StructField(0: Address string = "erik@erikunger.com") StructField(1: Description string = "Test") EndStruct(reflection.emailIdentity) EndSlice([]reflection.emailIdentity) StructField(2: ExtraInfo string = "info") EndStruct(reflection.person)
func VisitStructDepth ¶
func VisitStructDepth(strct interface{}, visitor StructVisitor, maxDepth int) error
VisitStructDepth is identical to VisitStruct except that its recursive depth is limited to maxDepth with the first depth level being zero. If maxDepth is -1, then the recursive depth is unlimited (VisitStruct).
Types ¶
type LogStructVisitor ¶
LogStructVisitor can be used for testing and debugging VisitStruct()
func NewStdLogStructVisitor ¶
func NewStdLogStructVisitor() *LogStructVisitor
func (*LogStructVisitor) ArrayField ¶
func (*LogStructVisitor) BeginArray ¶
func (self *LogStructVisitor) BeginArray(depth int, v reflect.Value) error
func (*LogStructVisitor) BeginMap ¶
func (self *LogStructVisitor) BeginMap(depth int, v reflect.Value) error
func (*LogStructVisitor) BeginSlice ¶
func (self *LogStructVisitor) BeginSlice(depth int, v reflect.Value) error
func (*LogStructVisitor) BeginStruct ¶
func (self *LogStructVisitor) BeginStruct(depth int, v reflect.Value) error
func (*LogStructVisitor) EndArray ¶
func (self *LogStructVisitor) EndArray(depth int, v reflect.Value) error
func (*LogStructVisitor) EndMap ¶
func (self *LogStructVisitor) EndMap(depth int, v reflect.Value) error
func (*LogStructVisitor) EndSlice ¶
func (self *LogStructVisitor) EndSlice(depth int, v reflect.Value) error
func (*LogStructVisitor) EndStruct ¶
func (self *LogStructVisitor) EndStruct(depth int, v reflect.Value) error
func (*LogStructVisitor) SliceField ¶
func (*LogStructVisitor) StructField ¶
func (self *LogStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error
type MatchStructFieldFunc ¶
type MatchStructFieldFunc func(field *reflect.StructField) bool
type ModifySliceStructVisitor ¶
ModifySliceStructVisitor is a StructVisitor that calls its self function value in BeginSlice() and ignores all other StructVisitor methos. It can be used to modify the length of slices in complex structs.
func (ModifySliceStructVisitor) ArrayField ¶
func (ModifySliceStructVisitor) BeginArray ¶
func (self ModifySliceStructVisitor) BeginArray(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) BeginMap ¶
func (self ModifySliceStructVisitor) BeginMap(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) BeginSlice ¶
func (self ModifySliceStructVisitor) BeginSlice(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) BeginStruct ¶
func (self ModifySliceStructVisitor) BeginStruct(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) EndArray ¶
func (self ModifySliceStructVisitor) EndArray(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) EndMap ¶
func (self ModifySliceStructVisitor) EndMap(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) EndSlice ¶
func (self ModifySliceStructVisitor) EndSlice(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) EndStruct ¶
func (self ModifySliceStructVisitor) EndStruct(depth int, v reflect.Value) error
func (ModifySliceStructVisitor) ModifySlice ¶
func (ModifySliceStructVisitor) SliceField ¶
func (ModifySliceStructVisitor) StructField ¶
func (self ModifySliceStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error
type SortCompareFunc ¶
func NewSortCompareFunc ¶
func NewSortCompareFunc(compareFunc interface{}) (*SortCompareFunc, error)
func (*SortCompareFunc) Sort ¶
func (self *SortCompareFunc) Sort(slice interface{}) error
type StructVisitor ¶
type StructVisitor interface { BeginStruct(depth int, v reflect.Value) error StructField(depth int, v reflect.Value, f reflect.StructField, index int) error EndStruct(depth int, v reflect.Value) error BeginSlice(depth int, v reflect.Value) error SliceField(depth int, v reflect.Value, index int) error EndSlice(depth int, v reflect.Value) error BeginArray(depth int, v reflect.Value) error ArrayField(depth int, v reflect.Value, index int) error EndArray(depth int, v reflect.Value) error BeginMap(depth int, v reflect.Value) error MapField(depth int, v reflect.Value, key string, index int) error EndMap(depth int, v reflect.Value) error }