2020-05-11 01:14:30 +00:00
|
|
|
package option
|
|
|
|
|
2020-05-11 21:32:22 +00:00
|
|
|
import "strconv"
|
|
|
|
|
2020-05-11 03:24:59 +00:00
|
|
|
// ================================ Uint ================================
|
|
|
|
|
|
|
|
// Uint is the option type for unsigned integers (uint).
|
|
|
|
type Uint *uint
|
|
|
|
|
|
|
|
// ZeroUint is a Uint with 0 as value.
|
|
|
|
var ZeroUint = NewUint(0)
|
2020-05-11 01:14:30 +00:00
|
|
|
|
|
|
|
// NewUint creates a new Uint using the value of the passed uint.
|
2020-05-11 03:24:59 +00:00
|
|
|
func NewUint(u uint) Uint { return &u }
|
|
|
|
|
|
|
|
// ================================ Int ================================
|
|
|
|
|
|
|
|
// Int is the option type for integers (int).
|
|
|
|
type Int *int
|
|
|
|
|
|
|
|
// ZeroInt is an Int with 0 as value.
|
|
|
|
var ZeroInt = NewInt(0)
|
2020-05-11 01:14:30 +00:00
|
|
|
|
|
|
|
// NewInt creates a new Int using the value of the passed int.
|
2020-05-11 03:24:59 +00:00
|
|
|
func NewInt(i int) Int { return &i }
|
2020-05-11 21:32:22 +00:00
|
|
|
|
|
|
|
// ================================ NullableUint ================================
|
|
|
|
|
|
|
|
// NullableUint is a nullable version of an unsigned integer (uint).
|
2020-05-13 00:09:43 +00:00
|
|
|
type NullableUint = *NullableUintData
|
2020-05-11 21:32:22 +00:00
|
|
|
|
2020-05-13 00:09:43 +00:00
|
|
|
type NullableUintData struct {
|
2020-05-11 21:32:22 +00:00
|
|
|
Val uint
|
|
|
|
Init bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NullUint serializes to JSON null.
|
2020-05-13 00:09:43 +00:00
|
|
|
var NullUint = &NullableUintData{}
|
2020-05-11 21:32:22 +00:00
|
|
|
|
|
|
|
// NewUint creates a new non-null NullableUint using the value of the passed uint.
|
|
|
|
func NewNullableUint(v uint) NullableUint {
|
2020-05-13 00:09:43 +00:00
|
|
|
return &NullableUintData{
|
2020-05-11 21:32:22 +00:00
|
|
|
Val: v,
|
|
|
|
Init: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 00:09:43 +00:00
|
|
|
func (u NullableUintData) MarshalJSON() ([]byte, error) {
|
2020-05-11 21:32:22 +00:00
|
|
|
if !u.Init {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
return []byte(strconv.FormatUint(uint64(u.Val), 10)), nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 00:09:43 +00:00
|
|
|
func (u *NullableUintData) UnmarshalJSON(json []byte) error {
|
2020-05-11 21:32:22 +00:00
|
|
|
s := string(json)
|
|
|
|
|
|
|
|
if s == "null" {
|
2020-11-22 17:31:52 +00:00
|
|
|
*u = *NullUint
|
2020-05-11 21:32:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
|
|
|
|
|
|
u.Val = uint(v)
|
2020-05-22 17:51:15 +00:00
|
|
|
u.Init = true
|
2020-05-11 21:32:22 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ================================ NullableInt ================================
|
|
|
|
|
|
|
|
// NullableInt is a nullable version of an integer (int).
|
2020-05-13 00:09:43 +00:00
|
|
|
type NullableInt = *NullableIntData
|
2020-05-11 21:32:22 +00:00
|
|
|
|
2020-05-13 00:09:43 +00:00
|
|
|
type NullableIntData struct {
|
2020-05-11 21:32:22 +00:00
|
|
|
Val int
|
|
|
|
Init bool
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:06:19 +00:00
|
|
|
// NullInt serializes to JSON null.
|
2020-05-13 00:09:43 +00:00
|
|
|
var NullInt = &NullableIntData{}
|
2020-05-11 21:32:22 +00:00
|
|
|
|
|
|
|
// NewInt creates a new non-null NullableInt using the value of the passed int.
|
|
|
|
func NewNullableInt(v int) NullableInt {
|
2020-05-13 00:09:43 +00:00
|
|
|
return &NullableIntData{
|
2020-05-11 21:32:22 +00:00
|
|
|
Val: v,
|
|
|
|
Init: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 00:09:43 +00:00
|
|
|
func (i NullableIntData) MarshalJSON() ([]byte, error) {
|
2020-05-11 21:32:22 +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 *NullableIntData) UnmarshalJSON(json []byte) error {
|
2020-05-11 21:32:22 +00:00
|
|
|
s := string(json)
|
|
|
|
|
|
|
|
if s == "null" {
|
2020-11-22 17:31:52 +00:00
|
|
|
*i = *NullInt
|
2020-05-11 21:32:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
|
|
|
|
|
|
i.Val = int(v)
|
2020-05-22 17:51:15 +00:00
|
|
|
i.Init = true
|
2020-05-11 21:32:22 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|