1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-30 18:53:30 +00:00

Utils: move Enum to package enum

This commit is contained in:
mavolin 2020-05-11 22:43:52 +02:00
parent d4bdf700f3
commit b3bfc29732
No known key found for this signature in database
GPG key ID: D8681218EDF216DF
3 changed files with 26 additions and 26 deletions

View file

@ -1,7 +1,7 @@
package discord package discord
import ( import (
"github.com/diamondburned/arikawa/utils/json/nullable" "github.com/diamondburned/arikawa/utils/json/enum"
) )
// Guild.MaxPresences is 5000 when it's 0. // Guild.MaxPresences is 5000 when it's 0.
@ -54,12 +54,12 @@ const (
) )
// ExplicitFilter is the explicit content filter level of a guild. // ExplicitFilter is the explicit content filter level of a guild.
type ExplicitFilter nullable.Enum type ExplicitFilter enum.Enum
var ( var (
// NullExplicitFilter serialized to JSON null. // NullExplicitFilter serialized to JSON null.
// This should only be used on nullable fields. // This should only be used on nullable fields.
NullExplicitFilter ExplicitFilter = nullable.EnumNull NullExplicitFilter ExplicitFilter = enum.Null
// NoContentFilter disables content filtering for the guild. // NoContentFilter disables content filtering for the guild.
NoContentFilter ExplicitFilter = 0 NoContentFilter ExplicitFilter = 0
// MembersWithoutRoles filters only members without roles. // MembersWithoutRoles filters only members without roles.
@ -69,23 +69,23 @@ var (
) )
func (f *ExplicitFilter) UnmarshalJSON(b []byte) error { func (f *ExplicitFilter) UnmarshalJSON(b []byte) error {
i, err := nullable.EnumFromJSON(b) i, err := enum.FromJSON(b)
*f = ExplicitFilter(i) *f = ExplicitFilter(i)
return err return err
} }
func (f ExplicitFilter) MarshalJSON() ([]byte, error) { func (f ExplicitFilter) MarshalJSON() ([]byte, error) {
return nullable.EnumToJSON(nullable.Enum(f)), nil return enum.ToJSON(enum.Enum(f)), nil
} }
// Notification is the default message notification level of a guild. // Notification is the default message notification level of a guild.
type Notification nullable.Enum type Notification enum.Enum
var ( var (
// NullNotification serialized to JSON null. // NullNotification serialized to JSON null.
// This should only be used on nullable fields. // This should only be used on nullable fields.
NullNotification Notification = nullable.EnumNull NullNotification Notification = enum.Null
// AllMessages sends notifications for all messages. // AllMessages sends notifications for all messages.
AllMessages Notification = 0 AllMessages Notification = 0
// OnlyMentions sends notifications only on mention. // OnlyMentions sends notifications only on mention.
@ -93,21 +93,21 @@ var (
) )
func (n *Notification) UnmarshalJSON(b []byte) error { func (n *Notification) UnmarshalJSON(b []byte) error {
i, err := nullable.EnumFromJSON(b) i, err := enum.FromJSON(b)
*n = Notification(i) *n = Notification(i)
return err return err
} }
func (n Notification) MarshalJSON() ([]byte, error) { return nullable.EnumToJSON(nullable.Enum(n)), nil } func (n Notification) MarshalJSON() ([]byte, error) { return enum.ToJSON(enum.Enum(n)), nil }
// Verification is the verification level required for a guild. // Verification is the verification level required for a guild.
type Verification nullable.Enum type Verification enum.Enum
var ( var (
// NullVerification serialized to JSON null. // NullVerification serialized to JSON null.
// This should only be used on nullable fields. // This should only be used on nullable fields.
NullVerification Verification = nullable.EnumNull NullVerification Verification = enum.Null
// NoVerification required no verification. // NoVerification required no verification.
NoVerification Verification = 0 NoVerification Verification = 0
// LowVerification requires a verified email // LowVerification requires a verified email
@ -124,13 +124,13 @@ var (
) )
func (v *Verification) UnmarshalJSON(b []byte) error { func (v *Verification) UnmarshalJSON(b []byte) error {
i, err := nullable.EnumFromJSON(b) i, err := enum.FromJSON(b)
*v = Verification(i) *v = Verification(i)
return err return err
} }
func (v Verification) MarshalJSON() ([]byte, error) { return nullable.EnumToJSON(nullable.Enum(v)), nil } func (v Verification) MarshalJSON() ([]byte, error) { return enum.ToJSON(enum.Enum(v)), nil }
// Service is used for guild integrations and user connections. // Service is used for guild integrations and user connections.
type Service string type Service string

View file

@ -1,20 +1,20 @@
package nullable package enum
import "strconv" import "strconv"
// EnumNull is the value used to represent JSON null. // Null is the value used to represent JSON null.
// It should never be used as a value, as it won't get serialized as such. // It should never be used as a value, as it won't get serialized as such.
const EnumNull = -1 const Null = -1
// Enum is a nullable version of a uint8. // Enum is a nullable version of a uint8.
// Enum values should only consist of positive values, as negative values are reserved for internal constants, such as // Enum values should only consist of positive values, as negative values are reserved for internal constants, such as
// EnumNull. // Null.
// This also mean that only 7 of the 8 Bit will be available for storage. // This also mean that only 7 of the 8 Bit will be available for storage.
type Enum int8 type Enum int8
// Int8ToJSON converts the passed Enum to a byte slice with it's JSON representation. // Int8ToJSON converts the passed Enum to a byte slice with it's JSON representation.
func EnumToJSON(i Enum) []byte { func ToJSON(i Enum) []byte {
if i == EnumNull { if i == Null {
return []byte("null") return []byte("null")
} else { } else {
return []byte(strconv.Itoa(int(i))) return []byte(strconv.Itoa(int(i)))
@ -22,11 +22,11 @@ func EnumToJSON(i Enum) []byte {
} }
// Int8FromJSON decodes the Enum stored as JSON src the passed byte slice. // Int8FromJSON decodes the Enum stored as JSON src the passed byte slice.
func EnumFromJSON(b []byte) (Enum, error) { func FromJSON(b []byte) (Enum, error) {
s := string(b) s := string(b)
if s == "null" { if s == "null" {
return EnumNull, nil return Null, nil
} else { } else {
i, err := strconv.ParseUint(s, 10, 7) i, err := strconv.ParseUint(s, 10, 7)
return Enum(i), err return Enum(i), err

View file

@ -1,4 +1,4 @@
package nullable package enum
import ( import (
"reflect" "reflect"
@ -13,7 +13,7 @@ func TestInt8ToJSON(t *testing.T) {
}{ }{
{ {
name: "null", name: "null",
src: EnumNull, src: Null,
expect: []byte("null"), expect: []byte("null"),
}, },
{ {
@ -25,7 +25,7 @@ func TestInt8ToJSON(t *testing.T) {
for _, c := range testCases { for _, c := range testCases {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
actual := EnumToJSON(c.src) actual := ToJSON(c.src)
if !reflect.DeepEqual(actual, c.expect) { if !reflect.DeepEqual(actual, c.expect) {
t.Errorf("expected nullable.Int8ToJSON to return: %+v, but got: %+v", c.expect, actual) t.Errorf("expected nullable.Int8ToJSON to return: %+v, but got: %+v", c.expect, actual)
@ -44,7 +44,7 @@ func TestInt8FromJSON(t *testing.T) {
{ {
name: "null", name: "null",
src: []byte("null"), src: []byte("null"),
expect: EnumNull, expect: Null,
err: false, err: false,
}, },
{ {
@ -63,7 +63,7 @@ func TestInt8FromJSON(t *testing.T) {
for _, c := range testCases { for _, c := range testCases {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
actual, err := EnumFromJSON(c.src) actual, err := FromJSON(c.src)
if c.err { if c.err {
if err == nil { if err == nil {