Discord: add Enum type
This commit is contained in:
parent
795182df16
commit
e522aaef9b
|
@ -0,0 +1,34 @@
|
|||
package option
|
||||
|
||||
import "strconv"
|
||||
|
||||
// EnumNull is the value used to represent JSON null.
|
||||
// It should never be used as a value, as it won't get serialized as such.
|
||||
const EnumNull = -1
|
||||
|
||||
// Enum is a nullable version of a uint8.
|
||||
// Enum values should only consist of positive values, as negative values are reserved for internal constants, such as
|
||||
// EnumNull.
|
||||
// This also mean that only 7 of the 8 Bit will be available for storage.
|
||||
type Enum int8
|
||||
|
||||
// Int8ToJSON converts the passed Enum to a byte slice with it's JSON representation.
|
||||
func EnumToJSON(i Enum) []byte {
|
||||
if i == EnumNull {
|
||||
return []byte("null")
|
||||
} else {
|
||||
return []byte(strconv.Itoa(int(i)))
|
||||
}
|
||||
}
|
||||
|
||||
// Int8FromJSON decodes the Enum stored as JSON src the passed byte slice.
|
||||
func EnumFromJSON(b []byte) (Enum, error) {
|
||||
s := string(b)
|
||||
|
||||
if s == "null" {
|
||||
return EnumNull, nil
|
||||
} else {
|
||||
i, err := strconv.ParseUint(s, 10, 7)
|
||||
return Enum(i), err
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package option
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInt8ToJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
src Enum
|
||||
expect []byte
|
||||
}{
|
||||
{
|
||||
name: "null",
|
||||
src: EnumNull,
|
||||
expect: []byte("null"),
|
||||
},
|
||||
{
|
||||
name: "value",
|
||||
src: 12,
|
||||
expect: []byte("12"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range testCases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
actual := EnumToJSON(c.src)
|
||||
|
||||
if !reflect.DeepEqual(actual, c.expect) {
|
||||
t.Errorf("expected nullable.Int8ToJSON to return: %+v, but got: %+v", c.expect, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt8FromJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
src []byte
|
||||
expect Enum
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
name: "null",
|
||||
src: []byte("null"),
|
||||
expect: EnumNull,
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "value",
|
||||
src: []byte("12"),
|
||||
expect: 12,
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "invalid input",
|
||||
src: []byte("NaN"),
|
||||
expect: 0,
|
||||
err: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range testCases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
actual, err := EnumFromJSON(c.src)
|
||||
|
||||
if c.err {
|
||||
if err == nil {
|
||||
t.Error("expected nullable.Int8FromJSON to return an error, but it did not")
|
||||
}
|
||||
} else {
|
||||
if !reflect.DeepEqual(actual, c.expect) {
|
||||
t.Errorf("expected nullable.Int8FromJSON to return: %+v, but got: %+v", c.expect, actual)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("nullable.Int8FromJSON returned an error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue