Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly support proto3 optional fields in commonjs+dts .d.ts output #1184

Merged
merged 3 commits into from
Jan 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions javascript/net/grpc/web/generator/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,12 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc,
"set$js_field_name$(value?: $js_field_type$): "
"$class_name$;\n");
}
if (field->type() == FieldDescriptor::TYPE_MESSAGE &&
!field->is_repeated() && !field->is_map()) {
if (field->has_optional_keyword() ||
(field->type() == FieldDescriptor::TYPE_MESSAGE &&
!field->is_repeated() && !field->is_map())) {
printer->Print(vars, "has$js_field_name$(): boolean;\n");
}
if (field->type() == FieldDescriptor::TYPE_MESSAGE ||
if (field->type() == FieldDescriptor::TYPE_MESSAGE || field->has_optional_keyword() ||
field->is_repeated() || field->is_map()) {
printer->Print(vars, "clear$js_field_name$(): $class_name$;\n");
}
Expand All @@ -852,10 +853,12 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc,

for (int i = 0; i < desc->oneof_decl_count(); i++) {
const OneofDescriptor* oneof = desc->oneof_decl(i);
vars["js_oneof_name"] = ToUpperCamel(ParseLowerUnderscore(oneof->name()));
printer->Print(
vars, "get$js_oneof_name$Case(): $class_name$.$js_oneof_name$Case;\n");
printer->Print("\n");
if (!oneof->is_synthetic()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw nice job finding the is_synthetic() API!😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you forget what it's like to find docs via Google instead of using your IDE 😂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes right.. 😄

vars["js_oneof_name"] = ToUpperCamel(ParseLowerUnderscore(oneof->name()));
printer->Print(
vars, "get$js_oneof_name$Case(): $class_name$.$js_oneof_name$Case;\n");
printer->Print("\n");
}
}

printer->Print(
Expand Down Expand Up @@ -885,7 +888,7 @@ void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc,
}
vars["js_field_name"] = js_field_name;
vars["js_field_type"] = AsObjectFieldType(field, file);
if (field->type() != FieldDescriptor::TYPE_MESSAGE ||
if ((field->type() != FieldDescriptor::TYPE_MESSAGE && !field->has_optional_keyword()) ||
field->is_repeated()) {
printer->Print(vars, "$js_field_name$: $js_field_type$,\n");
} else {
Expand Down