Support proto3 "presence" feature
ghaskins opened this issue · 2 comments
Since this issue was created, Protobuf 3.15.0 was released. Relevant changes:
- 3.15.0 added a
supported_features
field to thegoogle.protobuf.compiler.CodeGeneratorResponse
message: https://github.com/protocolbuffers/protobuf/blob/v24.4/src/google/protobuf/compiler/plugin.proto#L117-L126. Plugins can use this to report post hoc which features they supported. - https://github.com/protocolbuffers/protobuf/blob/v24.4/docs/field_presence.md was created, describing how to use field presence and noting that it's on by default in compiler 3.15.0+
- The doc linked in OP is still around, but may have changed in the last few years: https://github.com/protocolbuffers/protobuf/blob/v24.4/docs/implementing_proto3_presence.md
My attention was drawn to this issue when I was struggling to access an optional field in emitted Clojure code:
optional bool dryRun = 8;
was becoming
(= (get-in req [:grpc-params :-dryRun])
{:dryRun false})
rather than the expected
(= (get-in req [:grpc-params :dryRun]) false)
(h/t to @sundbry, who explained: This is a consequence of the behavior described in implementing_proto3_presence.md: "When a user adds an optional field to proto3, this is internally rewritten as a one-field oneof, for backward-compatibility with reflection-based algorithms" The leading -
is an accident of it being treated like oneof
.)
This also comes through in the output of buf generate
using the plugin in this repo:
% buf generate
Warning: plugin "clojure-wrapper" does not support required features.
Feature "proto3 optional" is required by 1 file(s):
my_file.proto
Among the work needed for this issue is upgrading the version of Google's plugin.proto to a more recent definition, one that includes the supported_features
field.
In the meantime, I'll code against the synthetic oneof representation currently being generated by the plugin.
@spencerwilson I took a crack at the first part: protojure/google.protobuf#9