awakesecurity / proto3-suite

Haskell Protobuf Implementation
https://hackage.haskell.org/package/proto3-suite
Other
81 stars 56 forks source link

Generate Enum instances for enumerations #179

Open riz0id opened 2 years ago

riz0id commented 2 years ago

Protocol buffer enumerations are not generated with Enum instances. They are generated with instances to Bounded and Ord, so there shouldn't be anything preventing Enum from also being provided by default.

Gabriella439 commented 2 years ago

This is easy to add, although the mapping to integers will not necessarily match the field numbers. Note that we currently provide a ProtoEnum class which allows gaps in the sequence and maps fields to their field numbers:

https://hackage.haskell.org/package/proto3-wire-1.2.1/docs/Proto3-Wire-Class.html#t:ProtoEnum

However, I'm guessing that if you're requesting this that you have a use case where you just need to map them onto a contiguous range of numbers?

riz0id commented 2 years ago

Right, being able to enumerate the set of fields for an enumeration would be convenient, e.g.

 [minBound @MyEnumeration .. maxBound @MyEnumeration]
riz0id commented 2 years ago

Although, it seems like the Ord instance respects field number, and having Enum deviate from that would be awkward?

j6carey commented 2 years ago

Would the enumerate method of type class Proto3.Suite.Class.Finite do what is needed? Though that produces the codes, not the typed values.

Alternatively, would it make sense to extend type class ProtoEnum with an enumeration method, perhaps named in such a way as to indicate that it is only enumerating known values, not new values that might arise from future versions of the enumeration?