-
Notifications
You must be signed in to change notification settings - Fork 18
/
explorable.go
402 lines (368 loc) · 9.11 KB
/
explorable.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package explorable
import (
"fmt"
"github.com/signalfx/golib/log"
"github.com/signalfx/golib/logkey"
"html"
"net/http"
"reflect"
"runtime"
"strconv"
"strings"
)
// DefaultLogger is used by explorable if a handler hasn't set a logger
var DefaultLogger = log.Logger(log.DefaultLogger.CreateChild())
// Result is the crude explorable representation of an object returned by ExploreObject
type Result struct {
Result interface{}
Children []string
Desc string
}
// Handler allows you to serve an exportable object for debugging over HTTP
type Handler struct {
Val interface{}
BasePath string
Logger log.Logger
}
func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
logger := h.Logger
if logger == nil {
logger = DefaultLogger
}
pathParts := strings.Split(strings.TrimPrefix(r.URL.Path, h.BasePath), "/")
nonEmptyParts := []string{}
for _, p := range pathParts {
if p != "" {
nonEmptyParts = append(nonEmptyParts, p)
}
}
logger.Log(logkey.ExplorableParts, fmt.Sprintf("%v", nonEmptyParts), logkey.URL, r.URL, "Exploring object")
o := ExploreObject(reflect.ValueOf(h.Val), nonEmptyParts)
parent := ""
if len(nonEmptyParts) > 0 {
parent = fmt.Sprintf(`
<h1>
<a href="%s">Parent</a>
</h1>
`, h.BasePath+strings.TrimPrefix("/"+strings.Join(nonEmptyParts[0:len(nonEmptyParts)-1], "/"), "/"))
}
childTable := ""
if len(o.Children) > 0 {
childTable += "<table>\n"
for _, c := range o.Children {
link := h.BasePath + strings.TrimPrefix("/"+strings.Join(append(nonEmptyParts, c), "/"), "/")
childTable += fmt.Sprintf(`
<tr>
<td><a href="%s">%s</a></td>
</tr>
`, link, html.EscapeString(c))
}
childTable += "</table>\n"
}
s :=
fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<title>Explorable object</title>
</head>
<body>
<h1>
%s
</h1>
%s
%s
</body>
</html>`, html.EscapeString(o.Desc), parent, childTable)
_, err := rw.Write([]byte(s))
log.IfErr(h.Logger, err)
}
func checkConsts(t reflect.Value) *Result {
ret := &Result{}
if !t.IsValid() {
ret.Desc = "<INVALID>"
return ret
}
kind := t.Kind()
if kind >= reflect.Int && kind <= reflect.Int64 {
ret.Desc = strconv.FormatInt(t.Int(), 10)
return ret
}
if kind >= reflect.Uint && kind <= reflect.Uint64 {
ret.Desc = strconv.FormatUint(t.Uint(), 10)
return ret
}
if kind >= reflect.Float32 && kind <= reflect.Float64 {
ret.Desc = strconv.FormatFloat(t.Float(), byte('f'), 10, 64)
return ret
}
return nil
}
func exploreArray(t reflect.Value, path []string) *Result {
ret := &Result{}
if len(path) == 0 {
ret.Desc = fmt.Sprintf("array-len(%d of %d)", t.Len(), t.Cap())
ret.Children = make([]string, t.Len())
for i := 0; i < t.Len(); i++ {
ret.Children[i] = strconv.FormatInt(int64(i), 10)
}
return ret
}
index, err := strconv.ParseInt(path[0], 10, 64)
if err != nil {
ret.Desc = err.Error()
return ret
}
// TODO: Catch panics here
return ExploreObject(t.Index(int(index)), path[1:])
}
func exploreFunc(t reflect.Value, path []string) *Result {
ret := &Result{}
if t.IsNil() {
ret.Desc = "<NIL function>"
return ret
}
f := runtime.FuncForPC(t.Pointer())
if f == nil {
ret.Desc = "<UNKNOWN FUNCTION>"
return ret
}
ret.Desc = f.Name()
return ret
}
func exploreSlice(t reflect.Value, path []string) *Result {
ret := &Result{}
if t.IsNil() {
ret.Desc = "<NIL>"
return ret
}
if len(path) == 0 {
ret.Desc = fmt.Sprintf("slice-len(%d of %d)", t.Len(), t.Cap())
ret.Children = make([]string, t.Len())
for i := 0; i < t.Len(); i++ {
ret.Children[i] = strconv.FormatInt(int64(i), 10)
}
return ret
}
index, err := strconv.ParseInt(path[0], 10, 64)
if err != nil {
ret.Desc = err.Error()
return ret
}
// TODO: Catch panics here
return ExploreObject(t.Index(int(index)), path[1:])
}
func exploreMap(t reflect.Value, path []string) *Result {
ret := &Result{}
if t.IsNil() {
ret.Desc = "<NIL>"
return ret
}
if len(path) == 0 {
ret.Desc = fmt.Sprintf("map-len(%d)", t.Len())
keys := t.MapKeys()
ret.Children = make([]string, len(keys))
for i, k := range keys {
// TODO: Better index?
ret.Children[i] = keyMapString(k)
}
return ret
}
mkey := keyMapType(t.Type().Key().Kind(), path[0])
if !mkey.IsValid() {
ret.Desc = fmt.Sprintf("<INVALID MAP KEY %s>", path[0])
return ret
}
v := t.MapIndex(mkey)
if !v.IsValid() {
ret.Desc = fmt.Sprintf("<NOT FOUND MAP KEY %s>", path[0])
return ret
}
return ExploreObject(v, path[1:])
}
func exploreStruct(t reflect.Value, path []string) *Result {
ret := &Result{}
if len(path) == 0 {
ret.Desc = t.Type().Name()
ret.Children = make([]string, t.Type().NumField())
for i := 0; i < t.Type().NumField(); i++ {
ret.Children[i] = t.Type().Field(i).Name
}
return ret
}
val := t.FieldByName(path[0])
if !val.IsValid() {
ret.Desc = fmt.Sprintf("<Invalid path %s>", path[0])
return ret
}
return ExploreObject(val, path[1:])
}
func exploreChan(t reflect.Value, path []string) *Result {
ret := &Result{}
if t.IsNil() {
ret.Desc = "<NIL>"
return ret
}
ret.Desc = fmt.Sprintf("chan-len(%d of %d)", t.Len(), t.Cap())
return ret
}
func explorePtr(t reflect.Value, path []string) *Result {
ret := &Result{}
if t.IsNil() {
ret.Desc = "<NIL>"
return ret
}
return ExploreObject(t.Elem(), path)
}
func exploreInterface(t reflect.Value, path []string) *Result {
ret := &Result{}
if t.IsNil() {
ret.Desc = "<NIL>"
return ret
}
return ExploreObject(t.Elem(), path)
}
// ExploreObject is a crude public way to explore an object's values via reflection
func ExploreObject(t reflect.Value, path []string) *Result {
if ret := checkConsts(t); ret != nil {
return ret
}
ret := &Result{}
switch t.Kind() {
case reflect.Bool:
ret.Desc = fmt.Sprintf("%t", t.Bool())
return ret
case reflect.String:
ret.Desc = t.String()
return ret
}
c := map[reflect.Kind](func(reflect.Value, []string) *Result){
reflect.Array: exploreArray,
reflect.Func: exploreFunc,
reflect.Slice: exploreSlice,
reflect.Map: exploreMap,
reflect.Struct: exploreStruct,
reflect.Chan: exploreChan,
reflect.Ptr: explorePtr,
reflect.Interface: exploreInterface,
}
callback, exists := c[t.Kind()]
if exists {
return callback(t, path)
}
ret.Desc = "<Unsupported>"
return ret
}
func stringToIntType(path string) reflect.Value {
i, err := strconv.ParseInt(path, 10, 64)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(int(i))
}
func stringToInt8Type(path string) reflect.Value {
i, err := strconv.ParseInt(path, 10, 8)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(int8(i))
}
func stringToInt16Type(path string) reflect.Value {
i, err := strconv.ParseInt(path, 10, 16)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(int16(i))
}
func stringToInt32Type(path string) reflect.Value {
i, err := strconv.ParseInt(path, 10, 32)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(int32(i))
}
func stringToInt64Type(path string) reflect.Value {
i, err := strconv.ParseInt(path, 10, 64)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(i)
}
func stringToUIntType(path string) reflect.Value {
i, err := strconv.ParseUint(path, 10, 64)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(uint(i))
}
func stringToUInt8Type(path string) reflect.Value {
i, err := strconv.ParseUint(path, 10, 8)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(uint8(i))
}
func stringToUInt16Type(path string) reflect.Value {
i, err := strconv.ParseUint(path, 10, 16)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(uint16(i))
}
func stringToUInt32Type(path string) reflect.Value {
i, err := strconv.ParseUint(path, 10, 32)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(uint32(i))
}
func stringToUInt64Type(path string) reflect.Value {
i, err := strconv.ParseUint(path, 10, 64)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(i)
}
func stringToFloat32Type(path string) reflect.Value {
i, err := strconv.ParseFloat(path, 32)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(float32(i))
}
func stringToFloat64Type(path string) reflect.Value {
i, err := strconv.ParseFloat(path, 64)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(i)
}
func stringToStringType(path string) reflect.Value {
return reflect.ValueOf(path)
}
func keyMapType(mapKeyKind reflect.Kind, path string) reflect.Value {
m := map[reflect.Kind](func(string) reflect.Value){
reflect.Int: stringToIntType,
reflect.Int8: stringToInt8Type,
reflect.Int16: stringToInt16Type,
reflect.Int32: stringToInt32Type,
reflect.Int64: stringToInt64Type,
reflect.Uint: stringToUIntType,
reflect.Uint8: stringToUInt8Type,
reflect.Uint16: stringToUInt16Type,
reflect.Uint32: stringToUInt32Type,
reflect.Uint64: stringToUInt64Type,
reflect.Float32: stringToFloat32Type,
reflect.Float64: stringToFloat64Type,
reflect.String: stringToStringType,
}
f, e := m[mapKeyKind]
if e {
return f(path)
}
return reflect.Value{}
}
func keyMapString(t reflect.Value) string {
o := ExploreObject(t, []string{})
return o.Desc
}