-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpretty.go
More file actions
186 lines (159 loc) · 3.79 KB
/
pretty.go
File metadata and controls
186 lines (159 loc) · 3.79 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Pretty-print Go data structures
*/
package pretty
import (
"bytes"
"fmt"
"io"
"os"
r "reflect"
"strconv"
"strings"
)
const (
DEFAULT_INDENT = " "
DEFAULT_NIL = "nil"
)
// The context for printing
type Pretty struct {
// indent string
Indent string
// output recipient
Out io.Writer
// string for nil
NilString string
// compact empty array and struct
Compact bool
// Maximum nesting level
MaxLevel int
}
// pretty print the input value (to stdout)
func PrettyPrint(i interface{}) {
PrettyPrintTo(os.Stdout, i, true)
}
// pretty print the input value (to a string)
func PrettyFormat(i interface{}) string {
var out bytes.Buffer
PrettyPrintTo(&out, i, false)
return out.String()
}
// pretty print the input value (to specified writer)
func PrettyPrintTo(out io.Writer, i interface{}, nl bool) {
p := &Pretty{Indent: DEFAULT_INDENT, Out: out, NilString: DEFAULT_NIL}
if nl {
p.Println(i)
} else {
p.Print(i)
}
}
// pretty print the input value (no newline)
func (p *Pretty) Print(i interface{}) {
p.PrintValue(r.ValueOf(i), 0)
}
// pretty print the input value (newline)
func (p *Pretty) Println(i interface{}) {
p.PrintValue(r.ValueOf(i), 0)
io.WriteString(p.Out, "\n")
}
// recursively print the input value
func (p *Pretty) PrintValue(val r.Value, level int) {
if !val.IsValid() {
io.WriteString(p.Out, p.NilString)
return
}
cur := strings.Repeat(p.Indent, level)
next := strings.Repeat(p.Indent, level+1)
nl := "\n"
if len(p.Indent) == 0 {
nl = " "
}
if p.MaxLevel > 0 && level >= p.MaxLevel {
io.WriteString(p.Out, val.String())
return
}
switch val.Kind() {
case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
io.WriteString(p.Out, strconv.FormatInt(val.Int(), 10))
case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64:
io.WriteString(p.Out, strconv.FormatUint(val.Uint(), 10))
case r.Float32, r.Float64:
io.WriteString(p.Out, strconv.FormatFloat(val.Float(), 'f', -1, 64))
case r.String:
io.WriteString(p.Out, strconv.Quote(val.String()))
case r.Bool:
io.WriteString(p.Out, strconv.FormatBool(val.Bool()))
case r.Map:
l := val.Len()
io.WriteString(p.Out, "{"+nl)
for i, k := range val.MapKeys() {
io.WriteString(p.Out, next)
io.WriteString(p.Out, strconv.Quote(k.String()))
io.WriteString(p.Out, ": ")
p.PrintValue(val.MapIndex(k), level+1)
if i < l-1 {
io.WriteString(p.Out, ","+nl)
} else {
io.WriteString(p.Out, nl)
}
}
io.WriteString(p.Out, cur)
io.WriteString(p.Out, "}")
case r.Array, r.Slice:
l := val.Len()
if p.Compact && l == 0 {
io.WriteString(p.Out, "[]")
} else {
io.WriteString(p.Out, "["+nl)
for i := 0; i < l; i++ {
io.WriteString(p.Out, next)
p.PrintValue(val.Index(i), level+1)
if i < l-1 {
io.WriteString(p.Out, ","+nl)
} else {
io.WriteString(p.Out, nl)
}
}
io.WriteString(p.Out, cur)
io.WriteString(p.Out, "]")
}
case r.Interface, r.Ptr:
p.PrintValue(val.Elem(), level)
case r.Struct:
if val.CanInterface() {
i := val.Interface()
if i, ok := i.(fmt.Stringer); ok {
io.WriteString(p.Out, i.String())
} else {
l := val.NumField()
sOpen := "struct {"
if p.Compact {
sOpen = "{"
}
if p.Compact && l == 0 {
io.WriteString(p.Out, "{}")
} else {
io.WriteString(p.Out, sOpen+nl)
for i := 0; i < l; i++ {
io.WriteString(p.Out, next)
io.WriteString(p.Out, val.Type().Field(i).Name)
io.WriteString(p.Out, ": ")
p.PrintValue(val.Field(i), level+1)
if i < l-1 {
io.WriteString(p.Out, ","+nl)
} else {
io.WriteString(p.Out, nl)
}
}
io.WriteString(p.Out, cur)
io.WriteString(p.Out, "}")
}
}
} else {
io.WriteString(p.Out, "protected")
}
default:
io.WriteString(p.Out, "unsupported:")
io.WriteString(p.Out, val.String())
}
}