Open GeertJohan opened 11 years ago
I'm working on a more full featured example generated code now..
I have uploaded some beginnings at the thrift4go organisation. https://github.com/thrift4go/enum-tryout
The most interesting file: https://github.com/thrift4go/enum-tryout/blob/master/gen-go/enums/foo.go
Looks good, let's change unexpected to be something extremely unlikely:
math.MinInt64 / 2
Make Unexpected
a longer name though to ensure someone's enum name doesn't match:
InvalidOrUnexpectedEnumValue
math.MinInt64 / 2
Why /2 ?
Although unlikely, there is still a change someone might pick this value for his/her enum value.
Should the generator then error on that? Or maybe check if the value is being used by the enum def in the .thrift file and if so, increment until a unique (non-used) value has been found?
InvalidOrUnexpectedEnumValue
Good catch, will do.
Actually, Thrift defines all enums to be an i32 value. So math.MinInt64 is fine.
Hmm... that would work yes.. Although we're kinda burning memory then... I'm going to think about another solution for this, for now will change it to MinInt64.
New thread as reply on https://github.com/pomack/thrift4go/pull/56
An other solution to this would be to create a package for each EnumType. However, I don't really like that solution as it will create a lot of packages and package references.
An third solution might be to define and instantiate a struct containing al EnumNames as fields.
Thrift code:
Generated Go code:
Usage example:
You could also add methods on the StatusEnum:
Using the FromInt64 method might lead to the statusEnumType becoming unexported. Making it imposible to create a invalid StatusEnum value.
I think that this solution might be cleaner and safer, but will probably cost a lot more performance.