/
printer.go
186 lines (153 loc) · 4.17 KB
/
printer.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
package dapper
import (
"io"
"os"
"reflect"
"strings"
"sync"
"github.com/dogmatiq/dapper/internal/stream"
)
// DefaultIndent is the default indent string used to indent nested values.
const DefaultIndent = " "
const (
// DefaultZeroValueMarker is the default string to display when rendering a
// zero-value struct.
DefaultZeroValueMarker = "<zero>"
// DefaultRecursionMarker is the default string to display when recursion
// is detected within a Go value.
DefaultRecursionMarker = "<recursion>"
)
// Config holds the configuration for a printer.
type Config struct {
// Filters is the set of filters to apply when formatting values.
Filters []Filter
// Indent is the string used to indent nested values.
// If it is empty, DefaultIndent is used.
Indent string
// ZeroValueMarker is a string that is displayed instead of a structs field
// list when it is the zero-value. If it is empty, DefaultZeroValueMarker is
// used.
ZeroValueMarker string
// RecursionMarker is a string that is displayed instead of a value's
// representation when recursion has been detected.
// If it is empty, DefaultRecursionMarker is used.
RecursionMarker string
// OmitPackagePaths, when true, causes the printer to omit the
// fully-qualified package path from the rendered type names.
OmitPackagePaths bool
// OmitUnexportedFields omits unexported struct fields when set to true
OmitUnexportedFields bool
}
// Printer generates human-readable representations of Go values.
//
// The output format is intended to be as minimal as possible, without being
// ambiguous. To that end, type information is only included where it can not be
// reliably inferred from the structure of the value.
type Printer struct {
// Config is the configuration for the printer.
Config Config
}
// panicSentinel is a panic value that wraps an error that must be returned from
// [Printer.Write].
type panicSentinel struct {
Err error
}
// Write writes a pretty-printed representation of v to w.
//
// It returns the number of bytes written.
func (p *Printer) Write(w io.Writer, v any) (_ int, err error) {
defer func() {
switch r := recover().(type) {
case panicSentinel:
err = r.Err
default:
panic(r)
case nil:
// no error
}
}()
cfg := p.Config
if len(cfg.Indent) == 0 {
cfg.Indent = DefaultIndent
}
if cfg.ZeroValueMarker == "" {
cfg.ZeroValueMarker = DefaultZeroValueMarker
}
if cfg.RecursionMarker == "" {
cfg.RecursionMarker = DefaultRecursionMarker
}
counter := &stream.Counter{
Target: w,
}
r := &renderer{
Indenter: stream.Indenter{
Target: counter,
Indent: []byte(cfg.Indent),
},
Configuration: cfg,
RecursionSet: map[uintptr]struct{}{},
}
rv := reflect.ValueOf(v)
var rt reflect.Type
if rv.Kind() != reflect.Invalid {
rt = rv.Type()
}
r.WriteValue(
Value{
Value: rv,
DynamicType: rt,
StaticType: typeOf[any](),
IsAmbiguousDynamicType: true,
IsAmbiguousStaticType: true,
IsUnexported: false,
},
)
return counter.Count(), nil
}
// Format returns a pretty-printed representation of v.
func (p *Printer) Format(v any) string {
var b strings.Builder
if _, err := p.Write(&b, v); err != nil {
// CODE COVERAGE: At the time of writing, strings.Builder.Write() never
// returns an error.
panic(err)
}
return b.String()
}
// DefaultPrinter is the printer used by Write(), Format() and Print().
var DefaultPrinter = Printer{
Config: Config{
Filters: []Filter{
StringerFilter, // always first
ErrorFilter,
ProtoFilter,
ReflectFilter,
SyncFilter,
TimeFilter,
},
},
}
// Write writes a pretty-printed representation of v to w using the default
// printer settings.
//
// It returns the number of bytes written.
func Write(w io.Writer, v any) (int, error) {
return DefaultPrinter.Write(w, v)
}
// Format returns a pretty-printed representation of v.
func Format(v any) string {
return DefaultPrinter.Format(v)
}
var (
mux sync.Mutex
newLine = []byte("\n")
)
// Print writes a pretty-printed representation of v to os.Stdout.
func Print(values ...any) {
mux.Lock()
defer mux.Unlock()
for _, v := range values {
Write(os.Stdout, v)
os.Stdout.Write(newLine)
}
}