Utils: add nullable package

This commit is contained in:
mavolin 2020-05-11 04:30:34 +02:00
parent c1f942bb92
commit a29a521c5a
No known key found for this signature in database
GPG Key ID: D8681218EDF216DF
10 changed files with 87 additions and 30 deletions

View File

@ -20,7 +20,7 @@ type CreateChannelData struct {
Name string `json:"name"` // 2-100
Topic string `json:"topic,omitempty"`
Type discord.ChannelType `json:"type,omitempty"`
Type *discord.ChannelType `json:"type,omitempty"`
VoiceBitrate uint `json:"bitrate,omitempty"`
VoiceUserLimit uint `json:"user_limit,omitempty"`

View File

@ -17,9 +17,9 @@ type CreateGuildData struct {
Icon Image `json:"image,omitempty"`
// package dc is just package discord
Verification discord.Verification `json:"verification_level"`
Notification discord.Notification `json:"default_message_notifications"`
ExplicitFilter discord.ExplicitFilter `json:"explicit_content_filter"`
Verification *discord.Verification `json:"verification_level"`
Notification *discord.Notification `json:"default_message_notifications"`
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter"`
// [0] (First entry) is ALWAYS @everyone.
Roles []discord.Role `json:"roles,omitempty"`
@ -138,9 +138,9 @@ type ModifyGuildData struct {
Region option.String `json:"region,omitempty"`
// package d is just package discord
Verification *discord.Verification `json:"verification_level,omitempty"`
Notification *discord.Notification `json:"default_message_notifications,omitempty"`
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"`
Verification *discord.Verification `json:"verification_level,omitempty"` // nullable
Notification *discord.Notification `json:"default_message_notifications,omitempty"` // nullable
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"` // nullable
AFKChannelID discord.Snowflake `json:"afk_channel_id,string,omitempty"`
AFKTimeout discord.Seconds `json:"afk_timeout,omitempty"`

View File

@ -58,14 +58,14 @@ func (ch Channel) IconURL() string {
type ChannelType uint8
const (
GuildText ChannelType = iota
DirectMessage
GuildVoice
GroupDM
GuildCategory
GuildNews
GuildStore
var (
GuildText ChannelType = 0
DirectMessage ChannelType = 1
GuildVoice ChannelType = 2
GroupDM ChannelType = 3
GuildCategory ChannelType = 4
GuildNews ChannelType = 5
GuildStore ChannelType = 6
)
type Overwrite struct {

View File

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

View File

@ -0,0 +1,14 @@
package nullable
// Bool is a nullable version of a bool.
type Bool *bool
var (
True = newBool(true)
False = newBool(false)
)
// newBool creates a new Bool with the value of the passed bool.
func newBool(b bool) Bool {
return &b
}

View File

@ -0,0 +1,2 @@
// Package nullable provides nullable types that get serialized to JSON null.
package nullable

View File

@ -1,4 +1,4 @@
package option
package nullable
import "strconv"

View File

@ -1,4 +1,4 @@
package option
package nullable
import (
"reflect"

View File

@ -0,0 +1,25 @@
package nullable
type (
// Uint is a nullable version of an unsigned integer (uint).
Uint *uint
// Int is a nullable version of an integer (int).
Int *int
)
var (
// ZeroUint is a Uint with 0 as value.
ZeroUint = NewUint(0)
// ZeroInt is an Int with 0 as value.
ZeroInt = NewInt(0)
)
// NewUint creates a new Uint using the value of the passed uint.
func NewUint(u uint) Uint {
return &u
}
// NewInt creates a new Int using the value of the passed int.
func NewInt(i int) Int {
return &i
}

View File

@ -0,0 +1,12 @@
package nullable
// String is a nullable version of a string.
type String *string
// EmptyString is a zero-length string.
var EmptyString = NewString("")
// NewString creates a new String with the value of the passed string.
func NewString(s string) String {
return &s
}