mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-23 07:40:01 +00:00
go: give enums their own scalar types
This commit is contained in:
@@ -2,14 +2,15 @@
|
||||
|
||||
package Example
|
||||
|
||||
type Any = byte
|
||||
const (
|
||||
AnyNONE = 0
|
||||
AnyMonster = 1
|
||||
AnyTestSimpleTableWithEnum = 2
|
||||
AnyMyGame_Example2_Monster = 3
|
||||
AnyNONE Any = 0
|
||||
AnyMonster Any = 1
|
||||
AnyTestSimpleTableWithEnum Any = 2
|
||||
AnyMyGame_Example2_Monster Any = 3
|
||||
)
|
||||
|
||||
var EnumNamesAny = map[int]string{
|
||||
var EnumNamesAny = map[Any]string{
|
||||
AnyNONE:"NONE",
|
||||
AnyMonster:"Monster",
|
||||
AnyTestSimpleTableWithEnum:"TestSimpleTableWithEnum",
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
package Example
|
||||
|
||||
type Color = int8
|
||||
const (
|
||||
ColorRed = 1
|
||||
ColorGreen = 2
|
||||
ColorBlue = 8
|
||||
ColorRed Color = 1
|
||||
ColorGreen Color = 2
|
||||
ColorBlue Color = 8
|
||||
)
|
||||
|
||||
var EnumNamesColor = map[int]string{
|
||||
var EnumNamesColor = map[Color]string{
|
||||
ColorRed:"Red",
|
||||
ColorGreen:"Green",
|
||||
ColorBlue:"Blue",
|
||||
|
||||
@@ -1356,31 +1356,28 @@ func CheckFinishedBytesError(fail func(string, ...interface{})) {
|
||||
|
||||
// CheckEnumNames checks that the generated enum names are correct.
|
||||
func CheckEnumNames(fail func(string, ...interface{})) {
|
||||
type testEnumNames struct {
|
||||
EnumNames map[int]string
|
||||
Expected map[int]string
|
||||
{
|
||||
|
||||
want := map[example.Any]string{
|
||||
example.AnyNONE: "NONE",
|
||||
example.AnyMonster: "Monster",
|
||||
example.AnyTestSimpleTableWithEnum: "TestSimpleTableWithEnum",
|
||||
example.AnyMyGame_Example2_Monster: "MyGame_Example2_Monster",
|
||||
}
|
||||
got := example.EnumNamesAny
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
fail("enum name is not equal")
|
||||
}
|
||||
}
|
||||
data := [...]testEnumNames{
|
||||
{example.EnumNamesAny,
|
||||
map[int]string{
|
||||
example.AnyNONE: "NONE",
|
||||
example.AnyMonster: "Monster",
|
||||
example.AnyTestSimpleTableWithEnum: "TestSimpleTableWithEnum",
|
||||
},
|
||||
},
|
||||
{example.EnumNamesColor,
|
||||
map[int]string{
|
||||
example.ColorRed: "Red",
|
||||
example.ColorGreen: "Green",
|
||||
example.ColorBlue: "Blue",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, t := range data {
|
||||
for val, name := range t.Expected {
|
||||
if name != t.EnumNames[val] {
|
||||
fail("enum name is not equal")
|
||||
}
|
||||
{
|
||||
want := map[example.Color]string{
|
||||
example.ColorRed: "Red",
|
||||
example.ColorGreen: "Green",
|
||||
example.ColorBlue: "Blue",
|
||||
}
|
||||
got := example.EnumNamesColor
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
fail("enum name is not equal")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user