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

add typescript definition for Oneof fields #460

Merged
merged 1 commit into from
Feb 26, 2019
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
47 changes: 47 additions & 0 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ using google::protobuf::FileDescriptor;
using google::protobuf::MethodDescriptor;
using google::protobuf::ServiceDescriptor;
using google::protobuf::FieldOptions;
using google::protobuf::OneofDescriptor;
using google::protobuf::compiler::CodeGenerator;
using google::protobuf::compiler::GeneratorContext;
using google::protobuf::compiler::ParseGeneratorParameter;
Expand Down Expand Up @@ -175,6 +176,17 @@ string UppercaseFirstLetter(string s) {
return s;
}

string Uppercase(string s) {
if (s.empty()) {
return s;
}

for (size_t i = 0; i < s.size(); i++) {
s[i] = ::toupper(s[i]);
}
return s;
}

// The following 5 functions were copied from
// google/protobuf/src/google/protobuf/stubs/strutil.h

Expand Down Expand Up @@ -788,6 +800,25 @@ void PrintProtoDtsEnum(Printer *printer, const EnumDescriptor *desc)
printer->Print("}\n");
}

void PrintProtoDtsOneofCase(Printer *printer, const OneofDescriptor *desc)
{
std::map<string, string> vars;
vars["oneof_name"] = ToUpperCamel(ParseLowerUnderscore(desc->name()));
vars["oneof_name_upper"] = Uppercase(desc->name());

printer->Print(vars, "export enum $oneof_name$Case { \n");
printer->Indent();
printer->Print(vars, "$oneof_name_upper$_NOT_SET = 0,\n");
for (int i = 0; i < desc->field_count(); i++) {
const FieldDescriptor *field = desc->field(i);
vars["field_name"] = Uppercase(field->name());
vars["field_number"] = std::to_string(field->number());
printer->Print(vars, "$field_name$ = $field_number$,\n");
}
printer->Outdent();
printer->Print("}\n");
}

void PrintProtoDtsMessage(Printer *printer, const Descriptor *desc,
const FileDescriptor *file) {
string class_name = desc->name();
Expand Down Expand Up @@ -837,9 +868,20 @@ void PrintProtoDtsMessage(Printer *printer, const Descriptor *desc,
"index?: number): $js_field_type$;\n");
}
}
if (field->containing_oneof() != nullptr) {
printer->Print(vars, "has$js_field_name$(): boolean;\n");
}

printer->Print("\n");
}

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");
}

printer->Print(
vars,
"serializeBinary(): Uint8Array;\n"
Expand Down Expand Up @@ -890,6 +932,11 @@ void PrintProtoDtsMessage(Printer *printer, const Descriptor *desc,
PrintProtoDtsEnum(printer, desc->enum_type(i));
}

for (int i = 0; i < desc->oneof_decl_count(); i++) {
printer->Print("\n");
PrintProtoDtsOneofCase(printer, desc->oneof_decl(i));
}

printer->Outdent();
printer->Print("}\n\n");
}
Expand Down