forked from Unity-Technologies/go-ts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
189 lines (164 loc) · 3.85 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
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)
// TODO(steve): support groups
key := Decode(parts[0])
if len(parts) == 2 {
v := Decode(parts[1])
if i, err := strconv.Atoi(v); err != nil {
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: mapstructure.ComposeDecodeHookFunc(
sliceHookFunc,
timeHookFunc,
),
}
dec, err := mapstructure.NewDecoder(cfg)
if err != nil {
return err
}
return dec.Decode(d)
}
// 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)
}
if err := decodeMap(input, v.Interface()); err != nil {
return err
}
if elemType.Kind() == reflect.Struct {
v = v.Elem()
}
slice.Set(reflect.Append(slice, v))
return nil
}
// sliceHookFunc supports int/string decoding to slice
func sliceHookFunc(from reflect.Kind, to reflect.Kind, data interface{}) (interface{}, error) {
if to == reflect.Slice {
switch from {
case reflect.String:
return strings.Split(data.(string), ","), nil
case reflect.Int:
return []int{data.(int)}, nil
}
}
return data, 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: %v", data, err)
}
}
if timeInt > 0 {
return time.Unix(timeInt, 0), nil
}
}
return data, nil
}