-
Notifications
You must be signed in to change notification settings - Fork 1
/
append_slice.go
58 lines (43 loc) · 1.39 KB
/
append_slice.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
package golog
import (
"encoding/json"
"fmt"
"reflect"
)
func (p *DevHandler) appendSlice(buf []byte, val reflect.Value, fgColor []byte, indent int) []byte {
// If val implements fmt.Stringer interface, than call it.
// This is useful for types like UUID
stringer := reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
if val.Type().Implements(stringer) == true {
valMethod := val.MethodByName("String")
// Double check if the method value returned is valid
if valMethod.Kind() != reflect.Invalid {
retVal := valMethod.Call([]reflect.Value{})
buf = append(buf, retVal[0].String()...)
return buf
}
}
// []byte is an alias for []uint8 which is a slice, test if it's truly []byte or not
// this is done to check if []byte is actually a JSON string
if val.CanConvert(reflect.TypeOf([]byte(nil))) {
b := val.Bytes()
validJSON := json.Valid(b)
// Not a valid JSON, output it as-is and return early
if validJSON == false {
buf = append(buf, b...)
return buf
}
// Valid JSON, prettify it
buf = p.appendJSON(buf, b, indent)
return buf
}
// Handle it like a regular slice
buf = fmt.Appendf(buf, "%s", val.Type().String())
for i := 0; i < val.Len(); i++ {
buf = append(buf, '\n')
buf = fmt.Appendf(buf, "%*s", indent+2, "")
buf = fmt.Appendf(buf, "%s|- %d%s : ", fgColor, i, colorReset)
buf = p.appendType(buf, val.Index(i), fgColor, indent+2)
}
return buf
}