-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
55 lines (44 loc) · 1.1 KB
/
query.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
package bspc
import (
"encoding/json"
"fmt"
"strings"
)
type QueryResponseResolver func(payload []byte) error
// TODO: godoc
// TODO: restrict this somehow so that it only accepts structs?
// Maybe I can have an interface implemented by all the package's types, and that is required here.
func ToStruct(res interface{}) QueryResponseResolver {
return func(payload []byte) error {
if err := json.Unmarshal(payload, &res); err != nil {
return err
}
return nil
}
}
func ToID(res *ID) QueryResponseResolver {
return func(payload []byte) error {
id, err := hexToID(strings.ReplaceAll(string(payload), "\n", ""))
if err != nil {
return fmt.Errorf("failed to convert hex iD into ID type: %v", err)
}
*res = id
return nil
}
}
func ToIDSlice(res *[]ID) QueryResponseResolver {
return func(payload []byte) error {
lines := strings.Split(string(payload), "\n")
for _, l := range lines {
if l == "" {
continue
}
id, err := hexToID(l)
if err != nil {
return fmt.Errorf("failed to convert hex iD into ID type: %v", err)
}
*res = append(*res, id)
}
return nil
}
}