Skip to content

Commit 9eb21c3

Browse files
Implement support for proto3 optional (#26)
proto3 optional requires opting into the experimental feature in our code generator. proto3 optional is implemented as a one-off synthetic field so we must treat those as normal objects in our codegenerator. You can find the instructions on how to handle those in the upstream protobuf documentation: https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md The PR adds tests for the logic for both binary and JSON decoding. We are missing reflection support but that seemed reasonable for now.
1 parent eaf0ef0 commit 9eb21c3

29 files changed

Lines changed: 1343 additions & 243 deletions

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pb.bin -diff
2+
*.pb.bin.gz -diff

BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ hh_test(
8989
hh_args = "lib_test/test.php",
9090
)
9191

92-
INTEGRATION_PHP = glob(["test/**/*.php"])
92+
INTEGRATION_PHP = glob(["test/**/*.php", "test/**/*.pb.json"])
9393

9494
ALL_PHP += INTEGRATION_PHP
9595

gen.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ for SRC in $PBS
3838
do
3939
echo source: $SRC
4040
ARGS="-I external/com_google_protobuf/src -I ./"
41-
$PROTOC $ARGS --plugin=$GENHACK --hack_out="plugin=grpc,allow_proto2_dangerous:$TMP" $SRC
41+
$PROTOC $ARGS --plugin=$GENHACK --hack_out="plugin=grpc,allow_proto2_dangerous:$TMP" --experimental_allow_proto3_optional $SRC
4242
echo
4343
done
4444

4545
ARGS="-I external/com_google_protobuf/src -I ./"
4646
$PROTOC $ARGS --encode=foo.bar.example1 ./test/example1.proto < ./test/example1.pb.txt > $TMP/test/example1.pb.bin
47+
$PROTOC $ARGS --encode=baz.optional_proto3 ./test/optional_proto3.proto < ./test/empty_optional_proto3.pb.txt > $TMP/test/empty_optional_proto3.pb.bin
48+
$PROTOC $ARGS --encode=baz.optional_proto3 ./test/optional_proto3.proto < ./test/default_optional_proto3.pb.txt > $TMP/test/default_optional_proto3.pb.bin
49+
$PROTOC $ARGS --encode=baz.optional_proto3 ./test/optional_proto3.proto < ./test/custom_optional_proto3.pb.txt > $TMP/test/custom_optional_proto3.pb.bin
4750

4851
if [ $# -gt 0 ]; then
4952
# Comparison mode; see if there are diffs, if none, exit 0.

generated/external/com_google_protobuf/conformance/conformance_proto.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ public function MergeFrom(\Protobuf\Internal\Decoder $d): void {
321321
$this->test_category = \conformance\TestCategory::FromInt($d->readVarint());
322322
break;
323323
case 6:
324-
if ($this->jspb_encoding_options == null) $this->jspb_encoding_options = new \conformance\JspbEncodingConfig();
324+
if ($this->jspb_encoding_options is null) {
325+
$this->jspb_encoding_options = new \conformance\JspbEncodingConfig();
326+
}
325327
$this->jspb_encoding_options->MergeFrom($d->readDecoder());
326328
break;
327329
case 7:
@@ -397,8 +399,10 @@ public function MergeJsonFrom(mixed $m): void {
397399
$this->test_category = \conformance\TestCategory::FromMixed($v);
398400
break;
399401
case 'jspb_encoding_options': case 'jspbEncodingOptions':
400-
if ($v === null) break;
401-
if ($this->jspb_encoding_options == null) $this->jspb_encoding_options = new \conformance\JspbEncodingConfig();
402+
if ($v is null) break;
403+
if ($this->jspb_encoding_options is null) {
404+
$this->jspb_encoding_options = new \conformance\JspbEncodingConfig();
405+
}
402406
$this->jspb_encoding_options->MergeJsonFrom($v);
403407
break;
404408
case 'jspb_payload': case 'jspbPayload':
@@ -424,7 +428,7 @@ public function CopyFrom(\Protobuf\Message $o): \Errors\Error {
424428
$this->message_type = $o->message_type;
425429
$this->test_category = $o->test_category;
426430
$tmp = $o->jspb_encoding_options;
427-
if ($tmp !== null) {
431+
if ($tmp is nonnull) {
428432
$nv = new \conformance\JspbEncodingConfig();
429433
$nv->CopyFrom($tmp);
430434
$this->jspb_encoding_options = $nv;

generated/google/protobuf/api_proto.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public function MergeFrom(\Protobuf\Internal\Decoder $d): void {
6767
$this->version = $d->readString();
6868
break;
6969
case 5:
70-
if ($this->source_context == null) $this->source_context = new \google\protobuf\SourceContext();
70+
if ($this->source_context is null) {
71+
$this->source_context = new \google\protobuf\SourceContext();
72+
}
7173
$this->source_context->MergeFrom($d->readDecoder());
7274
break;
7375
case 6:
@@ -158,8 +160,10 @@ public function MergeJsonFrom(mixed $m): void {
158160
$this->version = \Protobuf\Internal\JsonDecoder::readString($v);
159161
break;
160162
case 'source_context': case 'sourceContext':
161-
if ($v === null) break;
162-
if ($this->source_context == null) $this->source_context = new \google\protobuf\SourceContext();
163+
if ($v is null) break;
164+
if ($this->source_context is null) {
165+
$this->source_context = new \google\protobuf\SourceContext();
166+
}
163167
$this->source_context->MergeJsonFrom($v);
164168
break;
165169
case 'mixins':
@@ -195,7 +199,7 @@ public function CopyFrom(\Protobuf\Message $o): \Errors\Error {
195199
}
196200
$this->version = $o->version;
197201
$tmp = $o->source_context;
198-
if ($tmp !== null) {
202+
if ($tmp is nonnull) {
199203
$nv = new \google\protobuf\SourceContext();
200204
$nv->CopyFrom($tmp);
201205
$this->source_context = $nv;

generated/google/protobuf/compiler/plugin_proto.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ public function MergeFrom(\Protobuf\Internal\Decoder $d): void {
167167
$this->parameter = $d->readString();
168168
break;
169169
case 3:
170-
if ($this->compiler_version == null) $this->compiler_version = new \google\protobuf\compiler\Version();
170+
if ($this->compiler_version is null) {
171+
$this->compiler_version = new \google\protobuf\compiler\Version();
172+
}
171173
$this->compiler_version->MergeFrom($d->readDecoder());
172174
break;
173175
case 15:
@@ -226,8 +228,10 @@ public function MergeJsonFrom(mixed $m): void {
226228
$this->parameter = \Protobuf\Internal\JsonDecoder::readString($v);
227229
break;
228230
case 'compiler_version': case 'compilerVersion':
229-
if ($v === null) break;
230-
if ($this->compiler_version == null) $this->compiler_version = new \google\protobuf\compiler\Version();
231+
if ($v is null) break;
232+
if ($this->compiler_version is null) {
233+
$this->compiler_version = new \google\protobuf\compiler\Version();
234+
}
231235
$this->compiler_version->MergeJsonFrom($v);
232236
break;
233237
case 'proto_file': case 'protoFile':
@@ -250,7 +254,7 @@ public function CopyFrom(\Protobuf\Message $o): \Errors\Error {
250254
$this->file_to_generate = $o->file_to_generate;
251255
$this->parameter = $o->parameter;
252256
$tmp = $o->compiler_version;
253-
if ($tmp !== null) {
257+
if ($tmp is nonnull) {
254258
$nv = new \google\protobuf\compiler\Version();
255259
$nv->CopyFrom($tmp);
256260
$this->compiler_version = $nv;
@@ -337,7 +341,9 @@ public function MergeFrom(\Protobuf\Internal\Decoder $d): void {
337341
$this->content = $d->readString();
338342
break;
339343
case 16:
340-
if ($this->generated_code_info == null) $this->generated_code_info = new \google\protobuf\GeneratedCodeInfo();
344+
if ($this->generated_code_info is null) {
345+
$this->generated_code_info = new \google\protobuf\GeneratedCodeInfo();
346+
}
341347
$this->generated_code_info->MergeFrom($d->readDecoder());
342348
break;
343349
default:
@@ -391,8 +397,10 @@ public function MergeJsonFrom(mixed $m): void {
391397
$this->content = \Protobuf\Internal\JsonDecoder::readString($v);
392398
break;
393399
case 'generated_code_info': case 'generatedCodeInfo':
394-
if ($v === null) break;
395-
if ($this->generated_code_info == null) $this->generated_code_info = new \google\protobuf\GeneratedCodeInfo();
400+
if ($v is null) break;
401+
if ($this->generated_code_info is null) {
402+
$this->generated_code_info = new \google\protobuf\GeneratedCodeInfo();
403+
}
396404
$this->generated_code_info->MergeJsonFrom($v);
397405
break;
398406
default:
@@ -409,7 +417,7 @@ public function CopyFrom(\Protobuf\Message $o): \Errors\Error {
409417
$this->insertion_point = $o->insertion_point;
410418
$this->content = $o->content;
411419
$tmp = $o->generated_code_info;
412-
if ($tmp !== null) {
420+
if ($tmp is nonnull) {
413421
$nv = new \google\protobuf\GeneratedCodeInfo();
414422
$nv->CopyFrom($tmp);
415423
$this->generated_code_info = $nv;

0 commit comments

Comments
 (0)