|
@@ -1952,7 +1952,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
|
|
" * @return {!Object}\n"
|
|
" * @return {!Object}\n"
|
|
" */\n"
|
|
" */\n"
|
|
"$classname$.toObject = function(includeInstance, msg) {\n"
|
|
"$classname$.toObject = function(includeInstance, msg) {\n"
|
|
- " var f, obj = {};",
|
|
|
|
|
|
+ " var f, obj = {",
|
|
"classname", GetPath(options, desc));
|
|
"classname", GetPath(options, desc));
|
|
|
|
|
|
bool first = true;
|
|
bool first = true;
|
|
@@ -1963,16 +1963,20 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
|
|
}
|
|
}
|
|
|
|
|
|
if (!first) {
|
|
if (!first) {
|
|
- printer->Print("\n ");
|
|
|
|
|
|
+ printer->Print(",\n ");
|
|
} else {
|
|
} else {
|
|
- printer->Print("\n\n ");
|
|
|
|
|
|
+ printer->Print("\n ");
|
|
first = false;
|
|
first = false;
|
|
}
|
|
}
|
|
|
|
|
|
GenerateClassFieldToObject(options, printer, field);
|
|
GenerateClassFieldToObject(options, printer, field);
|
|
}
|
|
}
|
|
|
|
|
|
- printer->Print("\n\n");
|
|
|
|
|
|
+ if (!first) {
|
|
|
|
+ printer->Print("\n };\n\n");
|
|
|
|
+ } else {
|
|
|
|
+ printer->Print("\n\n };\n\n");
|
|
|
|
+ }
|
|
|
|
|
|
if (IsExtendable(desc)) {
|
|
if (IsExtendable(desc)) {
|
|
printer->Print(
|
|
printer->Print(
|
|
@@ -1999,12 +2003,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
|
|
void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
|
|
void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
|
|
io::Printer* printer,
|
|
io::Printer* printer,
|
|
const FieldDescriptor* field) const {
|
|
const FieldDescriptor* field) const {
|
|
- if (HasFieldPresence(field)) {
|
|
|
|
- printer->Print("if (msg.has$name$()) ",
|
|
|
|
- "name", JSGetterName(options, field));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- printer->Print("obj.$fieldname$ = ",
|
|
|
|
|
|
+ printer->Print("$fieldname$: ",
|
|
"fieldname", JSObjectFieldName(options, field));
|
|
"fieldname", JSObjectFieldName(options, field));
|
|
|
|
|
|
if (field->is_map()) {
|
|
if (field->is_map()) {
|
|
@@ -2034,12 +2033,21 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
|
|
printer->Print("msg.get$getter$()",
|
|
printer->Print("msg.get$getter$()",
|
|
"getter", JSGetterName(options, field, BYTES_B64));
|
|
"getter", JSGetterName(options, field, BYTES_B64));
|
|
} else {
|
|
} else {
|
|
|
|
+ if (field->has_default_value()) {
|
|
|
|
+ printer->Print("!msg.has$name$() ? $defaultValue$ : ",
|
|
|
|
+ "name", JSGetterName(options, field),
|
|
|
|
+ "defaultValue", JSFieldDefault(field));
|
|
|
|
+ }
|
|
if (field->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT ||
|
|
if (field->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT ||
|
|
field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE) {
|
|
field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE) {
|
|
if (field->is_repeated()) {
|
|
if (field->is_repeated()) {
|
|
printer->Print("jspb.Message.getRepeatedFloatingPointField("
|
|
printer->Print("jspb.Message.getRepeatedFloatingPointField("
|
|
"msg, $index$)",
|
|
"msg, $index$)",
|
|
"index", JSFieldIndex(field));
|
|
"index", JSFieldIndex(field));
|
|
|
|
+ } else if (field->is_optional() && !field->has_default_value()) {
|
|
|
|
+ printer->Print("jspb.Message.getOptionalFloatingPointField("
|
|
|
|
+ "msg, $index$)",
|
|
|
|
+ "index", JSFieldIndex(field));
|
|
} else {
|
|
} else {
|
|
// Convert "NaN" to NaN.
|
|
// Convert "NaN" to NaN.
|
|
printer->Print("+jspb.Message.getField(msg, $index$)",
|
|
printer->Print("+jspb.Message.getField(msg, $index$)",
|