-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnowflake.go
42 lines (34 loc) · 842 Bytes
/
snowflake.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
package corde
import (
"encoding/json"
"strconv"
)
// Snowflake is a DiscordSnowflake ID
type Snowflake uint64
// corde.SnowflakeFromString returns aSnowflake from a string
func SnowflakeFromString(s string) Snowflake {
i, _ := strconv.ParseUint(s, 10, 64)
return Snowflake(i)
}
// String implements fmt.Stringer
func (s Snowflake) String() string {
return strconv.FormatUint(uint64(s), 10)
}
// MarshalJSON implements json.Marshaler
func (s Snowflake) MarshalJSON() ([]byte, error) {
b := strconv.FormatUint(uint64(s), 10)
return json.Marshal(b)
}
// UnmarshalJSON implements json.Unmarshaler
func (s *Snowflake) UnmarshalJSON(b []byte) error {
str, err := strconv.Unquote(string(b))
if err != nil {
return err
}
i, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return err
}
*s = Snowflake(i)
return nil
}