Utils: add nullable package
This commit is contained in:
parent
c1f942bb92
commit
a29a521c5a
|
@ -20,7 +20,7 @@ type CreateChannelData struct {
|
||||||
Name string `json:"name"` // 2-100
|
Name string `json:"name"` // 2-100
|
||||||
Topic string `json:"topic,omitempty"`
|
Topic string `json:"topic,omitempty"`
|
||||||
|
|
||||||
Type discord.ChannelType `json:"type,omitempty"`
|
Type *discord.ChannelType `json:"type,omitempty"`
|
||||||
|
|
||||||
VoiceBitrate uint `json:"bitrate,omitempty"`
|
VoiceBitrate uint `json:"bitrate,omitempty"`
|
||||||
VoiceUserLimit uint `json:"user_limit,omitempty"`
|
VoiceUserLimit uint `json:"user_limit,omitempty"`
|
||||||
|
|
12
api/guild.go
12
api/guild.go
|
@ -17,9 +17,9 @@ type CreateGuildData struct {
|
||||||
Icon Image `json:"image,omitempty"`
|
Icon Image `json:"image,omitempty"`
|
||||||
|
|
||||||
// package dc is just package discord
|
// package dc is just package discord
|
||||||
Verification discord.Verification `json:"verification_level"`
|
Verification *discord.Verification `json:"verification_level"`
|
||||||
Notification discord.Notification `json:"default_message_notifications"`
|
Notification *discord.Notification `json:"default_message_notifications"`
|
||||||
ExplicitFilter discord.ExplicitFilter `json:"explicit_content_filter"`
|
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter"`
|
||||||
|
|
||||||
// [0] (First entry) is ALWAYS @everyone.
|
// [0] (First entry) is ALWAYS @everyone.
|
||||||
Roles []discord.Role `json:"roles,omitempty"`
|
Roles []discord.Role `json:"roles,omitempty"`
|
||||||
|
@ -138,9 +138,9 @@ type ModifyGuildData struct {
|
||||||
Region option.String `json:"region,omitempty"`
|
Region option.String `json:"region,omitempty"`
|
||||||
|
|
||||||
// package d is just package discord
|
// package d is just package discord
|
||||||
Verification *discord.Verification `json:"verification_level,omitempty"`
|
Verification *discord.Verification `json:"verification_level,omitempty"` // nullable
|
||||||
Notification *discord.Notification `json:"default_message_notifications,omitempty"`
|
Notification *discord.Notification `json:"default_message_notifications,omitempty"` // nullable
|
||||||
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"`
|
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"` // nullable
|
||||||
|
|
||||||
AFKChannelID discord.Snowflake `json:"afk_channel_id,string,omitempty"`
|
AFKChannelID discord.Snowflake `json:"afk_channel_id,string,omitempty"`
|
||||||
AFKTimeout discord.Seconds `json:"afk_timeout,omitempty"`
|
AFKTimeout discord.Seconds `json:"afk_timeout,omitempty"`
|
||||||
|
|
|
@ -58,14 +58,14 @@ func (ch Channel) IconURL() string {
|
||||||
|
|
||||||
type ChannelType uint8
|
type ChannelType uint8
|
||||||
|
|
||||||
const (
|
var (
|
||||||
GuildText ChannelType = iota
|
GuildText ChannelType = 0
|
||||||
DirectMessage
|
DirectMessage ChannelType = 1
|
||||||
GuildVoice
|
GuildVoice ChannelType = 2
|
||||||
GroupDM
|
GroupDM ChannelType = 3
|
||||||
GuildCategory
|
GuildCategory ChannelType = 4
|
||||||
GuildNews
|
GuildNews ChannelType = 5
|
||||||
GuildStore
|
GuildStore ChannelType = 6
|
||||||
)
|
)
|
||||||
|
|
||||||
type Overwrite struct {
|
type Overwrite struct {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package discord
|
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.
|
// Guild.MaxPresences is 5000 when it's 0.
|
||||||
const DefaultMaxPresences = 5000
|
const DefaultMaxPresences = 5000
|
||||||
|
@ -52,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 option.Enum
|
type ExplicitFilter nullable.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 = option.EnumNull
|
NullExplicitFilter ExplicitFilter = nullable.EnumNull
|
||||||
// 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.
|
||||||
|
@ -67,21 +69,23 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (f *ExplicitFilter) UnmarshalJSON(b []byte) error {
|
func (f *ExplicitFilter) UnmarshalJSON(b []byte) error {
|
||||||
i, err := option.EnumFromJSON(b)
|
i, err := nullable.EnumFromJSON(b)
|
||||||
*f = ExplicitFilter(i)
|
*f = ExplicitFilter(i)
|
||||||
|
|
||||||
return err
|
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.
|
// Notification is the default message notification level of a guild.
|
||||||
type Notification option.Enum
|
type Notification nullable.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 = option.EnumNull
|
NullNotification Notification = nullable.EnumNull
|
||||||
// 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.
|
||||||
|
@ -89,21 +93,21 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (n *Notification) UnmarshalJSON(b []byte) error {
|
func (n *Notification) UnmarshalJSON(b []byte) error {
|
||||||
i, err := option.EnumFromJSON(b)
|
i, err := nullable.EnumFromJSON(b)
|
||||||
*n = Notification(i)
|
*n = Notification(i)
|
||||||
|
|
||||||
return err
|
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.
|
// Verification is the verification level required for a guild.
|
||||||
type Verification option.Enum
|
type Verification nullable.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 = option.EnumNull
|
NullVerification Verification = nullable.EnumNull
|
||||||
// NoVerification required no verification.
|
// NoVerification required no verification.
|
||||||
NoVerification Verification = 0
|
NoVerification Verification = 0
|
||||||
// LowVerification requires a verified email
|
// LowVerification requires a verified email
|
||||||
|
@ -120,13 +124,13 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v *Verification) UnmarshalJSON(b []byte) error {
|
func (v *Verification) UnmarshalJSON(b []byte) error {
|
||||||
i, err := option.EnumFromJSON(b)
|
i, err := nullable.EnumFromJSON(b)
|
||||||
*v = Verification(i)
|
*v = Verification(i)
|
||||||
|
|
||||||
return err
|
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.
|
// Service is used for guild integrations and user connections.
|
||||||
type Service string
|
type Service string
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Package nullable provides nullable types that get serialized to JSON null.
|
||||||
|
package nullable
|
|
@ -1,4 +1,4 @@
|
||||||
package option
|
package nullable
|
||||||
|
|
||||||
import "strconv"
|
import "strconv"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package option
|
package nullable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue