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

fix: exclude map entry message from typings, fix optional values #452

Merged
merged 4 commits 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
199 changes: 158 additions & 41 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/descriptor.pb.h>
#include <algorithm>
#include <string>

Expand All @@ -30,6 +31,7 @@ using google::protobuf::FieldDescriptor;
using google::protobuf::FileDescriptor;
using google::protobuf::MethodDescriptor;
using google::protobuf::ServiceDescriptor;
using google::protobuf::FieldOptions;
using google::protobuf::compiler::CodeGenerator;
using google::protobuf::compiler::GeneratorContext;
using google::protobuf::compiler::ParseGeneratorParameter;
Expand Down Expand Up @@ -57,6 +59,76 @@ enum ImportStyle {
TYPESCRIPT = 2, // import * as grpcWeb from 'grpc-web'
};

const char* kKeyword[] = {
"abstract",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
};

bool IsReserved(const string& ident) {
for (int i = 0; i < sizeof(kKeyword) / sizeof(kKeyword[0]); i++) {
if (ident == kKeyword[i]) {
return true;
}
}
return false;
}

string GetModeVar(const Mode mode) {
switch (mode) {
case OP:
Expand Down Expand Up @@ -216,19 +288,18 @@ string ModuleAlias(const string& filename) {
}

string JSMessageType(const Descriptor *desc, const FileDescriptor *file) {
string result;
if (desc->file() != file) {
result = ModuleAlias(desc->file()->name());
}
result += StripPrefixString(desc->full_name(), desc->file()->package());
if (!result.empty() && result[0] == '.') {
result = result.substr(1);
}

return result;
string result;
if (desc->file() != file) {
result = ModuleAlias(desc->file()->name());
}
result += StripPrefixString(desc->full_name(), desc->file()->package());
if (!result.empty() && result[0] == '.') {
result = result.substr(1);
}
return result;
}

string JSFieldType(const FieldDescriptor *desc, const FileDescriptor *file)
string JSElementType(const FieldDescriptor *desc, const FileDescriptor *file)
{
string js_field_type;
switch (desc->type())
Expand All @@ -237,15 +308,21 @@ string JSFieldType(const FieldDescriptor *desc, const FileDescriptor *file)
case FieldDescriptor::TYPE_FLOAT:
case FieldDescriptor::TYPE_INT32:
case FieldDescriptor::TYPE_UINT32:
case FieldDescriptor::TYPE_SINT32:
case FieldDescriptor::TYPE_FIXED32:
case FieldDescriptor::TYPE_SFIXED32:
js_field_type = "number";
break;
case FieldDescriptor::TYPE_INT64:
case FieldDescriptor::TYPE_UINT64:
case FieldDescriptor::TYPE_FIXED32:
case FieldDescriptor::TYPE_FIXED64:
case FieldDescriptor::TYPE_SINT32:
case FieldDescriptor::TYPE_SINT64:
case FieldDescriptor::TYPE_SFIXED32:
case FieldDescriptor::TYPE_FIXED64:
case FieldDescriptor::TYPE_SFIXED64:
js_field_type = "number";
if (desc->options().jstype() == FieldOptions::JS_STRING) {
js_field_type = "string";
} else {
js_field_type = "number";
};
break;
case FieldDescriptor::TYPE_BOOL:
js_field_type = "boolean";
Expand All @@ -261,27 +338,52 @@ string JSFieldType(const FieldDescriptor *desc, const FileDescriptor *file)
}
break;
case FieldDescriptor::TYPE_MESSAGE:
if (desc->is_map()) {
string key_type = JSFieldType(desc->message_type()->field(0), file);
string value_type = JSFieldType(desc->message_type()->field(1), file);
return "jspb.Map<" + key_type + ", " + value_type + ">";
}
js_field_type = JSMessageType(desc->message_type(), file);
break;
default:
js_field_type = "{}";
break;
}
return js_field_type;
}

string JSFieldType(const FieldDescriptor *desc, const FileDescriptor *file) {
string js_field_type = JSElementType(desc, file);
if (desc->is_map()) {
string key_type = JSFieldType(desc->message_type()->field(0), file);
string value_type = JSFieldType(desc->message_type()->field(1), file);
return "jspb.Map<" + key_type + ", " + value_type + ">";
}
if (desc->is_repeated())
{
js_field_type += "[]";
return "Array<" + js_field_type + ">";
}
return js_field_type;
}

