-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.go
More file actions
51 lines (45 loc) · 1.08 KB
/
pointer.go
File metadata and controls
51 lines (45 loc) · 1.08 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
package pretty
import (
"reflect"
"github.com/pierrre/pretty/internal/must"
)
// PointerWriter is a [ValueWriter] that handles pointer values.
//
// It should be created with [NewPointerWriter].
type PointerWriter struct {
ValueWriter
// ShowAddr shows the address.
// Default: true.
ShowAddr bool
}
// NewPointerWriter creates a new [PointerWriter] with default values.
func NewPointerWriter(vw ValueWriter) *PointerWriter {
return &PointerWriter{
ValueWriter: vw,
ShowAddr: true,
}
}
// WriteValue implements [ValueWriter].
func (vw *PointerWriter) WriteValue(st *State, v reflect.Value) bool {
if v.Kind() != reflect.Pointer {
return false
}
if checkNil(st.Writer, v) {
return true
}
infos{
showAddr: vw.ShowAddr,
addr: uintptr(v.UnsafePointer()),
}.writeWithTrailingSpace(st)
writeArrow(st.Writer)
must.Handle(vw.ValueWriter.WriteValue(st, v.Elem()))
return true
}
// Supports implements [SupportChecker].
func (vw *PointerWriter) Supports(typ reflect.Type) ValueWriter {
var res ValueWriter
if typ.Kind() == reflect.Pointer {
res = vw
}
return res
}