issuu / ocaml-protoc-plugin

ocaml-protoc-plugin
https://issuu.github.io/ocaml-protoc-plugin/
Other
48 stars 19 forks source link

Consider using common polymorphic variants for optional fields #30

Closed rauanmayemir closed 1 year ago

rauanmayemir commented 2 years ago

Currently, plugin generates types for optional fields as:

...
_field_name: [`not_set | `Field_name of field_type]
...

I wonder what is the use case for types to be set up like that and if it's not better to simplify it to something like this:

...
field_name: [`not_set | `SomeValue of field_type]
...
andersfugmann commented 2 years ago

Optional fields for proto2 syntax are treated as an alias for oneof fields with only one element. It would be possible to differentiate optional fields from oneof fields, and use a regular option type instead (which would be more idiomatic to ocaml), and hide this feature behind a flag to be passed to the compiler.

I'm no longer an active maintainer of the project, but you could always try and create a PR. However I would strongly recommend to switch to proto3 where ever possible.

rauanmayemir commented 2 years ago

I do use proto3 and it supports optional since 3.15 as a way to help with presence. (it's generally considered a synthetic oneof as opposed to real oneof with multiple options)

Thank you for the answer, I'll look into it when I get free time.

andersfugmann commented 2 years ago

I think this is related to #33

andersfugmann commented 1 year ago

Closing as #33 has been merged. I will release a new version soon