-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
75 lines (65 loc) · 1.63 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
package snowflake
import (
"errors"
"fmt"
"strconv"
)
// ParseSnowflakeString interprets a string with a decimal number.
// Note that in contrast to ParseUint, this function assumes the given string is
// always valid and thus will panic rather than return an error.
// This should only be used on checks that can be done at compile time,
// unless you want to trust other modules to returns valid data.
func ParseSnowflakeString(v string) Snowflake {
id, err := ParseSnowflakeUint(v, 10)
if err != nil {
panic(fmt.Errorf("unable to parse %s into snowflake", v)) // TODO
}
return id
}
// ParseUint converts a string and given base to a Snowflake
func ParseSnowflakeUint(v string, base int) (Snowflake, error) {
if v == "" {
return Snowflake(0), nil
}
id, err := strconv.ParseUint(v, base, 64)
return Snowflake(id), err
}
func GetSnowflake(v interface{}) (s Snowflake, err error) {
switch x := v.(type) {
case int:
s = NewSnowflake(uint64(x))
case int8:
s = NewSnowflake(uint64(x))
case int16:
s = NewSnowflake(uint64(x))
case int32:
s = NewSnowflake(uint64(x))
case int64:
s = NewSnowflake(uint64(x))
case uint:
s = NewSnowflake(uint64(x))
case uint8:
s = NewSnowflake(uint64(x))
case uint16:
s = NewSnowflake(uint64(x))
case uint32:
s = NewSnowflake(uint64(x))
case uint64:
s = NewSnowflake(x)
case string:
i, err := strconv.ParseUint(x, 10, 64)
if err != nil {
s = NewSnowflake(0)
} else {
s = NewSnowflake(i)
}
case Snowflake:
s = x
case SnowflakeJSON:
s = x.ID
default:
s = NewSnowflake(0)
err = errors.New("not supported type for snowflake")
}
return
}