proto3-suite icon indicating copy to clipboard operation
proto3-suite copied to clipboard

Generate Enum instances for enumerations

Open riz0id opened this issue 3 years ago • 4 comments

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.

riz0id avatar Feb 07 '22 20:02 riz0id

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?

Gabriella439 avatar Feb 08 '22 18:02 Gabriella439

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

 [minBound @MyEnumeration .. maxBound @MyEnumeration]

riz0id avatar Feb 08 '22 19:02 riz0id

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

riz0id avatar Feb 08 '22 19:02 riz0id

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?

j6carey avatar Feb 08 '22 20:02 j6carey