Documentation
¶
Index ¶
- Variables
- func CallMethod(object interface{}, method string, args ...interface{}) (results []interface{}, err error)
- func CallMethod1(object interface{}, method string, args ...interface{}) (result interface{}, err error)
- func CopyExportedStructFields(src, dst interface{}) (copied int)
- func DereferenceValue(v reflect.Value) (result reflect.Value, ok bool)
- func ExportedStructFields(s interface{}) map[string]interface{}
- func FindFlattenedStructField(t reflect.Type, matchFunc MatchStructFieldFunc) *reflect.StructField
- func IsDeepNil(i interface{}) bool
- func IsDefaultValue(value interface{}) bool
- func IsExportedName(name string) bool
- func NewInstance(prototype interface{}) 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 StructVisitor
Examples ¶
- VisitStruct (DeepAnonymousStructFields)
- VisitStruct (DeepStructFields)
- VisitStruct (FlatAnonymousStructFields)
- VisitStruct (FlatStructWithMap)
- VisitStruct (FlatStructWithSliceAndArray)
- VisitStruct (LimitDepth)
- VisitStruct (SimpleFlatPtrsStruct)
- VisitStruct (SimpleFlatStruct)
- VisitStruct (SliceOfStructInAnonymousStruct)
Constants ¶
This section is empty.
Variables ¶
var TypeOfError = reflect.TypeOf(func(error) {}).In(0)
TypeOfError is the built-in error type
Functions ¶
func CallMethod ¶
func CallMethod1 ¶
func CopyExportedStructFields ¶
func CopyExportedStructFields(src, dst interface{}) (copied int)
func DereferenceValue ¶
DereferenceValue recursively dereferences v if it is a pointer or interface. It will return ok == false if nil is encountered.
func ExportedStructFields ¶
func ExportedStructFields(s interface{}) map[string]interface{}
func FindFlattenedStructField ¶
func FindFlattenedStructField(t reflect.Type, matchFunc MatchStructFieldFunc) *reflect.StructField
func IsDeepNil ¶
func IsDeepNil(i interface{}) bool
Non nil interfaces can wrap nil values. Comparing the interface to nil, won't return if the wrapped value is nil.
func IsDefaultValue ¶
func IsDefaultValue(value interface{}) bool
func IsExportedName ¶
func NewInstance ¶
func NewInstance(prototype interface{}) interface{}
Creates a new zero valued instance of prototype
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 (FlatStructWithMap) ¶
type exampleStruct struct { X int IntMap map[string]int StringMap map[string]string } val := &exampleStruct{ X: 9, IntMap: map[string]int{"1": 1, "2": 2}, StringMap: map[string]string{"1": "Hello", "2": "World"}, } VisitStruct(val, NewStdLogStructVisitor())
Output: BeginStruct(reflection.exampleStruct) StructField(0: X int = 9) StructField(1: IntMap map[string]int = map[string]int{"1":1, "2":2}) BeginMap(map[string]int) MapField("1": int = 1) MapField("2": int = 2) EndMap(map[string]int) StructField(2: StringMap map[string]string = map[string]string{"1":"Hello", "2":"World"}) BeginMap(map[string]string) MapField("1": string = "Hello") MapField("2": string = "World") EndMap(map[string]string) 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 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 }