1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-01 03:03:48 +00:00
arikawa/utils/json/option/custom.go

72 lines
1.6 KiB
Go
Raw Normal View History

2020-05-11 03:24:12 +00:00
package option
2020-05-11 22:06:19 +00:00
import (
"strconv"
2020-10-28 22:39:59 +00:00
"github.com/diamondburned/arikawa/v2/discord"
2020-05-11 22:06:19 +00:00
)
2020-05-11 03:24:12 +00:00
// ================================ Seconds ================================
// Seconds is the option type for discord.Seconds.
type Seconds = *discord.Seconds
2020-05-11 03:24:12 +00:00
// ZeroSeconds are 0 Seconds.
var ZeroSeconds = NewSeconds(0)
// NewString creates a new Seconds with the value of the passed discord.Seconds.
func NewSeconds(s discord.Seconds) Seconds { return &s }
// ================================ Color ================================
// Color is the option type for discord.Color.
type Color = *discord.Color
2020-05-11 03:24:12 +00:00
// NewString creates a new Color with the value of the passed discord.Color.
func NewColor(s discord.Color) Color { return &s }
2020-05-11 22:06:19 +00:00
// ================================ NullableColor ================================
// Nullable is a nullable version of discord.Color.
2020-05-13 00:09:43 +00:00
type NullableColor = *NullableColorData
2020-05-11 22:06:19 +00:00
2020-05-13 00:09:43 +00:00
type NullableColorData struct {
2020-05-11 22:06:19 +00:00
Val discord.Color
Init bool
}
// NullColor serializes to JSON null.
2020-05-13 00:09:43 +00:00
var NullColor = &NullableColorData{}
2020-05-11 22:06:19 +00:00
// NewNullableColor creates a new non-null NullableColor using the value of the
// passed discord.Color.
func NewNullableColor(v discord.Color) NullableColor {
2020-05-13 00:09:43 +00:00
return &NullableColorData{
2020-05-11 22:06:19 +00:00
Val: v,
Init: true,
}
}
2020-05-13 00:09:43 +00:00
func (i NullableColorData) MarshalJSON() ([]byte, error) {
2020-05-11 22:06:19 +00:00
if !i.Init {
return []byte("null"), nil
}
return []byte(strconv.FormatUint(uint64(i.Val), 10)), nil
}
2020-05-13 00:09:43 +00:00
func (i *NullableColorData) UnmarshalJSON(json []byte) error {
2020-05-11 22:06:19 +00:00
s := string(json)
if s == "null" {
*i = *NullColor
2020-05-11 22:06:19 +00:00
return nil
}
v, err := strconv.ParseUint(s, 10, 32)
i.Val = discord.Color(v)
2020-05-22 17:51:15 +00:00
i.Init = true
2020-05-11 22:06:19 +00:00
return err
}