-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect_value.go
40 lines (35 loc) · 948 Bytes
/
reflect_value.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
package pretty
import (
"reflect"
)
var reflectValueType = reflect.TypeFor[reflect.Value]()
// ReflectValueWriter is a [ValueWriter] that handles [reflect.Value].
//
// It should be created with [NewReflectValueWriter].
type ReflectValueWriter struct {
ValueWriter
}
// NewReflectValueWriter creates a new [ReflectValueWriter].
func NewReflectValueWriter(vw ValueWriter) *ReflectValueWriter {
return &ReflectValueWriter{
ValueWriter: vw,
}
}
// WriteValue implements [ValueWriter].
func (vw *ReflectValueWriter) WriteValue(st *State, v reflect.Value) bool {
if v.Type() != reflectValueType {
return false
}
if !v.CanInterface() {
writeString(st.Writer, "<unexported>")
return true
}
rv := v.Interface().(reflect.Value) //nolint:forcetypeassert // Checked above.
writeArrow(st.Writer)
if checkInvalid(st.Writer, rv) {
return true
}
defer st.setRestoreKnownType(false)()
mustHandle(vw.ValueWriter(st, rv))
return true
}