forked from foomo/logfrog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
67 lines (63 loc) · 1.67 KB
/
dump.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
package logfrog
import (
"fmt"
"strings"
"github.com/fatih/color"
)
var (
dumpColorLabel = color.New(color.BgBlack).Add(color.FgHiWhite).Add(color.Bold)
dumpColorString = color.New(color.BgBlack).Add(color.FgBlue).Add(color.Bold)
dumpColorNumber = color.New(color.BgBlack).Add(color.FgGreen).Add(color.Bold)
dumpColorBoolean = color.New(color.BgBlack).Add(color.FgYellow).Add(color.Bold)
dumpColorNull = color.New(color.BgBlack).Add(color.FgWhite).Add(color.Bold)
)
func dump(left func(line int), v interface{}, indent int, label string, line int) {
if label != "" {
left(line)
dumpColorLabel.Print(strings.Repeat(" ", indent))
dumpColorLabel.Print(label)
} else {
dumpColorLabel.Print(strings.Repeat(" ", indent))
}
switch t := v.(type) {
case string:
dumpColorString.Println("\"" + strings.ReplaceAll(t, "\n", "\\n") + "\"")
case float64, float32, int, int64, int32, int16, int8, uint, uint16, uint32, uint64:
dumpColorNumber.Println(t)
case bool:
if t {
dumpColorBoolean.Println("true")
} else {
dumpColorBoolean.Println("false")
}
case nil:
fmt.Println("null")
case LogData:
for k, value := range t {
dump(left, value, indent+1, k+": ", line+1)
}
case map[string]interface{}:
if len(t) > 0 {
fmt.Println()
}
for k, value := range t {
dump(left, value, indent+1, k+": ", line+1)
}
case map[string]string:
if len(t) > 0 {
fmt.Println()
}
for k, value := range t {
dump(left, value, indent+1, k+": ", line+1)
}
case []interface{}:
if len(t) > 0 {
fmt.Println()
}
for _, value := range t {
dump(left, value, indent+1, "- ", line+1)
}
default:
fmt.Printf("I don't know about type %T!\n", t)
}
}