diff options
author | David Reiss <davidn@gmail.com> | 2019-05-17 12:41:39 -0700 |
---|---|---|
committer | Robert <rw@users.noreply.github.com> | 2019-05-17 12:41:39 -0700 |
commit | 718ddea558b3efb0544fc56bea07ed3da8884125 (patch) | |
tree | c0c0d47ad645c7dea72da3799205e7954ff06053 /tests/MyGame | |
parent | 8d86b5347fbeba85f11b7bf479334f6700feb455 (diff) | |
download | flatbuffers-718ddea558b3efb0544fc56bea07ed3da8884125.tar.gz flatbuffers-718ddea558b3efb0544fc56bea07ed3da8884125.tar.bz2 flatbuffers-718ddea558b3efb0544fc56bea07ed3da8884125.zip |
[Go] Make enums into real types, add String() (#5235)
* [Go] Make enums into real types, add String()
This changes the generated code for enums: instead of type aliases,
they're now distinct types, allowing for better type-checking. Some
client code may have to be changed to add casts.
Enum types now have a String() method, so they implement fmt.Stringer.
An EnumValues map is now generated, in addition to the existing
EnumNames map, to easily map strings to values.
Generated enum files are now gofmt-clean.
Fixes #5207
* use example.ColorGreen explicitly
* use valid enum value in mutation test, add new test for "invalid" enum
* add length check and comment
Diffstat (limited to 'tests/MyGame')
-rw-r--r-- | tests/MyGame/Example/Any.go | 30 | ||||
-rw-r--r-- | tests/MyGame/Example/AnyAmbiguousAliases.go | 32 | ||||
-rw-r--r-- | tests/MyGame/Example/AnyUniqueAliases.go | 32 | ||||
-rw-r--r-- | tests/MyGame/Example/Color.go | 27 | ||||
-rw-r--r-- | tests/MyGame/Example/Monster.go | 32 | ||||
-rw-r--r-- | tests/MyGame/Example/TestSimpleTableWithEnum.go | 4 | ||||
-rw-r--r-- | tests/MyGame/Example/Vec3.go | 4 |
7 files changed, 112 insertions, 49 deletions
diff --git a/tests/MyGame/Example/Any.go b/tests/MyGame/Example/Any.go index 39b89a79..8d9067e1 100644 --- a/tests/MyGame/Example/Any.go +++ b/tests/MyGame/Example/Any.go @@ -2,18 +2,34 @@ package Example -type Any = byte +import "strconv" + +type Any byte + const ( - AnyNONE Any = 0 - AnyMonster Any = 1 + AnyNONE Any = 0 + AnyMonster Any = 1 AnyTestSimpleTableWithEnum Any = 2 AnyMyGame_Example2_Monster Any = 3 ) var EnumNamesAny = map[Any]string{ - AnyNONE:"NONE", - AnyMonster:"Monster", - AnyTestSimpleTableWithEnum:"TestSimpleTableWithEnum", - AnyMyGame_Example2_Monster:"MyGame_Example2_Monster", + AnyNONE: "NONE", + AnyMonster: "Monster", + AnyTestSimpleTableWithEnum: "TestSimpleTableWithEnum", + AnyMyGame_Example2_Monster: "MyGame_Example2_Monster", } +var EnumValuesAny = map[string]Any{ + "NONE": AnyNONE, + "Monster": AnyMonster, + "TestSimpleTableWithEnum": AnyTestSimpleTableWithEnum, + "MyGame_Example2_Monster": AnyMyGame_Example2_Monster, +} + +func (v Any) String() string { + if s, ok := EnumNamesAny[v]; ok { + return s + } + return "Any(" + strconv.FormatInt(int64(v), 10) + ")" +} diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.go b/tests/MyGame/Example/AnyAmbiguousAliases.go index b5eacde4..b9c3793a 100644 --- a/tests/MyGame/Example/AnyAmbiguousAliases.go +++ b/tests/MyGame/Example/AnyAmbiguousAliases.go @@ -2,18 +2,34 @@ package Example -type AnyAmbiguousAliases = byte +import "strconv" + +type AnyAmbiguousAliases byte + const ( AnyAmbiguousAliasesNONE AnyAmbiguousAliases = 0 - AnyAmbiguousAliasesM1 AnyAmbiguousAliases = 1 - AnyAmbiguousAliasesM2 AnyAmbiguousAliases = 2 - AnyAmbiguousAliasesM3 AnyAmbiguousAliases = 3 + AnyAmbiguousAliasesM1 AnyAmbiguousAliases = 1 + AnyAmbiguousAliasesM2 AnyAmbiguousAliases = 2 + AnyAmbiguousAliasesM3 AnyAmbiguousAliases = 3 ) var EnumNamesAnyAmbiguousAliases = map[AnyAmbiguousAliases]string{ - AnyAmbiguousAliasesNONE:"NONE", - AnyAmbiguousAliasesM1:"M1", - AnyAmbiguousAliasesM2:"M2", - AnyAmbiguousAliasesM3:"M3", + AnyAmbiguousAliasesNONE: "NONE", + AnyAmbiguousAliasesM1: "M1", + AnyAmbiguousAliasesM2: "M2", + AnyAmbiguousAliasesM3: "M3", } +var EnumValuesAnyAmbiguousAliases = map[string]AnyAmbiguousAliases{ + "NONE": AnyAmbiguousAliasesNONE, + "M1": AnyAmbiguousAliasesM1, + "M2": AnyAmbiguousAliasesM2, + "M3": AnyAmbiguousAliasesM3, +} + +func (v AnyAmbiguousAliases) String() string { + if s, ok := EnumNamesAnyAmbiguousAliases[v]; ok { + return s + } + return "AnyAmbiguousAliases(" + strconv.FormatInt(int64(v), 10) + ")" +} diff --git a/tests/MyGame/Example/AnyUniqueAliases.go b/tests/MyGame/Example/AnyUniqueAliases.go index 14a2694a..36e4d8e2 100644 --- a/tests/MyGame/Example/AnyUniqueAliases.go +++ b/tests/MyGame/Example/AnyUniqueAliases.go @@ -2,18 +2,34 @@ package Example -type AnyUniqueAliases = byte +import "strconv" + +type AnyUniqueAliases byte + const ( AnyUniqueAliasesNONE AnyUniqueAliases = 0 - AnyUniqueAliasesM AnyUniqueAliases = 1 - AnyUniqueAliasesT AnyUniqueAliases = 2 - AnyUniqueAliasesM2 AnyUniqueAliases = 3 + AnyUniqueAliasesM AnyUniqueAliases = 1 + AnyUniqueAliasesT AnyUniqueAliases = 2 + AnyUniqueAliasesM2 AnyUniqueAliases = 3 ) var EnumNamesAnyUniqueAliases = map[AnyUniqueAliases]string{ - AnyUniqueAliasesNONE:"NONE", - AnyUniqueAliasesM:"M", - AnyUniqueAliasesT:"T", - AnyUniqueAliasesM2:"M2", + AnyUniqueAliasesNONE: "NONE", + AnyUniqueAliasesM: "M", + AnyUniqueAliasesT: "T", + AnyUniqueAliasesM2: "M2", } +var EnumValuesAnyUniqueAliases = map[string]AnyUniqueAliases{ + "NONE": AnyUniqueAliasesNONE, + "M": AnyUniqueAliasesM, + "T": AnyUniqueAliasesT, + "M2": AnyUniqueAliasesM2, +} + +func (v AnyUniqueAliases) String() string { + if s, ok := EnumNamesAnyUniqueAliases[v]; ok { + return s + } + return "AnyUniqueAliases(" + strconv.FormatInt(int64(v), 10) + ")" +} diff --git a/tests/MyGame/Example/Color.go b/tests/MyGame/Example/Color.go index 67718b28..52e28ebc 100644 --- a/tests/MyGame/Example/Color.go +++ b/tests/MyGame/Example/Color.go @@ -2,16 +2,31 @@ package Example -type Color = byte +import "strconv" + +type Color byte + const ( - ColorRed Color = 1 + ColorRed Color = 1 ColorGreen Color = 2 - ColorBlue Color = 8 + ColorBlue Color = 8 ) var EnumNamesColor = map[Color]string{ - ColorRed:"Red", - ColorGreen:"Green", - ColorBlue:"Blue", + ColorRed: "Red", + ColorGreen: "Green", + ColorBlue: "Blue", } +var EnumValuesColor = map[string]Color{ + "Red": ColorRed, + "Green": ColorGreen, + "Blue": ColorBlue, +} + +func (v Color) String() string { + if s, ok := EnumNamesColor[v]; ok { + return s + } + return "Color(" + strconv.FormatInt(int64(v), 10) + ")" +} diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 1f146b0f..ab32ccfd 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -111,25 +111,25 @@ func (rcv *Monster) MutateInventory(j int, n byte) bool { func (rcv *Monster) Color() Color { o := flatbuffers.UOffsetT(rcv._tab.Offset(16)) if o != 0 { - return rcv._tab.GetByte(o + rcv._tab.Pos) + return Color(rcv._tab.GetByte(o + rcv._tab.Pos)) } return 8 } func (rcv *Monster) MutateColor(n Color) bool { - return rcv._tab.MutateByteSlot(16, n) + return rcv._tab.MutateByteSlot(16, byte(n)) } -func (rcv *Monster) TestType() byte { +func (rcv *Monster) TestType() Any { o := flatbuffers.UOffsetT(rcv._tab.Offset(18)) if o != 0 { - return rcv._tab.GetByte(o + rcv._tab.Pos) + return Any(rcv._tab.GetByte(o + rcv._tab.Pos)) } return 0 } -func (rcv *Monster) MutateTestType(n byte) bool { - return rcv._tab.MutateByteSlot(18, n) +func (rcv *Monster) MutateTestType(n Any) bool { + return rcv._tab.MutateByteSlot(18, byte(n)) } func (rcv *Monster) Test(obj *flatbuffers.Table) bool { @@ -739,16 +739,16 @@ func (rcv *Monster) MutateVectorOfNonOwningReferences(j int, n uint64) bool { return false } -func (rcv *Monster) AnyUniqueType() byte { +func (rcv *Monster) AnyUniqueType() AnyUniqueAliases { o := flatbuffers.UOffsetT(rcv._tab.Offset(90)) if o != 0 { - return rcv._tab.GetByte(o + rcv._tab.Pos) + return AnyUniqueAliases(rcv._tab.GetByte(o + rcv._tab.Pos)) } return 0 } -func (rcv *Monster) MutateAnyUniqueType(n byte) bool { - return rcv._tab.MutateByteSlot(90, n) +func (rcv *Monster) MutateAnyUniqueType(n AnyUniqueAliases) bool { + return rcv._tab.MutateByteSlot(90, byte(n)) } func (rcv *Monster) AnyUnique(obj *flatbuffers.Table) bool { @@ -760,16 +760,16 @@ func (rcv *Monster) AnyUnique(obj *flatbuffers.Table) bool { return false } -func (rcv *Monster) AnyAmbiguousType() byte { +func (rcv *Monster) AnyAmbiguousType() AnyAmbiguousAliases { o := flatbuffers.UOffsetT(rcv._tab.Offset(94)) if o != 0 { - return rcv._tab.GetByte(o + rcv._tab.Pos) + return AnyAmbiguousAliases(rcv._tab.GetByte(o + rcv._tab.Pos)) } return 0 } -func (rcv *Monster) MutateAnyAmbiguousType(n byte) bool { - return rcv._tab.MutateByteSlot(94, n) +func (rcv *Monster) MutateAnyAmbiguousType(n AnyAmbiguousAliases) bool { + return rcv._tab.MutateByteSlot(94, byte(n)) } func (rcv *Monster) AnyAmbiguous(obj *flatbuffers.Table) bool { @@ -785,7 +785,7 @@ func (rcv *Monster) VectorOfEnums(j int) Color { o := flatbuffers.UOffsetT(rcv._tab.Offset(98)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) + return Color(rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1))) } return 0 } @@ -810,7 +810,7 @@ func (rcv *Monster) MutateVectorOfEnums(j int, n Color) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(98)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.MutateByte(a+flatbuffers.UOffsetT(j*1), n) + return rcv._tab.MutateByte(a+flatbuffers.UOffsetT(j*1), byte(n)) } return false } diff --git a/tests/MyGame/Example/TestSimpleTableWithEnum.go b/tests/MyGame/Example/TestSimpleTableWithEnum.go index 638ed4db..78c4d713 100644 --- a/tests/MyGame/Example/TestSimpleTableWithEnum.go +++ b/tests/MyGame/Example/TestSimpleTableWithEnum.go @@ -29,13 +29,13 @@ func (rcv *TestSimpleTableWithEnum) Table() flatbuffers.Table { func (rcv *TestSimpleTableWithEnum) Color() Color { o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) if o != 0 { - return rcv._tab.GetByte(o + rcv._tab.Pos) + return Color(rcv._tab.GetByte(o + rcv._tab.Pos)) } return 2 } func (rcv *TestSimpleTableWithEnum) MutateColor(n Color) bool { - return rcv._tab.MutateByteSlot(4, n) + return rcv._tab.MutateByteSlot(4, byte(n)) } func TestSimpleTableWithEnumStart(builder *flatbuffers.Builder) { diff --git a/tests/MyGame/Example/Vec3.go b/tests/MyGame/Example/Vec3.go index 81b5c396..a9feaae0 100644 --- a/tests/MyGame/Example/Vec3.go +++ b/tests/MyGame/Example/Vec3.go @@ -48,10 +48,10 @@ func (rcv *Vec3) MutateTest1(n float64) bool { } func (rcv *Vec3) Test2() Color { - return rcv._tab.GetByte(rcv._tab.Pos + flatbuffers.UOffsetT(24)) + return Color(rcv._tab.GetByte(rcv._tab.Pos + flatbuffers.UOffsetT(24))) } func (rcv *Vec3) MutateTest2(n Color) bool { - return rcv._tab.MutateByte(rcv._tab.Pos+flatbuffers.UOffsetT(24), n) + return rcv._tab.MutateByte(rcv._tab.Pos+flatbuffers.UOffsetT(24), byte(n)) } func (rcv *Vec3) Test3(obj *Test) *Test { |