From 3c964e10ab4f97e2a3602a8d0a8f4c402806ef89 Mon Sep 17 00:00:00 2001 From: Tiago Silva <3629062+tigrato@users.noreply.github.com> Date: Tue, 26 Nov 2019 20:25:36 +0000 Subject: [PATCH] [GO] Fix support for enums with underscores and Unions with imported members (#5600) * Fix Enum Stringer method when Enum has underscores Fixes #5481 * Fix go package importing itself when Union has imported members. --- src/idl_gen_go.cpp | 2 +- tests/MyGame/Example/Any.go | 60 ++++++++++----------- tests/MyGame/Example/AnyAmbiguousAliases.go | 60 ++++++++++----------- tests/MyGame/Example/AnyUniqueAliases.go | 60 ++++++++++----------- 4 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 7dc56df0d..fce7b1258 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -81,13 +81,13 @@ class GoGenerator : public BaseGenerator { tracked_imported_namespaces_.clear(); needs_imports = false; std::string enumcode; + GenEnum(**it, &enumcode); if ((*it)->is_union && parser_.opts.generate_object_based_api) { GenNativeUnion(**it, &enumcode); GenNativeUnionPack(**it, &enumcode); GenNativeUnionUnPack(**it, &enumcode); needs_imports = true; } - GenEnum(**it, &enumcode); if (parser_.opts.one_file) { one_file_code += enumcode; } else { diff --git a/tests/MyGame/Example/Any.go b/tests/MyGame/Example/Any.go index 7b9ffb918..c9a0a166d 100644 --- a/tests/MyGame/Example/Any.go +++ b/tests/MyGame/Example/Any.go @@ -10,6 +10,36 @@ import ( MyGame__Example2 "MyGame/Example2" ) +type Any byte + +const ( + 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", +} + +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) + ")" +} + type AnyT struct { Type Any Value interface{} @@ -44,33 +74,3 @@ func AnyUnPack(t Any, table flatbuffers.Table) *AnyT { } return nil } - -type Any byte - -const ( - 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", -} - -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 cc4adb8a0..08cc59625 100644 --- a/tests/MyGame/Example/AnyAmbiguousAliases.go +++ b/tests/MyGame/Example/AnyAmbiguousAliases.go @@ -8,6 +8,36 @@ import ( flatbuffers "github.com/google/flatbuffers/go" ) +type AnyAmbiguousAliases byte + +const ( + AnyAmbiguousAliasesNONE AnyAmbiguousAliases = 0 + AnyAmbiguousAliasesM1 AnyAmbiguousAliases = 1 + AnyAmbiguousAliasesM2 AnyAmbiguousAliases = 2 + AnyAmbiguousAliasesM3 AnyAmbiguousAliases = 3 +) + +var EnumNamesAnyAmbiguousAliases = map[AnyAmbiguousAliases]string{ + 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) + ")" +} + type AnyAmbiguousAliasesT struct { Type AnyAmbiguousAliases Value interface{} @@ -42,33 +72,3 @@ func AnyAmbiguousAliasesUnPack(t AnyAmbiguousAliases, table flatbuffers.Table) * } return nil } - -type AnyAmbiguousAliases byte - -const ( - AnyAmbiguousAliasesNONE AnyAmbiguousAliases = 0 - AnyAmbiguousAliasesM1 AnyAmbiguousAliases = 1 - AnyAmbiguousAliasesM2 AnyAmbiguousAliases = 2 - AnyAmbiguousAliasesM3 AnyAmbiguousAliases = 3 -) - -var EnumNamesAnyAmbiguousAliases = map[AnyAmbiguousAliases]string{ - 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 56b516380..008cada48 100644 --- a/tests/MyGame/Example/AnyUniqueAliases.go +++ b/tests/MyGame/Example/AnyUniqueAliases.go @@ -10,6 +10,36 @@ import ( MyGame__Example2 "MyGame/Example2" ) +type AnyUniqueAliases byte + +const ( + AnyUniqueAliasesNONE AnyUniqueAliases = 0 + AnyUniqueAliasesM AnyUniqueAliases = 1 + AnyUniqueAliasesTS AnyUniqueAliases = 2 + AnyUniqueAliasesM2 AnyUniqueAliases = 3 +) + +var EnumNamesAnyUniqueAliases = map[AnyUniqueAliases]string{ + AnyUniqueAliasesNONE: "NONE", + AnyUniqueAliasesM: "M", + AnyUniqueAliasesTS: "TS", + AnyUniqueAliasesM2: "M2", +} + +var EnumValuesAnyUniqueAliases = map[string]AnyUniqueAliases{ + "NONE": AnyUniqueAliasesNONE, + "M": AnyUniqueAliasesM, + "TS": AnyUniqueAliasesTS, + "M2": AnyUniqueAliasesM2, +} + +func (v AnyUniqueAliases) String() string { + if s, ok := EnumNamesAnyUniqueAliases[v]; ok { + return s + } + return "AnyUniqueAliases(" + strconv.FormatInt(int64(v), 10) + ")" +} + type AnyUniqueAliasesT struct { Type AnyUniqueAliases Value interface{} @@ -44,33 +74,3 @@ func AnyUniqueAliasesUnPack(t AnyUniqueAliases, table flatbuffers.Table) *AnyUni } return nil } - -type AnyUniqueAliases byte - -const ( - AnyUniqueAliasesNONE AnyUniqueAliases = 0 - AnyUniqueAliasesM AnyUniqueAliases = 1 - AnyUniqueAliasesTS AnyUniqueAliases = 2 - AnyUniqueAliasesM2 AnyUniqueAliases = 3 -) - -var EnumNamesAnyUniqueAliases = map[AnyUniqueAliases]string{ - AnyUniqueAliasesNONE: "NONE", - AnyUniqueAliasesM: "M", - AnyUniqueAliasesTS: "TS", - AnyUniqueAliasesM2: "M2", -} - -var EnumValuesAnyUniqueAliases = map[string]AnyUniqueAliases{ - "NONE": AnyUniqueAliasesNONE, - "M": AnyUniqueAliasesM, - "TS": AnyUniqueAliasesTS, - "M2": AnyUniqueAliasesM2, -} - -func (v AnyUniqueAliases) String() string { - if s, ok := EnumNamesAnyUniqueAliases[v]; ok { - return s - } - return "AnyUniqueAliases(" + strconv.FormatInt(int64(v), 10) + ")" -}