Skip to content

Commit d3ce92c

Browse files
reddalygonzojive
authored andcommitted
Fix treatment of nested messages by TypeNames::JsExpression.
Before this, the parent type would be returned. Now, <parent type>.<child type> is returned.
1 parent 7193703 commit d3ce92c

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

generator/js_generator.cc

+19-20
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ void Generator::GenerateRequiresImpl(const GeneratorOptions& options,
18541854
std::set<std::string>* provided,
18551855
bool require_jspb, bool require_extension,
18561856
bool require_map) const {
1857-
1857+
18581858
if (options.WantEs6()) {
18591859
// In ES6 mode, imports are handled by GenerateFile and
18601860
// goog.* isn't used.
@@ -2044,7 +2044,7 @@ void Generator::GenerateClassEs6(const GeneratorOptions& options,
20442044
return;
20452045
}
20462046

2047-
2047+
20482048
std::string prefix = (desc->containing_type() == nullptr) ?
20492049
"export " : ("static " + desc->name() + " = ");
20502050

@@ -2068,11 +2068,11 @@ void Generator::GenerateClassEs6(const GeneratorOptions& options,
20682068
"$prefix$class $classname$ extends jspb.Message {\n",
20692069
"prefix", prefix,
20702070
"classname", desc->name());
2071-
2071+
20722072
printer->Indent();
20732073

20742074
GenerateClassConstructor(options, printer, desc);
2075-
2075+
20762076
GenerateClassFieldInfo(options, printer, desc);
20772077

20782078
GenerateClassToObject(options, type_names, printer, desc);
@@ -2255,7 +2255,7 @@ void Generator::GenerateClassXid(const GeneratorOptions& options,
22552255
void Generator::GenerateOneofCaseDefinition(
22562256
const GeneratorOptions& options, io::Printer* printer,
22572257
const OneofDescriptor* oneof) const {
2258-
2258+
22592259
const std::string className = GetMessagePath(options, oneof->containing_type());
22602260

22612261
const std::string oneofCaseName = options.WantEs6() ? (
@@ -2294,7 +2294,7 @@ void Generator::GenerateOneofCaseDefinition(
22942294
" */\n",
22952295
"class", className,
22962296
"oneof", JSOneofName(oneof));
2297-
2297+
22982298
GenerateMethodStart(options, printer, className.c_str(),
22992299
(std::string("get") + JSOneofName(oneof) + "Case").c_str());
23002300

@@ -2319,7 +2319,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
23192319
const char * if_guard_start = options.WantEs6() ? "" : "if (jspb.Message.GENERATE_TO_OBJECT) {\n";
23202320
const char * if_guard_end = options.WantEs6() ? "" : "}\n";
23212321
const std::string classSymbol = options.WantEs6() ? desc->name() : GetMessagePath(options, desc);
2322-
2322+
23232323
printer->Print(
23242324
"\n"
23252325
"\n"
@@ -2742,7 +2742,7 @@ const char * methodEndBrace = options.WantEs6() ? "}" : "};";
27422742
"fielddef", FieldDefinition(options, field),
27432743
"keytype", key_type,
27442744
"valuetype", value_type);
2745-
2745+
27462746
// Function start
27472747
if (options.import_style == GeneratorOptions::kImportEs6) {
27482748
printer->Print(
@@ -2981,12 +2981,12 @@ const char * methodEndBrace = options.WantEs6() ? "}" : "};";
29812981
"value);\n",
29822982
"typetag", JSTypeTag(field),
29832983
"index", JSFieldIndex(field));
2984-
2984+
29852985
GenerateMethodEnd(options, printer);
29862986
printer->Print(
29872987
"\n"
29882988
"\n");
2989-
2989+
29902990
// DO NOT SUBMIT: Can the Annotate call be safely removed?
29912991
// printer->Annotate("settername", field);
29922992

@@ -3132,9 +3132,9 @@ const char * methodEndBrace = options.WantEs6() ? "}" : "};";
31323132
void Generator::GenerateRepeatedPrimitiveHelperMethods(
31333133
const GeneratorOptions& options, io::Printer* printer,
31343134
const FieldDescriptor* field, bool untyped) const {
3135-
3135+
31363136
const std::string classSymbol = GetMessagePath(options, field->containing_type());
3137-
const std::string adderName = std::string("add") +
3137+
const std::string adderName = std::string("add") +
31383138
JSGetterName(options, field, BYTES_DEFAULT, /* drop_list = */ true);
31393139
const std::string adderMethodStart = MethodStart(
31403140
options, classSymbol.c_str(), adderName.c_str());
@@ -3184,7 +3184,7 @@ void Generator::GenerateRepeatedMessageHelperMethods(
31843184
const FieldDescriptor* field) const {
31853185

31863186
const std::string classSymbol = GetMessagePath(options, field->containing_type());
3187-
const std::string adderName = std::string("add") +
3187+
const std::string adderName = std::string("add") +
31883188
JSGetterName(options, field, BYTES_DEFAULT, /* drop_list = */ true);
31893189
const std::string adderMethodStart = MethodStart(
31903190
options, classSymbol.c_str(), adderName.c_str());
@@ -3975,7 +3975,7 @@ void RegisterTypesDefinedInGeneratedFile(
39753975
TypeNames TypeNames::Es6TypeNames(
39763976
const GeneratorOptions& options,
39773977
const FileDescriptor* codegen_file) {
3978-
3978+
39793979
// Full proto name -> local alias in JS codegen file.
39803980
std::map<std::string, std::string> full_name_to_alias;
39813981
// Local aliases that are already reserved
@@ -4017,7 +4017,7 @@ TypeNames TypeNames::Es6TypeNames(
40174017
pick_name(message_type->full_name(), message_type->name());
40184018
}
40194019
}
4020-
4020+
40214021
for (int j = 0; j < file->enum_type_count(); j++) {
40224022
auto enum_type = file->enum_type(j);
40234023
if (file == codegen_file) {
@@ -4069,7 +4069,7 @@ std::string TypeNames::SubmessageTypeRef(const FieldDescriptor* field) const {
40694069
GOOGLE_CHECK(this->codegen_file == nullptr ||
40704070
this->codegen_file == field->file());
40714071
GOOGLE_CHECK_NOTNULL(field->message_type());
4072-
return JsExpression(*field->message_type());
4072+
return JsExpression(*field->message_type()) + "/* message_type = " + field->message_type()->DebugString() +" */";
40734073
}
40744074

40754075
std::string TypeNames::JsExpression(const std::string& full_name) const {
@@ -4079,15 +4079,14 @@ std::string TypeNames::JsExpression(const std::string& full_name) const {
40794079
if (iter != this->map_.end()) {
40804080
return iter->second;
40814081
}
4082-
// See if the parent full_name is available.
4082+
// See if the parent full_name is available. If it is, use it as the prefix.
40834083
auto parts = google::protobuf::Split(full_name, ".", false);
40844084
if (parts.size() > 1) {
4085-
// uh oh... not sure how this happend.
40864085
std::vector<std::string> parent_parts = {parts.begin(), parts.end() - 1};
40874086
auto parent_path = google::protobuf::JoinStrings(
40884087
parent_parts,
40894088
".");
4090-
return this->JsExpression(parent_path);
4089+
return this->JsExpression(parent_path) + "." + parts[parts.size() - 1];
40914090
}
40924091
return std::string("INVALID TYPE NAME ") + full_name;
40934092
}
@@ -4117,7 +4116,7 @@ std::vector<std::string> ImportAliases(
41174116
out.push_back(exported_name + " as " + alias);
41184117
}
41194118
}
4120-
4119+
41214120
for (int j = 0; j < dep_file.enum_type_count(); j++) {
41224121
auto enum_type = dep_file.enum_type(j);
41234122
out.push_back(type_names.JsExpression(*enum_type));

0 commit comments

Comments
 (0)