Merge pull request #4843 from rw/2018-07--explicit-enumish-types
go: give enums their own scalar types
This commit is contained in:
commit
8b39a0ee53
|
@ -86,6 +86,18 @@ static void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
|
||||||
code += "\n}\n\n";
|
code += "\n}\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Construct the name of the type alias for this enum.
|
||||||
|
std::string GetEnumTypeName(const EnumDef &enum_def) {
|
||||||
|
return GoIdentity(enum_def.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a type for the enum values.
|
||||||
|
static void GenEnumType(const EnumDef &enum_def, std::string *code_ptr) {
|
||||||
|
std::string &code = *code_ptr;
|
||||||
|
code += "type " + GetEnumTypeName(enum_def) + " = ";
|
||||||
|
code += GenTypeBasic(enum_def.underlying_type) + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
// Begin enum code with a class declaration.
|
// Begin enum code with a class declaration.
|
||||||
static void BeginEnum(std::string *code_ptr) {
|
static void BeginEnum(std::string *code_ptr) {
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
|
@ -99,6 +111,8 @@ static void EnumMember(const EnumDef &enum_def, const EnumVal ev,
|
||||||
code += "\t";
|
code += "\t";
|
||||||
code += enum_def.name;
|
code += enum_def.name;
|
||||||
code += ev.name;
|
code += ev.name;
|
||||||
|
code += " ";
|
||||||
|
code += GetEnumTypeName(enum_def);
|
||||||
code += " = ";
|
code += " = ";
|
||||||
code += NumToString(ev.value) + "\n";
|
code += NumToString(ev.value) + "\n";
|
||||||
}
|
}
|
||||||
|
@ -114,7 +128,7 @@ static void BeginEnumNames(const EnumDef &enum_def, std::string *code_ptr) {
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
code += "var EnumNames";
|
code += "var EnumNames";
|
||||||
code += enum_def.name;
|
code += enum_def.name;
|
||||||
code += " = map[int]string{\n";
|
code += " = map[" + GetEnumTypeName(enum_def) + "]string{\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// A single enum name member.
|
// A single enum name member.
|
||||||
|
@ -636,6 +650,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
|
||||||
if (enum_def.generated) return;
|
if (enum_def.generated) return;
|
||||||
|
|
||||||
GenComment(enum_def.doc_comment, code_ptr, nullptr);
|
GenComment(enum_def.doc_comment, code_ptr, nullptr);
|
||||||
|
GenEnumType(enum_def, code_ptr);
|
||||||
BeginEnum(code_ptr);
|
BeginEnum(code_ptr);
|
||||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||||
++it) {
|
++it) {
|
||||||
|
|
|
@ -2,14 +2,15 @@
|
||||||
|
|
||||||
package Example
|
package Example
|
||||||
|
|
||||||
|
type Any = byte
|
||||||
const (
|
const (
|
||||||
AnyNONE = 0
|
AnyNONE Any = 0
|
||||||
AnyMonster = 1
|
AnyMonster Any = 1
|
||||||
AnyTestSimpleTableWithEnum = 2
|
AnyTestSimpleTableWithEnum Any = 2
|
||||||
AnyMyGame_Example2_Monster = 3
|
AnyMyGame_Example2_Monster Any = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
var EnumNamesAny = map[int]string{
|
var EnumNamesAny = map[Any]string{
|
||||||
AnyNONE:"NONE",
|
AnyNONE:"NONE",
|
||||||
AnyMonster:"Monster",
|
AnyMonster:"Monster",
|
||||||
AnyTestSimpleTableWithEnum:"TestSimpleTableWithEnum",
|
AnyTestSimpleTableWithEnum:"TestSimpleTableWithEnum",
|
||||||
|
|
|
@ -2,13 +2,14 @@
|
||||||
|
|
||||||
package Example
|
package Example
|
||||||
|
|
||||||
|
type Color = int8
|
||||||
const (
|
const (
|
||||||
ColorRed = 1
|
ColorRed Color = 1
|
||||||
ColorGreen = 2
|
ColorGreen Color = 2
|
||||||
ColorBlue = 8
|
ColorBlue Color = 8
|
||||||
)
|
)
|
||||||
|
|
||||||
var EnumNamesColor = map[int]string{
|
var EnumNamesColor = map[Color]string{
|
||||||
ColorRed:"Red",
|
ColorRed:"Red",
|
||||||
ColorGreen:"Green",
|
ColorGreen:"Green",
|
||||||
ColorBlue:"Blue",
|
ColorBlue:"Blue",
|
||||||
|
|
|
@ -1356,31 +1356,28 @@ func CheckFinishedBytesError(fail func(string, ...interface{})) {
|
||||||
|
|
||||||
// CheckEnumNames checks that the generated enum names are correct.
|
// CheckEnumNames checks that the generated enum names are correct.
|
||||||
func CheckEnumNames(fail func(string, ...interface{})) {
|
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,
|
want := map[example.Color]string{
|
||||||
map[int]string{
|
example.ColorRed: "Red",
|
||||||
example.AnyNONE: "NONE",
|
example.ColorGreen: "Green",
|
||||||
example.AnyMonster: "Monster",
|
example.ColorBlue: "Blue",
|
||||||
example.AnyTestSimpleTableWithEnum: "TestSimpleTableWithEnum",
|
}
|
||||||
},
|
got := example.EnumNamesColor
|
||||||
},
|
if !reflect.DeepEqual(got, want) {
|
||||||
{example.EnumNamesColor,
|
fail("enum name is not equal")
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue