-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.go
271 lines (231 loc) · 5.73 KB
/
renderer.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package dapper
import (
"fmt"
"io"
"reflect"
"strings"
"github.com/dogmatiq/dapper/internal/stream"
"github.com/dogmatiq/dapper/internal/unsafereflect"
)
// Renderer is an interface for rendering human-readable representations of
// arbitrary values.
type Renderer interface {
io.Writer
Config() Config
FormatType(Value) string
WriteType(Value)
WriteValue(Value)
FormatValue(Value) string
Indent()
Outdent()
Print(format string, args ...any)
WithModifiedConfig(func(*Config)) Renderer
}
type renderer struct {
Indenter stream.Indenter
ProducedOutput bool
Configuration Config
RecursionSet map[uintptr]struct{}
FilterIndex int
FilterValue *Value
}
func (r *renderer) Write(data []byte) (int, error) {
n, err := r.Indenter.Write(data)
if n > 0 {
r.ProducedOutput = true
}
if err != nil {
panic(panicSentinel{err})
}
return n, nil
}
func (r *renderer) Config() Config {
return r.Configuration
}
func (r *renderer) Print(format string, args ...any) {
if _, err := fmt.Fprintf(r, format, args...); err != nil {
panic(panicSentinel{err})
}
}
func (r *renderer) FormatType(v Value) string {
var w strings.Builder
r.child(&w, r.Configuration).WriteType(v)
return w.String()
}
func (r *renderer) WriteType(v Value) {
t := v.DynamicType
switch t.Kind() {
case reflect.Chan:
renderChanType(r, r.Configuration, t)
case reflect.Func:
renderFuncType(r, r.Configuration, t)
case reflect.Map:
renderMapType(r, r.Configuration, t)
case reflect.Ptr:
renderPtrType(r, r.Configuration, t)
case reflect.Array:
renderArrayType(r, r.Configuration, t)
case reflect.Slice:
renderSliceType(r, r.Configuration, t)
default:
renderType(r, r.Configuration, t)
}
}
func renderType(r Renderer, c Config, t reflect.Type) {
pkg := t.PkgPath()
name := t.Name()
if c.OmitPackagePaths || name == "" || pkg == "" {
name = t.String()
} else {
name = pkg + "." + name
}
name = strings.ReplaceAll(name, "interface {}", "any")
name = strings.ReplaceAll(name, "interface {", "interface{")
name = strings.ReplaceAll(name, "struct {", "struct{")
if strings.ContainsAny(name, "() \t\n") {
name = "(" + name + ")"
}
r.Print("%s", name)
}
func (r *renderer) FormatValue(v Value) string {
var w strings.Builder
r.child(&w, r.Configuration).WriteValue(v)
return w.String()
}
func (r *renderer) WriteValue(v Value) {
if v.Value.Kind() == reflect.Invalid {
r.Print("any(nil)")
return
}
isFilterValue := r.FilterValue != nil && r.FilterValue.Value == v.Value
if !isFilterValue {
if recursive := r.enter(v); recursive {
if v.IsAmbiguousType() {
r.WriteType(v)
r.Print("(%s)", r.Configuration.RecursionMarker)
} else {
r.Print("%s", r.Configuration.RecursionMarker)
}
return
}
defer r.leave(v)
}
v.Value = unsafereflect.MakeMutable(v.Value)
for index, filter := range r.Configuration.Filters {
if r.FilterIndex == index && isFilterValue {
continue
}
child := r.child(r, r.Configuration)
child.FilterIndex = index
child.FilterValue = &v
filter(child, v)
if child.ProducedOutput {
return
}
}
switch v.DynamicType.Kind() {
case reflect.String:
renderStringKind(r, v)
case reflect.Bool:
renderBoolKind(r, v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
renderIntKind(r, v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
renderUintKind(r, v)
case reflect.Float32, reflect.Float64:
renderFloatKind(r, v)
case reflect.Complex64, reflect.Complex128:
renderComplexKind(r, v)
case reflect.Uintptr:
renderUintptrKind(r, v)
case reflect.UnsafePointer:
renderUnsafePointerKind(r, v)
case reflect.Chan:
renderChanKind(r, v)
case reflect.Func:
renderFuncKind(r, v)
case reflect.Interface:
renderInterfaceKind(r, v)
case reflect.Map:
renderMapKind(r, v)
case reflect.Ptr:
renderPtrKind(r, v)
case reflect.Array:
renderArrayOrSliceKind(r, v)
case reflect.Slice:
renderArrayOrSliceKind(r, v)
case reflect.Struct:
renderStructKind(r, v)
default:
panic("unsupported kind: " + v.DynamicType.Kind().String())
}
}
func (r *renderer) Indent() {
r.Indenter.Depth++
}
func (r *renderer) Outdent() {
r.Indenter.Depth--
}
func (r *renderer) WithModifiedConfig(modify func(*Config)) Renderer {
c := r.Configuration
modify(&c)
return r.child(r, c)
}
func (r *renderer) child(w io.Writer, c Config) *renderer {
return &renderer{
Indenter: stream.Indenter{
Target: w,
Indent: []byte(c.Indent),
},
Configuration: c,
RecursionSet: r.RecursionSet,
FilterIndex: r.FilterIndex,
FilterValue: r.FilterValue,
}
}
// enter indicates that a potentially value is about to be formatted.
// It returns true if recursion has occurred, indicating that the value should.
func (r *renderer) enter(v Value) bool {
if possiblyRecursive(v) {
ptr := v.Value.Pointer()
if _, ok := r.RecursionSet[ptr]; ok {
return true
}
r.RecursionSet[ptr] = struct{}{}
}
return false
}
// leave indicates that a potentially recursive value has finished rendering.
//
// It must be called after enter(v) returns true.
func (r *renderer) leave(v Value) {
if possiblyRecursive(v) {
delete(r.RecursionSet, v.Value.Pointer())
}
}
// possiblyRecursive returns true if v may be a recursive data structure.
func possiblyRecursive(v Value) bool {
switch v.DynamicType.Kind() {
case reflect.Map,
reflect.Ptr,
reflect.Slice:
return true
default:
return false
}
}
// printWithTypeIfAmbiguous prints a format string and arguments. If v's type is
// ambiguous the formatted string is prefixed with the type name.
func printWithTypeIfAmbiguous(
r Renderer,
v Value,
format string,
args ...any,
) {
if v.IsAmbiguousType() {
r.WriteType(v)
r.Print("("+format+")", args...)
} else {
r.Print(format, args...)
}
}