Utils: Exposed NullableTData structs

This commit is contained in:
diamondburned (Forefront) 2020-05-12 17:09:43 -07:00
parent cc4e8c0966
commit ae793848aa
5 changed files with 36 additions and 37 deletions

View File

@ -18,34 +18,34 @@ func newBool(b bool) Bool { return &b }
// ================================ NullableBool ================================
// NullableBool is the nullable type for bool.
type NullableBool = *nullableBool
type NullableBool = *NullableBoolData
type nullableBool struct {
type NullableBoolData struct {
Val bool
Init bool
}
var (
// NullBool serializes to JSON null.
NullBool = &nullableBool{}
NullableTrue = &nullableBool{
NullBool = &NullableBoolData{}
NullableTrue = &NullableBoolData{
Val: true,
Init: true,
}
NullableFalse = &nullableBool{
NullableFalse = &NullableBoolData{
Val: false,
Init: true,
}
)
func (b nullableBool) MarshalJSON() ([]byte, error) {
func (b NullableBoolData) MarshalJSON() ([]byte, error) {
if !b.Init {
return []byte("null"), nil
}
return []byte(strconv.FormatBool(b.Val)), nil
}
func (b *nullableBool) UnmarshalJSON(json []byte) (err error) {
func (b *NullableBoolData) UnmarshalJSON(json []byte) (err error) {
s := string(json)
if s == "null" {

View File

@ -28,33 +28,33 @@ func NewColor(s discord.Color) Color { return &s }
// ================================ NullableColor ================================
// Nullable is a nullable version of discord.Color.
type NullableColor = *nullableColor
type NullableColor = *NullableColorData
type nullableColor struct {
type NullableColorData struct {
Val discord.Color
Init bool
}
// NullColor serializes to JSON null.
var NullColor = &nullableColor{}
var NullColor = &NullableColorData{}
// NewNullableColor creates a new non-null NullableColor using the value of the
// passed discord.Color.
func NewNullableColor(v discord.Color) NullableColor {
return &nullableColor{
return &NullableColorData{
Val: v,
Init: true,
}
}
func (i nullableColor) MarshalJSON() ([]byte, error) {
func (i NullableColorData) MarshalJSON() ([]byte, error) {
if !i.Init {
return []byte("null"), nil
}
return []byte(strconv.FormatUint(uint64(i.Val), 10)), nil
}
func (i *nullableColor) UnmarshalJSON(json []byte) error {
func (i *NullableColorData) UnmarshalJSON(json []byte) error {
s := string(json)
if s == "null" {

View File

@ -27,32 +27,32 @@ func NewInt(i int) Int { return &i }
// ================================ NullableUint ================================
// NullableUint is a nullable version of an unsigned integer (uint).
type NullableUint = *nullableUint
type NullableUint = *NullableUintData
type nullableUint struct {
type NullableUintData struct {
Val uint
Init bool
}
// NullUint serializes to JSON null.
var NullUint = &nullableUint{}
var NullUint = &NullableUintData{}
// NewUint creates a new non-null NullableUint using the value of the passed uint.
func NewNullableUint(v uint) NullableUint {
return &nullableUint{
return &NullableUintData{
Val: v,
Init: true,
}
}
func (u nullableUint) MarshalJSON() ([]byte, error) {
func (u NullableUintData) MarshalJSON() ([]byte, error) {
if !u.Init {
return []byte("null"), nil
}
return []byte(strconv.FormatUint(uint64(u.Val), 10)), nil
}
func (u *nullableUint) UnmarshalJSON(json []byte) error {
func (u *NullableUintData) UnmarshalJSON(json []byte) error {
s := string(json)
if s == "null" {
@ -70,32 +70,32 @@ func (u *nullableUint) UnmarshalJSON(json []byte) error {
// ================================ NullableInt ================================
// NullableInt is a nullable version of an integer (int).
type NullableInt *nullableInt
type NullableInt = *NullableIntData
type nullableInt struct {
type NullableIntData struct {
Val int
Init bool
}
// NullInt serializes to JSON null.
var NullInt = &nullableUint{}
var NullInt = &NullableIntData{}
// NewInt creates a new non-null NullableInt using the value of the passed int.
func NewNullableInt(v int) NullableInt {
return &nullableInt{
return &NullableIntData{
Val: v,
Init: true,
}
}
func (i nullableInt) MarshalJSON() ([]byte, error) {
func (i NullableIntData) MarshalJSON() ([]byte, error) {
if !i.Init {
return []byte("null"), nil
}
return []byte(strconv.FormatUint(uint64(i.Val), 10)), nil
}
func (i *nullableInt) UnmarshalJSON(json []byte) error {
func (i *NullableIntData) UnmarshalJSON(json []byte) error {
s := string(json)
if s == "null" {

View File

@ -1,5 +1,6 @@
// Package option provides the ability to create omittable primitives.
// This is accomplished by pointerrizing common primitive types so that they may assume a nil value, which is considered
// as omitted by encoding/json.
// To generate pointerrized primitives, there are helper functions `NewT` for each option type.
// This is accomplished by pointerrizing common primitive types so that they may
// assume a nil value, which is considered as omitted by encoding/json.
// To generate pointerrized primitives, there are helper functions NewT() for
// each option type.
package option

View File

@ -15,37 +15,35 @@ func NewString(s string) String { return &s }
// ================================ NullableString ================================
// NullableString is a nullable version of a string.
type NullableString = *nullableString
type NullableString = *NullableStringData
type nullableString struct {
type NullableStringData struct {
Val string
Init bool
}
// NullBool serializes to JSON null.
var NullString = &nullableString{}
var NullString = &NullableStringData{}
// NewNullableString creates a new non-null NullableString with the value of the passed string.
func NewNullableString(v string) NullableString {
return &nullableString{
return &NullableStringData{
Val: v,
Init: true,
}
}
func (s nullableString) MarshalJSON() ([]byte, error) {
func (s NullableStringData) MarshalJSON() ([]byte, error) {
if !s.Init {
return []byte("null"), nil
}
return []byte("\"" + s.Val + "\""), nil
return json.Marshal(s.Val)
}
func (s *nullableString) UnmarshalJSON(b []byte) error {
func (s *NullableStringData) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
s.Init = false
return nil
}
return json.Unmarshal(b, &s.Val)
}