-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
105 lines (98 loc) · 2.02 KB
/
debug.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
//go:build debugNflex
// +build debugNflex
package nflex
import (
"fmt"
"log"
"strconv"
"strings"
"sync/atomic"
)
var counter int32 = 1000
func debug(args ...interface{}) {
c := make([]interface{}, len(args))
for i, arg := range args {
if f, ok := arg.(func() string); ok {
c[i] = f()
} else {
c[i] = arg
}
}
log.Println(c...)
}
func debugID() int {
return int(atomic.AddInt32(&counter, 1))
}
func id(raw Source) string {
switch s := raw.(type) {
case offset:
return fmt.Sprintf("O%d/%s", s.debugID, id(s.source))
case prefixSource:
return fmt.Sprintf("P%d/%s", s.debugID, id(s.source))
case parsedYAML:
return fmt.Sprintf("J%d/%v", s.debugID, s.pathToHere)
case parsedJSON:
return fmt.Sprintf("J%d/%v", s.debugID, s.pathToHere)
case *MultiSource:
ss := make([]string, len(s.sources))
for i, source := range s.sources {
ss[i] = id(source)
}
return fmt.Sprintf("M%d/%v<%s>", s.debugID, s.pathToHere, strings.Join(ss, "|"))
default:
return "?"
}
}
var debugCombine = combine
func debugKeys(s Source) string {
switch s.Type() {
case Undefined:
return "=UNDEFINED"
case Nil:
return "=NIL"
case String:
str, err := s.GetString()
if err != nil {
return "=s!" + err.Error() + "!"
}
return "='" + str + "'"
case Float:
f, err := s.GetFloat()
if err != nil {
return "=f!" + err.Error() + "!"
}
return strconv.FormatFloat(f, 'E', -1, 64)
case Int:
i, err := s.GetInt()
if err != nil {
return "=i!" + err.Error() + "!"
}
return strconv.FormatInt(i, 10)
case Bool:
b, err := s.GetBool()
if err != nil {
return "=b!" + err.Error() + "!"
}
return strconv.FormatBool(b)
case Slice:
l, err := s.Len()
if err != nil {
return "slice:!" + err.Error() + "!"
}
if l == 0 {
return "slice:[]"
}
return "slice:[0.." + strconv.Itoa(l-1) + "]"
case Map:
k, err := s.Keys()
if err != nil {
return "keys:!" + err.Error() + "!"
}
if len(k) == 0 {
return "keys:{}"
}
return `{"` + strings.Join(k, `", "`) + `"}`
default:
return "=???"
}
}