Skip to content

Commit ea456ed

Browse files
committed
Update 'syntax' and synthetic oneof to support protobuf 27.1 descriptor API
1 parent 1976603 commit ea456ed

File tree

7 files changed

+538
-79
lines changed

7 files changed

+538
-79
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ permissions: read-all
66

77
# update in build.yml and codeql.yml at same time
88
env:
9-
PROTOC_VERSION: 23.1
9+
PROTOC_VERSION: 27.1
1010

1111
jobs:
1212
build:

.github/workflows/codeql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ permissions: read-all
1515

1616
# update in build.yml and codeql.yml at same time
1717
env:
18-
PROTOC_VERSION: 23.1
18+
PROTOC_VERSION: 27.1
1919

2020
on:
2121
push:

MODULE.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module(name = "protobuf_javascript", version = "3.21.2")
22

3-
bazel_dep(name = "protobuf", version = "23.1", repo_name = "com_google_protobuf")
3+
bazel_dep(name = "protobuf", version = "27.1", repo_name = "com_google_protobuf")
44
bazel_dep(name = "rules_pkg", version = "0.7.0")

MODULE.bazel.lock

+122
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WORKSPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

55
http_archive(
66
name = "com_google_protobuf",
7-
urls = ["https://github.com/protocolbuffers/protobuf/archive/refs/tags/v23.1.zip"],
8-
sha256 = "c0ea9f4d75b37ea8e9d78ce4c670d066bcb7cebdba190fa5fc8c57b1f00c0c2c",
9-
strip_prefix = "protobuf-23.1",
7+
urls = ["https://github.com/protocolbuffers/protobuf/archive/refs/tags/v27.1.zip"],
8+
sha256 = "7d7f2ddccc37e3c1c5dfe65ad69d99923d8fe84beac68ed9cdec489909c4d8d3",
9+
strip_prefix = "protobuf-27.1",
1010
)
1111

1212
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

generator/js_generator.cc

+22-19
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,19 @@ bool IgnoreField(const FieldDescriptor* field) {
452452
// Do we ignore this message type?
453453
bool IgnoreMessage(const Descriptor* d) { return d->options().map_entry(); }
454454

455+
bool IsSyntheticOneof(const OneofDescriptor* oneof) {
456+
if (oneof->field_count() == 1 &&
457+
oneof->field(0)->real_containing_oneof() == nullptr) {
458+
return true;
459+
}
460+
// if (OneofDescriptorLegacy(oneof).is_synthetic()) return true;
461+
462+
return false;
463+
}
464+
455465
// Does JSPB ignore this entire oneof? True only if all fields are ignored.
456466
bool IgnoreOneof(const OneofDescriptor* oneof) {
457-
if (OneofDescriptorLegacy(oneof).is_synthetic()) return true;
467+
if (IsSyntheticOneof(oneof)) { return true; }
458468
for (int i = 0; i < oneof->field_count(); i++) {
459469
if (!IgnoreField(oneof->field(i))) {
460470
return false;
@@ -563,7 +573,7 @@ std::string JSOneofIndex(const OneofDescriptor* oneof) {
563573
int index = -1;
564574
for (int i = 0; i < oneof->containing_type()->oneof_decl_count(); i++) {
565575
const OneofDescriptor* o = oneof->containing_type()->oneof_decl(i);
566-
if (OneofDescriptorLegacy(o).is_synthetic()) continue;
576+
if (IsSyntheticOneof(o)) { continue; }
567577
// If at least one field in this oneof is not JSPB-ignored, count the oneof.
568578
for (int j = 0; j < o->field_count(); j++) {
569579
const FieldDescriptor* f = o->field(j);
@@ -783,8 +793,7 @@ std::string DoubleToString(double value) {
783793
}
784794

785795
bool InRealOneof(const FieldDescriptor* field) {
786-
return field->containing_oneof() &&
787-
!OneofDescriptorLegacy(field->containing_oneof()).is_synthetic();
796+
return field->real_containing_oneof() != nullptr;
788797
}
789798

790799
// Return true if this is an integral field that should be represented as string
@@ -984,7 +993,7 @@ bool DeclaredReturnTypeIsNullable(const GeneratorOptions& options,
984993
return false;
985994
}
986995

987-
if (FileDescriptorLegacy(field->file()).syntax() == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
996+
if (!field->has_presence() && !field->is_repeated() &&
988997
field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
989998
return false;
990999
}
@@ -2345,17 +2354,13 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
23452354
printer->Print("msg.get$getter$()", "getter",
23462355
JSGetterName(options, field, BYTES_B64));
23472356
} else {
2348-
bool use_default = field->has_default_value();
2349-
2350-
if (FileDescriptorLegacy(field->file()).syntax() == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
2351-
// Repeated fields get initialized to their default in the constructor
2352-
// (why?), so we emit a plain getField() call for them.
2353-
!field->is_repeated()) {
2354-
// Proto3 puts all defaults (including implicit defaults) in toObject().
2355-
// But for proto2 we leave the existing semantics unchanged: unset fields
2356-
// without default are unset.
2357-
use_default = true;
2358-
}
2357+
// We rely on the default field value if it is explicit in the .proto file
2358+
// or if the field in question doesn't have presence semantics (consider
2359+
// proto3 fields without optional, repeated fields)
2360+
// Repeated fields get initialized to their default in the constructor
2361+
// (why?), so we emit a plain getField() call for them.
2362+
const bool use_default = !field->is_repeated() &&
2363+
(field->has_default_value() || !field->has_presence());
23592364

23602365
// We don't implement this by calling the accessors, because the semantics
23612366
// of the accessors are changing independently of the toObject() semantics.
@@ -2755,9 +2760,7 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
27552760
/* force_present = */ false,
27562761
/* singular_if_not_packed = */ false));
27572762

2758-
if (FileDescriptorLegacy(field->file()).syntax() == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
2759-
!field->is_repeated() && !field->is_map() &&
2760-
!HasFieldPresence(options, field)) {
2763+
if (!field->is_repeated() && !field->is_map() && !field->has_presence()) {
27612764
// Proto3 non-repeated and non-map fields without presence use the
27622765
// setProto3*Field function.
27632766
printer->Print(

0 commit comments

Comments
 (0)