This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.go
171 lines (158 loc) · 3.35 KB
/
util.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
package dendrite
import (
"fmt"
"github.com/kylelemons/go-gypsy/yaml"
"io"
"strconv"
)
func Unescape(in string) string {
bytes := []byte(in)
out := make([]byte, 0, len(bytes)-2)
for i := 1; i < len(bytes)-1; i++ {
c := bytes[i]
if c == '\\' {
i++
switch bytes[i] {
case 't':
c = '\t'
case 'n':
c = '\n'
case 'r':
c = '\r'
default:
c = bytes[i]
}
}
out = append(out, c)
}
return string(out)
}
func YamlUnmarshal(node yaml.Node) interface{} {
switch node := node.(type) {
case yaml.Map:
out := make(map[string]interface{})
for k, v := range node {
out[k] = YamlUnmarshal(v)
}
return out
case yaml.List:
out := make([]interface{}, 0)
for _, v := range node {
out = append(out, YamlUnmarshal(v))
}
return out
case yaml.Scalar:
if len(node) > 0 && node[0] == '"' {
return Unescape(string(node))
} else {
return node
}
default:
}
return nil
}
type anyReader struct {
readers []io.Reader
}
func NewAnyReader(r []io.Reader) io.Reader {
return &anyReader{r}
}
func (any *anyReader) Read(buf []byte) (int, error) {
for _, r := range any.readers {
i, e := r.Read(buf)
if e == io.EOF {
continue
} else {
return i, e
}
}
return 0, io.EOF
}
func RecursiveMergeNoConflict(a map[string]interface{}, b map[string]interface{}, path string) error {
for k, v := range b {
old, found := a[k]
if found {
switch old := old.(type) {
case map[string]interface{}:
switch v := v.(type) {
case map[string]interface{}:
err := RecursiveMergeNoConflict(old, v, path+"/"+k)
if err != nil {
return err
}
default:
return fmt.Errorf("Overwriting map with scalar value at %s/%s", path, k)
}
default:
return fmt.Errorf("Duplicate value at %s/%s", path, k)
}
} else {
a[k] = b[k]
}
}
return nil
}
func parseFieldType(str string) (FieldType, error) {
switch str {
case "int":
return Integer, nil
case "double":
return Double, nil
case "string", "":
return String, nil
case "timestamp", "date":
return Timestamp, nil
}
return -1, fmt.Errorf("Can't recognize field type: %s", str)
}
func parseFieldTreatment(str string) (FieldTreatment, error) {
switch str {
case "simple", "":
return Simple, nil
case "gauge":
return Gauge, nil
case "metric":
return Metric, nil
case "counter":
return Counter, nil
case "tokenized":
return Tokens, nil
case "hash":
return Hash, nil
}
return -1, fmt.Errorf("Can't recognize field treatment: %s", str)
}
func getMap(mapping map[string]interface{}, key string) (map[string]interface{}, error) {
val, ok := mapping[key]
if ok {
switch val := val.(type) {
case map[string]interface{}:
return val, nil
default:
return nil, fmt.Errorf("key is not a map: %s", key)
}
}
return nil, fmt.Errorf("key not found: %s", key)
}
func getString(mapping map[string]interface{}, key string) (string, error) {
val, ok := mapping[key]
if ok {
return fmt.Sprintf("%s", val), nil
}
return "", fmt.Errorf("key not found: %s", key)
}
func getInt64(mapping map[string]interface{}, key string) (int64, error) {
val, err := getString(mapping, key)
if err != nil {
return -1, err
}
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return -1, err
}
return i, nil
}
func getInt(mapping map[string]interface{}, key string) (int, error) {
a, b := getInt64(mapping, key)
return int(a), b
}