forked from ungerik/go-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstructvisitor.go
110 lines (95 loc) · 3.4 KB
/
logstructvisitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package reflection
import (
"log"
"os"
"reflect"
"strings"
// "github.com/ungerik/go-start/debug"
)
func NewStdLogStructVisitor() *LogStructVisitor {
return &LogStructVisitor{Logger: log.New(os.Stdout, "", 0)}
}
// LogStructVisitor can be used for testing and debugging VisitStruct()
type LogStructVisitor struct {
Logger *log.Logger
}
func (self *LogStructVisitor) BeginStruct(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sBeginStruct(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error {
indent := strings.Repeat(" ", depth)
switch v.Kind() {
case reflect.Struct, reflect.Slice, reflect.Array:
self.Logger.Printf("%sStructField(%d: %s %s)", indent, index, f.Name, v.Type())
default:
self.Logger.Printf("%sStructField(%d: %s %s = %#v)", indent, index, f.Name, v.Type(), v.Interface())
}
return nil
}
func (self *LogStructVisitor) EndStruct(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sEndStruct(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) BeginSlice(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sBeginSlice(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) SliceField(depth int, v reflect.Value, index int) error {
indent := strings.Repeat(" ", depth)
switch v.Kind() {
case reflect.Struct, reflect.Slice, reflect.Array:
self.Logger.Printf("%sSliceField(%d: %s)", indent, index, v.Type())
default:
self.Logger.Printf("%sSliceField(%d: %s = %#v)", indent, index, v.Type(), v.Interface())
}
return nil
}
func (self *LogStructVisitor) EndSlice(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sEndSlice(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) BeginArray(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sBeginArray(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) ArrayField(depth int, v reflect.Value, index int) error {
indent := strings.Repeat(" ", depth)
switch v.Kind() {
case reflect.Struct, reflect.Slice, reflect.Array:
self.Logger.Printf("%sArrayField(%d: %s)", indent, index, v.Type())
default:
self.Logger.Printf("%sArrayField(%d: %s = %#v)", indent, index, v.Type(), v.Interface())
}
return nil
}
func (self *LogStructVisitor) EndArray(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sEndArray(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) BeginMap(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sBeginMap(%T)", indent, v.Interface())
return nil
}
func (self *LogStructVisitor) MapField(depth int, v reflect.Value, key string, index int) error {
indent := strings.Repeat(" ", depth)
switch v.Kind() {
case reflect.Struct, reflect.Slice, reflect.Array:
self.Logger.Printf("%sMapField(\"%s\": %s)", indent, key, v.Type())
default:
self.Logger.Printf("%sMapField(\"%s\": %s = %#v)", indent, key, v.Type(), v.Interface())
}
return nil
}
func (self *LogStructVisitor) EndMap(depth int, v reflect.Value) error {
indent := strings.Repeat(" ", depth)
self.Logger.Printf("%sEndMap(%T)", indent, v.Interface())
return nil
}