-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
327 lines (283 loc) · 7.51 KB
/
vars.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package kama
import (
"bytes"
"fmt"
"reflect"
"runtime"
"strings"
"unicode"
)
// vari represents a variable
type vari struct {
Name string
Kind reflect.Kind
Signature []string
Fields []string
Methods []string
Val string
cfg Config
}
func newVari(i interface{}, cfg Config) vari {
{ // config validation.
if cfg.MaxLength < 1 {
cfg.MaxLength = 1
}
if cfg.MaxLength > 10_000 {
// the upper limit of a slice is some significant fraction of the address space of a process.
// https://github.com/golang/go/issues/38673#issuecomment-643885108
cfg.MaxLength = 10_000
}
if cfg.MaxIndentLevel < 1 {
cfg.MaxIndentLevel = 10 // ie, the default
}
if cfg.MaxIndentLevel > 100 {
cfg.MaxIndentLevel = 100
}
}
iType := reflect.TypeOf(i)
valueOfi := reflect.ValueOf(i)
hideZeroValues := false
indentLevel := 0
if iType == nil {
// TODO: maybe there is a way in reflect to diffrentiate the various types of nil
v := vari{
Name: "nil",
Kind: reflect.Ptr,
Signature: []string{"nil"},
cfg: cfg,
}
v.Val = v.dump(valueOfi, hideZeroValues, indentLevel)
return v
}
if iType.Kind() == reflect.Pointer && valueOfi.IsNil() && valueOfi.IsZero() {
v := vari{
Name: "unknown",
Kind: reflect.Ptr,
Signature: []string{fmt.Sprintf("%v", iType)},
cfg: cfg,
}
v.Val = v.dump(valueOfi, hideZeroValues, indentLevel)
return v
}
typeKind := getKind(i)
typeName := getName(i)
typeSig := getSignature(i)
fields := getAllFields(i)
methods := trimMethods(getAllMethods(i))
v := vari{
Name: typeName,
Kind: typeKind,
Signature: typeSig,
Fields: fields,
Methods: methods,
cfg: cfg,
}
v.Val = v.dump(valueOfi, hideZeroValues, indentLevel)
return v
}
func (v vari) String() string {
nLf := func(x []string) []string {
fm := []string{}
if len(x) <= 0 {
return fm
}
for _, c := range x {
fm = append(fm, "\n\t"+c)
}
fm = append(fm, "\n\t")
return fm
}
w := &bytes.Buffer{}
stackp(w, v.cfg.ShowColor)
return fmt.Sprintf(
`
[
NAME: %v
KIND: %v
SIGNATURE: %v
FIELDS: %v
METHODS: %v
STACK_TRACE: [
%v
]
SNIPPET: %s
]
`,
v.Name,
v.Kind,
v.Signature,
nLf(v.Fields),
nLf(v.Methods),
strings.TrimRightFunc(w.String(), unicode.IsSpace),
v.Val,
)
}
func getName(i interface{}) string {
iType := reflect.TypeOf(i)
typeKind := iType.Kind()
typeName := ""
switch typeKind { //nolint:exhaustive
case reflect.Func:
typeName = runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
if typeName == "" {
typeName = "." + iType.Elem().Name()
}
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
typeName = iType.String()
case reflect.Ptr:
valueI := reflect.ValueOf(i).Elem()
valueType := valueI.Type()
typeName = valueType.PkgPath() + "." + valueType.Name()
default:
typeName = typeKind.String()
}
if iType.PkgPath() != "" {
typeName = iType.PkgPath() + "." + iType.Name()
}
return typeName
}
func getKind(i interface{}) reflect.Kind {
iType := reflect.TypeOf(i)
typeKind := iType.Kind()
if typeKind != reflect.Ptr {
return typeKind
} else {
// the passed in type maybe be a `*T` so lets find the kind of `T`
valueI := reflect.ValueOf(i).Elem()
valueType := valueI.Type()
valueTypeKind := valueType.Kind()
return valueTypeKind
}
}
func getSignature(i interface{}) []string {
iType := reflect.TypeOf(i)
typeKind := iType.Kind()
allSignatures := []string{iType.String()}
if typeKind == reflect.Ptr {
// the passed in type maybe be a `*T` so lets find the signature of `T`
valueI := reflect.ValueOf(i).Elem()
vType := valueI.Type()
sig := vType.String()
allSignatures = append(allSignatures, sig)
} else if typeKind == reflect.Struct {
// the passed in type is a `T` so lets also find the signature of `*T`
// NB: We are currently only allowing structs here. But we could expand to other types
ptrOfT := reflect.PtrTo(iType)
sig := ptrOfT.String()
allSignatures = append(allSignatures, sig)
}
return allSignatures
}
// getFields finds all the fields(if any) of type `T` and `*T`
func getAllFields(i interface{}) []string {
iType := reflect.TypeOf(i)
typeKind := iType.Kind()
allFields := []string{}
if typeKind == reflect.Ptr {
// the passed in type maybe be a `*T struct{}` so lets also find methods of `T struct{}`
valueI := reflect.ValueOf(i).Elem()
allFields = getFields(valueI.Type())
} else if typeKind == reflect.Struct {
allFields = getFields(iType)
}
return allFields
}
// getFields finds all the fields(if any) of a type
func getFields(iType reflect.Type) []string {
fields := []string{}
typeKind := iType.Kind()
if typeKind == reflect.Struct {
// TODO: If a structField happens to be a func,
// We should enhance that signature.
// Look at `dumpFunc()` for implementation
// https://github.com/komuw/kama/issues/38
numFields := iType.NumField()
for i := 0; i < numFields; i++ {
f := iType.Field(i)
if f.PkgPath != "" {
// private field
continue
}
name := f.Name + " " + f.Type.String()
fields = append(fields, name)
}
}
return fields
}
// getAllMethods finds all the methods of type `T` and `*T`
func getAllMethods(i interface{}) []string {
iType := reflect.TypeOf(i)
allMethods := []string{}
methodsOfT := []string{}
methodsOfPointerT := []string{}
methodsOfPassedInType := getMethods(iType)
if iType.Kind() == reflect.Ptr {
// the passed in type is a `*T` so lets also find methods of `T`
valueI := reflect.ValueOf(i).Elem()
methodsOfT = getMethods(valueI.Type())
} else {
// the passed in type is a `T` so lets also find methods of `*T`
ptrOfT := reflect.PtrTo(iType)
methodsOfPointerT = getMethods(ptrOfT)
}
allMethods = append(allMethods, methodsOfT...)
allMethods = append(allMethods, methodsOfPointerT...)
allMethods = append(allMethods, methodsOfPassedInType...)
return allMethods
}
// getMethods finds all the methods of type.
func getMethods(iType reflect.Type) []string {
methods := []string{}
numMethods := iType.NumMethod()
for i := 0; i < numMethods; i++ {
meth := iType.Method(i)
if meth.PkgPath != "" {
// private method
continue
}
methName := meth.Name
methSig := meth.Type.String() // type signature
// TODO: maybe we should try and also add argument names if any.
// currently the signature is displayed as;
// func(main.Foo, int, int) int
// it would be cooler to display as;
// func(main.Foo, price int, commission int) int
// https://github.com/komuw/kama/issues/39
methods = append(methods, methName+" "+methSig)
}
return methods
}
// trimMethods removes any duplicated methods.
// if a method is applicable for both type `T` and `*T`, then `trimMethods` will
// just remove the one for `*T`
func trimMethods(methods []string) []string {
// contains tells whether a contains x.
contains := func(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
trimmedMethods := []string{}
var TmethNames []string
// first add all methods for type `T`
for _, meth := range methods {
if !strings.Contains(meth, "*") {
trimmedMethods = append(trimmedMethods, meth)
methName := strings.Split(meth, " ")[0]
TmethNames = append(TmethNames, methName)
}
}
// then add methods for `*T` but only if they do not exist for `T`
for _, meth := range methods {
if strings.Contains(meth, "*") {
methName := strings.Split(meth, " ")[0]
if !contains(TmethNames, methName) {
trimmedMethods = append(trimmedMethods, meth)
}
}
}
return trimmedMethods
}