-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
42 lines (37 loc) · 894 Bytes
/
func.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
package pretty
import (
"reflect"
"runtime"
"github.com/pierrre/pretty/internal/write"
)
// FuncValueWriter is a [ValueWriter] that handles function values.
//
// It should be created with [NewFuncValueWriter].
type FuncValueWriter struct {
// ShowAddr shows the address.
// Default: false.
ShowAddr bool
}
// NewFuncValueWriter creates a new [FuncValueWriter] with default values.
func NewFuncValueWriter() *FuncValueWriter {
return &FuncValueWriter{
ShowAddr: false,
}
}
// WriteValue implements [ValueWriter].
func (vw *FuncValueWriter) WriteValue(st *State, v reflect.Value) bool {
if v.Kind() != reflect.Func {
return false
}
if checkNil(st.Writer, v) {
return true
}
p := uintptr(v.UnsafePointer())
infos{
showAddr: vw.ShowAddr,
addr: p,
}.writeWithTrailingSpace(st)
name := runtime.FuncForPC(p).Name()
write.MustString(st.Writer, name)
return true
}