string JSFieldName(const FieldDescriptor *desc)
{
string js_field_name = ToUpperCamel(ParseLowerUnderscore(desc->name()));
string AsObjectFieldType(const FieldDescriptor *desc, const FileDescriptor *file) {
if (desc->type() != FieldDescriptor::TYPE_MESSAGE) {
return JSFieldType(desc, file);
};
if (desc->is_map()) {
const Descriptor* message = desc->message_type();
string key_type = AsObjectFieldType(message->field(0), file);
string value_type = AsObjectFieldType(message->field(1), file);
return "Array<[" + key_type + ", " + value_type + "]>";
};
string field_type = JSMessageType(desc->message_type(), file) + ".AsObject";
if (desc->is_repeated()) {
return "Array<" + field_type + ">";
};
return field_type;
}

string JSElementName(const FieldDescriptor *desc) {
return ToUpperCamel(ParseLowerUnderscore(desc->name()));
}

string JSFieldName(const FieldDescriptor *desc) {
string js_field_name = JSElementName(desc);
if (desc->is_map()) {
js_field_name += "Map";
} else if (desc->is_repeated()) {
Expand Down Expand Up @@ -693,29 +795,43 @@ void PrintProtoDtsMessage(Printer *printer, const Descriptor *desc,

printer->Print(vars, "export class $class_name$ extends jspb.Message {\n");
printer->Indent();
printer->Print("constructor ();\n");
for (int i = 0; i < desc->field_count(); i++) {
const FieldDescriptor* field = desc->field(i);
vars["js_field_name"] = JSFieldName(field);
vars["js_field_type"] = JSFieldType(field, file);
if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
if (field->type() != FieldDescriptor::TYPE_MESSAGE || field->is_repeated()) {
printer->Print(vars,
"get$js_field_name$(): $js_field_type$;\n");
} else {
printer->Print(vars,
"get$js_field_name$(): $js_field_type$ | undefined;\n");
}
if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
if (!field->is_map() && (field->type() != FieldDescriptor::TYPE_MESSAGE || field->is_repeated())) {
printer->Print(vars,
"set$js_field_name$(value: $js_field_type$): void;\n");
} else if (!field->is_map()) {
printer->Print(vars,
"set$js_field_name$(value?: $js_field_type$): void;\n");
}
if (field->type() == FieldDescriptor::TYPE_MESSAGE ||
field->is_repeated()) {
if (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 || field->is_repeated() || field->is_map()) {
printer->Print(vars, "clear$js_field_name$(): void;\n");
}
if (field->is_repeated() && !field->is_map()) {
vars["js_field_name"] = JSElementName(field);
vars["js_field_type"] = JSElementType(field, file);
if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
printer->Print(vars,
"add$js_field_name$(value: $js_field_type$, index?: number);\n");
} else {
printer->Print(vars,
"add$js_field_name$(value?: $js_field_type$, index?: number): $js_field_type$;\n");
}
}

printer->Print("\n");
}
printer->Print(
vars,
Expand All @@ -738,24 +854,25 @@ void PrintProtoDtsMessage(Printer *printer, const Descriptor *desc,
printer->Indent();
for (int i = 0; i < desc->field_count(); i++) {
const FieldDescriptor* field = desc->field(i);
vars["js_field_name"] = CamelCaseJSFieldName(field);
if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
vars["js_field_type"] = JSFieldType(field, file);
printer->Print(vars, "$js_field_name$: $js_field_type$;\n");
string js_field_name = CamelCaseJSFieldName(field);
if (IsReserved(js_field_name)) {
js_field_name = "pb_" + js_field_name;
}
vars["js_field_name"] = js_field_name;
vars["js_field_type"] = AsObjectFieldType(field, file);
if (field->type() != FieldDescriptor::TYPE_MESSAGE || field->is_repeated()) {
printer->Print(vars, "$js_field_name$: $js_field_type$,\n");
} else {
string js_field_type = JSMessageType(field->message_type(), file) +
".AsObject";
if (field->is_repeated()) {
js_field_type += "[]";
}
vars["js_field_type"] = js_field_type;
printer->Print(vars, "$js_field_name$?: $js_field_type$;\n");
printer->Print(vars, "$js_field_name$?: $js_field_type$,\n");
}
}
printer->Outdent();
printer->Print("}\n");

for (int i = 0; i < desc->nested_type_count(); i++) {
if (desc->nested_type(i)->options().map_entry()) {
continue;
}
printer->Print("\n");
PrintProtoDtsMessage(printer, desc->nested_type(i), file);
}
Expand Down