-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwrap_interface.go
More file actions
36 lines (31 loc) · 938 Bytes
/
unwrap_interface.go
File metadata and controls
36 lines (31 loc) · 938 Bytes
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
package pretty
import (
"reflect"
)
// UnwrapInterfaceWriter is a [ValueWriter] that unwraps interface values.
//
// It should be created with [NewUnwrapInterfaceWriter].
type UnwrapInterfaceWriter struct {
ValueWriter
}
// NewUnwrapInterfaceWriter creates a new [UnwrapInterfaceWriter].
func NewUnwrapInterfaceWriter(vw ValueWriter) *UnwrapInterfaceWriter {
return &UnwrapInterfaceWriter{
ValueWriter: vw,
}
}
// WriteValue implements [ValueWriter].
func (vw *UnwrapInterfaceWriter) WriteValue(st *State, v reflect.Value) bool {
v, isNil := vw.unwrapInterface(st, v)
return isNil || vw.ValueWriter.WriteValue(st, v)
}
func (vw *UnwrapInterfaceWriter) unwrapInterface(st *State, v reflect.Value) (_ reflect.Value, isNil bool) {
if v.Kind() == reflect.Interface {
if checkNil(st, v) {
return reflect.Value{}, true
}
v = v.Elem()
st.KnownType = false // We want to show the type of the value.
}
return v, false
}