/
utils.go
63 lines (52 loc) · 1.56 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
package snowflake
import (
"strconv"
"errors"
)
// 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(err) // 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
}