mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-12-02 09:47:52 +00:00
Discord: Fixed Snowflake Valid() returning true for null
This commit is contained in:
parent
763999a81e
commit
1ca7d1c62c
|
|
@ -27,6 +27,10 @@ func NewSnowflake(t time.Time) Snowflake {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseSnowflake(sf string) (Snowflake, error) {
|
func ParseSnowflake(sf string) (Snowflake, error) {
|
||||||
|
if sf == "null" {
|
||||||
|
return NullSnowflake, nil
|
||||||
|
}
|
||||||
|
|
||||||
i, err := strconv.ParseInt(sf, 10, 64)
|
i, err := strconv.ParseInt(sf, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
@ -36,24 +40,19 @@ func ParseSnowflake(sf string) (Snowflake, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Snowflake) UnmarshalJSON(v []byte) error {
|
func (s *Snowflake) UnmarshalJSON(v []byte) error {
|
||||||
id := strings.Trim(string(v), `"`)
|
p, err := ParseSnowflake(strings.Trim(string(v), `"`))
|
||||||
if id == "null" {
|
|
||||||
// Use -1 for null.
|
|
||||||
*s = -1
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
i, err := strconv.ParseInt(id, 10, 64)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*s = Snowflake(i)
|
*s = p
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Snowflake) MarshalJSON() ([]byte, error) {
|
func (s Snowflake) MarshalJSON() ([]byte, error) {
|
||||||
if s < 1 {
|
// This includes 0 and null, because MarshalJSON does not dictate when a
|
||||||
|
// value gets omitted.
|
||||||
|
if !s.Valid() {
|
||||||
return []byte("null"), nil
|
return []byte("null"), nil
|
||||||
} else {
|
} else {
|
||||||
return []byte(`"` + strconv.FormatInt(int64(s), 10) + `"`), nil
|
return []byte(`"` + strconv.FormatInt(int64(s), 10) + `"`), nil
|
||||||
|
|
@ -62,14 +61,16 @@ func (s Snowflake) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
// String returns the ID, or nothing if the snowflake isn't valid.
|
// String returns the ID, or nothing if the snowflake isn't valid.
|
||||||
func (s Snowflake) String() string {
|
func (s Snowflake) String() string {
|
||||||
|
// Check if negative.
|
||||||
if !s.Valid() {
|
if !s.Valid() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return strconv.FormatUint(uint64(s), 10)
|
return strconv.FormatUint(uint64(s), 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid returns whether or not the snowflake is valid.
|
||||||
func (s Snowflake) Valid() bool {
|
func (s Snowflake) Valid() bool {
|
||||||
return uint64(s) > 0
|
return int64(s) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Snowflake) Time() time.Time {
|
func (s Snowflake) Time() time.Time {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue