forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
77 lines (71 loc) · 1.59 KB
/
utils.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
package stream
import (
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
)
func decodeJSONHook(f reflect.Type, typ reflect.Type, data interface{}) (interface{}, error) {
switch typ {
case reflect.TypeOf(Time{}):
return timeFromString(data.(string))
case reflect.TypeOf(Duration{}):
switch v := data.(type) {
case string:
return durationFromString(v)
case float64:
return durationFromString(fmt.Sprintf("%fs", v))
default:
return nil, fmt.Errorf("invalid duration")
}
case reflect.TypeOf(Data{}):
switch v := data.(type) {
case string:
return Data{
ID: v,
}, nil
case map[string]interface{}:
a := Data{}
if err := a.decode(v); err != nil {
return nil, err
}
return a, nil
default:
return nil, fmt.Errorf("invalid data")
}
}
return data, nil
}
func decodeData(data map[string]interface{}, target interface{}) (*mapstructure.Metadata, error) {
cfg := &mapstructure.DecoderConfig{
DecodeHook: decodeJSONHook,
Result: target,
Metadata: &mapstructure.Metadata{},
TagName: "json",
}
dec, err := mapstructure.NewDecoder(cfg)
if err != nil {
return nil, err
}
if err := dec.Decode(data); err != nil {
return nil, err
}
return cfg.Metadata, nil
}
func parseIntValue(values url.Values, key string) (int, bool, error) {
v := values.Get(key)
if v == "" {
return 0, false, nil
}
i, err := strconv.Atoi(v)
if err != nil {
return 0, false, err
}
return i, true, nil
}
func parseBool(value string) bool {
v := strings.ToLower(value)
return v != "" && v != "false" && v != "f" && v != "0"
}