-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
171 lines (159 loc) · 4.47 KB
/
type.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
package pretty
import (
"io"
"reflect"
"unsafe" //nolint:depguard // Required for unsafe.Pointer.
"github.com/pierrre/go-libs/syncutil"
)
// TypeValueWriter is a [ValueWriter] that writes the type.
//
// It should be created with [NewTypeValueWriter].
type TypeValueWriter struct {
// Stringer converts the [reflect.Type] to a string.
// Default: [reflect.Type.String].
Stringer func(reflect.Type) string
}
// NewTypeValueWriter creates a new [TypeValueWriter] with default values.
func NewTypeValueWriter() *TypeValueWriter {
return &TypeValueWriter{
Stringer: reflect.Type.String,
}
}
// WriteValue implements [ValueWriter].
func (vw *TypeValueWriter) WriteValue(st *State, v reflect.Value) bool {
writeString(st.Writer, vw.Stringer(v.Type()))
return true
}
// TypeAndValueWriter is a [ValueWriter] that writes the type and the value.
//
// It should be created with [NewTypeAndValueWriter].
type TypeAndValueWriter struct {
// Type is the type [ValueWriter].
Type ValueWriter
// Value is the value [ValueWriter].
Value ValueWriter
// ShowKnownTypes shows known types.
// Default: false.
ShowKnownTypes bool
// ShowBaseType shows the base type.
// Default: true.
ShowBaseType bool
}
// NewTypeAndValueWriter creates a new [TypeAndValueWriter] with default values.
func NewTypeAndValueWriter(t, v ValueWriter) *TypeAndValueWriter {
return &TypeAndValueWriter{
Type: t,
Value: v,
ShowKnownTypes: false,
ShowBaseType: true,
}
}
// WriteValue implements [ValueWriter].
func (vw *TypeAndValueWriter) WriteValue(st *State, v reflect.Value) bool {
if !st.KnownType || vw.ShowKnownTypes {
writeString(st.Writer, "[")
mustHandle(vw.Type(st, v))
writeString(st.Writer, "]")
vw.writeBaseType(st.Writer, v)
writeString(st.Writer, " ")
}
defer st.setRestoreKnownType(true)()
mustHandle(vw.Value(st, v))
return true
}
func (vw *TypeAndValueWriter) writeBaseType(w io.Writer, v reflect.Value) {
if !vw.ShowBaseType {
return
}
typ := v.Type()
if typ.PkgPath() == "" {
return
}
baseType := vw.getCachedBaseType(typ)
if baseType == nil {
return
}
writeString(w, "(")
writeString(w, baseType.String())
writeString(w, ")")
}
var baseTypeCache syncutil.Map[reflect.Type, reflect.Type]
func (vw *TypeAndValueWriter) getCachedBaseType(typ reflect.Type) reflect.Type {
baseType, ok := baseTypeCache.Load(typ)
if ok {
return baseType
}
baseType = vw.getBaseType(typ)
baseTypeCache.Store(typ, baseType)
return baseType
}
//nolint:gocyclo // We need to handle all kinds.
func (vw *TypeAndValueWriter) getBaseType(typ reflect.Type) reflect.Type {
var baseType reflect.Type
switch typ.Kind() { //nolint:exhaustive //Some kinds are not handled.
case reflect.Bool:
baseType = reflect.TypeFor[bool]()
case reflect.Int:
baseType = reflect.TypeFor[int]()
case reflect.Int8:
baseType = reflect.TypeFor[int8]()
case reflect.Int16:
baseType = reflect.TypeFor[int16]()
case reflect.Int32:
baseType = reflect.TypeFor[int32]()
case reflect.Int64:
baseType = reflect.TypeFor[int64]()
case reflect.Uint:
baseType = reflect.TypeFor[uint]()
case reflect.Uint8:
baseType = reflect.TypeFor[uint8]()
case reflect.Uint16:
baseType = reflect.TypeFor[uint16]()
case reflect.Uint32:
baseType = reflect.TypeFor[uint32]()
case reflect.Uint64:
baseType = reflect.TypeFor[uint64]()
case reflect.Uintptr:
baseType = reflect.TypeFor[uintptr]()
case reflect.Float32:
baseType = reflect.TypeFor[float32]()
case reflect.Float64:
baseType = reflect.TypeFor[float64]()
case reflect.Complex64:
baseType = reflect.TypeFor[complex64]()
case reflect.Complex128:
baseType = reflect.TypeFor[complex128]()
case reflect.Array:
baseType = reflect.ArrayOf(typ.Len(), typ.Elem())
case reflect.Chan:
baseType = reflect.ChanOf(typ.ChanDir(), typ.Elem())
case reflect.Func:
in := make([]reflect.Type, typ.NumIn())
for i := range in {
in[i] = typ.In(i)
}
out := make([]reflect.Type, typ.NumOut())
for i := range out {
out[i] = typ.Out(i)
}
baseType = reflect.FuncOf(in, out, typ.IsVariadic())
case reflect.Map:
baseType = reflect.MapOf(typ.Key(), typ.Elem())
case reflect.Pointer:
baseType = reflect.PointerTo(typ.Elem())
case reflect.Slice:
baseType = reflect.SliceOf(typ.Elem())
case reflect.String:
baseType = reflect.TypeFor[string]()
case reflect.UnsafePointer:
baseType = reflect.TypeFor[unsafe.Pointer]()
}
if baseType == nil {
// Unimplemented: invalid, interface, struct.
return nil
}
if baseType == typ {
return nil
}
return baseType
}