-
Notifications
You must be signed in to change notification settings - Fork 21
/
helpers.go
236 lines (205 loc) · 5.44 KB
/
helpers.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
package ts3
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/mitchellh/mapstructure"
)
var (
// encoder performs white space and special character encoding
// as required by the ServerQuery protocol.
encoder = strings.NewReplacer(
`\`, `\\`,
`/`, `\/`,
` `, `\s`,
`|`, `\p`,
"\a", `\a`,
"\b", `\b`,
"\f", `\f`,
"\n", `\n`,
"\r", `\r`,
"\t", `\t`,
"\v", `\v`,
)
// decoder performs white space and special character decoding
// as required by the ServerQuery protocol.
decoder = strings.NewReplacer(
`\\`, "\\",
`\/`, "/",
`\s`, " ",
`\p`, "|",
`\a`, "\a",
`\b`, "\b",
`\f`, "\f",
`\n`, "\n",
`\r`, "\r",
`\t`, "\t",
`\v`, "\v",
)
)
// Decode returns a decoded version of str.
func Decode(str string) string {
return decoder.Replace(str)
}
// DecodeResponse decodes a response into a struct.
func DecodeResponse(lines []string, v interface{}) error {
if len(lines) > 1 {
return NewInvalidResponseError("too many lines", lines)
} else if len(lines) == 0 {
return NewInvalidResponseError("no lines", lines)
}
input := make(map[string]interface{})
value := reflect.ValueOf(v)
var slice reflect.Value
var elemType reflect.Type
if value.Kind() == reflect.Ptr {
slice = value.Elem()
if slice.Kind() == reflect.Slice {
elemType = slice.Type().Elem()
}
}
for _, part := range strings.Split(lines[0], "|") {
for _, val := range strings.Split(part, " ") {
parts := strings.SplitN(val, "=", 2)
key := Decode(parts[0])
if len(parts) == 2 {
v := Decode(parts[1])
if i, err := strconv.Atoi(v); err != nil {
// Only support comma seperated lists
// by keyname to avoid incorrect decoding.
if key == "client_servergroups" {
parts := strings.Split(v, ",")
serverGroups := make([]int, len(parts))
for i, s := range parts {
group, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("decode server group: %w", err)
}
serverGroups[i] = group
}
input[key] = serverGroups
} else {
input[key] = v
}
} else {
input[key] = i
}
} else {
input[key] = ""
}
}
if elemType != nil {
// Expecting a slice
if err := decodeSlice(elemType, slice, input); err != nil {
return err
}
// Reset the input map
input = make(map[string]interface{})
}
}
if elemType != nil {
// Expecting a slice, already decoded
return nil
}
return decodeMap(input, v)
}
// decodeMap decodes input into r.
func decodeMap(d map[string]interface{}, r interface{}) error {
cfg := &mapstructure.DecoderConfig{
WeaklyTypedInput: true,
TagName: "ms",
Result: r,
DecodeHook: timeHookFunc,
}
dec, err := mapstructure.NewDecoder(cfg)
if err != nil {
return fmt.Errorf("decode map: new decoder %w", err)
}
if err := dec.Decode(d); err != nil {
return fmt.Errorf("decode map: decode: %w", err)
}
return nil
}
// decodeSlice decodes input into slice.
func decodeSlice(elemType reflect.Type, slice reflect.Value, input map[string]interface{}) error {
var v reflect.Value
if elemType.Kind() == reflect.Ptr {
v = reflect.New(elemType.Elem())
} else {
v = reflect.New(elemType)
}
if !v.CanInterface() {
return fmt.Errorf("can't interface %#v", v)
}
// The mapstructure's decoder doesn't support squashing
// for embedded pointers to structs (the type is lost when
// using reflection for nil values). We need to add pointers
// to empty structs within the interface to get around this.
switch v.Interface().(type) {
case *OnlineClient:
ext := &OnlineClientExt{
OnlineClientGroups: &OnlineClientGroups{},
OnlineClientInfo: &OnlineClientInfo{},
OnlineClientTimes: &OnlineClientTimes{},
OnlineClientVoice: &OnlineClientVoice{},
}
v.Interface().(*OnlineClient).OnlineClientExt = ext
}
if err := decodeMap(input, v.Interface()); err != nil {
return err
}
// nil out empty structs
switch v.Interface().(type) {
case *OnlineClient:
ext := v.Interface().(*OnlineClient).OnlineClientExt
emptyExt := OnlineClientExt{}
emptyExtGroups := OnlineClientGroups{}
emptyExtInfo := OnlineClientInfo{}
emptyExtTimes := OnlineClientTimes{}
emptyExtVoice := OnlineClientVoice{}
if *ext.OnlineClientGroups == emptyExtGroups {
v.Interface().(*OnlineClient).OnlineClientExt.OnlineClientGroups = nil
}
if *ext.OnlineClientInfo == emptyExtInfo {
v.Interface().(*OnlineClient).OnlineClientExt.OnlineClientInfo = nil
}
if *ext.OnlineClientTimes == emptyExtTimes {
v.Interface().(*OnlineClient).OnlineClientExt.OnlineClientTimes = nil
}
if *ext.OnlineClientVoice == emptyExtVoice {
v.Interface().(*OnlineClient).OnlineClientExt.OnlineClientVoice = nil
}
if *ext == emptyExt {
v.Interface().(*OnlineClient).OnlineClientExt = nil
}
}
if elemType.Kind() == reflect.Struct {
v = v.Elem()
}
slice.Set(reflect.Append(slice, v))
return nil
}
var timeType = reflect.TypeOf(time.Time{})
// timeHookFunc supports decoding to time.
func timeHookFunc(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
// Decode time.Time
if to == timeType {
var timeInt int64
switch from.Kind() {
case reflect.Int:
timeInt = int64(data.(int))
case reflect.String:
var err error
timeInt, err = strconv.ParseInt(data.(string), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid time %q: %w", data, err)
}
}
if timeInt > 0 {
return time.Unix(timeInt, 0), nil
}
}
return data, nil
}