Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
0.9.2
-
None
-
Patch Available
Description
Generated Go code for enums provides String() and EnumFromString methods for enums. E.g.:
func (p TestEnum) String() string { switch p { case TestEnum_FIRST: return "TestEnum_FIRST" case TestEnum_SECOND: return "TestEnum_SECOND" case TestEnum_THIRD: return "TestEnum_THIRD" case TestEnum_FOURTH: return "TestEnum_FOURTH" } return "<UNSET>" } func TestEnumFromString(s string) (TestEnum, error) { switch s { case "TestEnum_FIRST": return TestEnum_FIRST, nil case "TestEnum_SECOND": return TestEnum_SECOND, nil case "TestEnum_THIRD": return TestEnum_THIRD, nil case "TestEnum_FOURTH": return TestEnum_FOURTH, nil } }
The current implementation uses enum name as string representation. E.g. String(TestEnum_FIRST) = "TestEnum_FIRST" which seems to be unnecessary verbose to me.
I propose to change it to use enum values from the thrift file instead. E.g.:
func (p TestEnum) String() string { switch p { case TestEnum_FIRST: return "FIRST" case TestEnum_SECOND: return "SECOND" case TestEnum_THIRD: return "THIRD" case TestEnum_FOURTH: return "FOURTH" } return "<UNSET>" } func TestEnumFromString(s string) (TestEnum, error) { switch s { case "FIRST": return TestEnum_FIRST, nil case "SECOND": return TestEnum_SECOND, nil case "THIRD": return TestEnum_THIRD, nil case "FOURTH": return TestEnum_FOURTH, nil } }
This is also consistent with generated code for other languages (e.g. Python, C++).