Utils: add NullableX types in option

This commit is contained in:
mavolin 2020-05-11 23:32:22 +02:00
parent b3bfc29732
commit bc188140f0
No known key found for this signature in database
GPG Key ID: D8681218EDF216DF
7 changed files with 179 additions and 56 deletions

View File

@ -1,14 +0,0 @@
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

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

View File

@ -1,25 +0,0 @@
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

@ -1,12 +0,0 @@
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
}

View File

@ -1,5 +1,9 @@
package option
import "strconv"
// ================================ Bool ================================
// Bool is the option type for bool.
type Bool *bool
@ -10,3 +14,46 @@ var (
// newBool creates a new Bool with the value of the passed bool.
func newBool(b bool) Bool { return &b }
// ================================ NullableBool ================================
// NullableBool is the nullable type for bool.
type NullableBool = *nullableBool
type nullableBool struct {
Val bool
Init bool
}
var (
// NullBool serializes to JSON null.
NullBool = &nullableBool{}
NullableTrue = &nullableBool{
Val: true,
Init: true,
}
NullableFalse = &nullableBool{
Val: false,
Init: true,
}
)
func (b nullableBool) 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) {
s := string(json)
if s == "null" {
b.Init = false
return
}
b.Val, err = strconv.ParseBool(s)
return
}

View File

@ -1,5 +1,7 @@
package option
import "strconv"
// ================================ Uint ================================
// Uint is the option type for unsigned integers (uint).
@ -21,3 +23,89 @@ var ZeroInt = NewInt(0)
// NewInt creates a new Int using the value of the passed int.
func NewInt(i int) Int { return &i }
// ================================ NullableUint ================================
// NullableUint is a nullable version of an unsigned integer (uint).
type NullableUint = *nullableUint
type nullableUint struct {
Val uint
Init bool
}
// NullUint serializes to JSON null.
var NullUint = &nullableUint{}
// NewUint creates a new non-null NullableUint using the value of the passed uint.
func NewNullableUint(v uint) NullableUint {
return &nullableUint{
Val: v,
Init: true,
}
}
func (u nullableUint) 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 {
s := string(json)
if s == "null" {
u.Init = false
return nil
}
v, err := strconv.ParseUint(s, 10, 64)
u.Val = uint(v)
return err
}
// ================================ NullableInt ================================
// NullableInt is a nullable version of an integer (int).
type NullableInt *nullableInt
type nullableInt struct {
Val int
Init bool
}
// NullUint serializes to JSON null.
var NullInt = &nullableUint{}
// NewInt creates a new non-null NullableInt using the value of the passed int.
func NewNullableInt(v int) NullableInt {
return &nullableInt{
Val: v,
Init: true,
}
}
func (i nullableInt) 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 {
s := string(json)
if s == "null" {
i.Init = false
return nil
}
v, err := strconv.ParseUint(s, 10, 64)
i.Val = int(v)
return err
}

View File

@ -1,10 +1,51 @@
package option
import (
"encoding/json"
)
// ================================ String ================================
// String is the option type for strings.
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 }
// ================================ NullableString ================================
// NullableString is a nullable version of a string.
type NullableString = *nullableString
type nullableString struct {
Val string
Init bool
}
// NullBool serializes to JSON null.
var NullString = &nullableString{}
// NewNullableString creates a new non-null NullableString with the value of the passed string.
func NewNullableString(v string) NullableString {
return &nullableString{
Val: v,
Init: true,
}
}
func (s nullableString) MarshalJSON() ([]byte, error) {
if !s.Init {
return []byte("null"), nil
}
return []byte("\"" + s.Val + "\""), nil
}
func (s *nullableString) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
s.Init = false
return nil
}
return json.Unmarshal(b, &s.Val)
}