mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-03-24 10:59:18 +00:00
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
|
||||
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"`
|
||||
|
|
12
api/guild.go
12
api/guild.go
|
@ -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"`
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
14
utils/json/nullable/bool.go
Normal file
14
utils/json/nullable/bool.go
Normal 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
|
||||
}
|
2
utils/json/nullable/doc.go
Normal file
2
utils/json/nullable/doc.go
Normal file
|
@ -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"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package option
|
||||
package nullable
|
||||
|
||||
import (
|
||||
"reflect"
|
25
utils/json/nullable/number.go
Normal file
25
utils/json/nullable/number.go
Normal 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
|
||||
}
|
12
utils/json/nullable/string.go
Normal file
12
utils/json/nullable/string.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue