mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-12 07:50:59 +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,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) + ")"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user