mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-08 14:15:17 +00:00
[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
This commit is contained in:
@@ -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) + ")"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user