aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikolai Vavilov <vvnicholas@gmail.com>2016-07-19 10:34:58 +0300
committerNikolai Vavilov <vvnicholas@gmail.com>2016-07-19 10:34:58 +0300
commite4b129f304a28db71a538af35afae0af91b02129 (patch)
tree6e8b2cfcf82f18a70bc874c327fc33b711c84680
parentdb1b2a06e3999ffd6e52719ec0216849aa515ba1 (diff)
downloadprotobuf-e4b129f304a28db71a538af35afae0af91b02129.tar.gz
protobuf-e4b129f304a28db71a538af35afae0af91b02129.tar.bz2
protobuf-e4b129f304a28db71a538af35afae0af91b02129.zip
restore old behavior for toObject
-rw-r--r--js/message_test.js22
-rwxr-xr-xsrc/google/protobuf/compiler/js/js_generator.cc28
2 files changed, 37 insertions, 13 deletions
diff --git a/js/message_test.js b/js/message_test.js
index c9eee17d..b7791431 100644
--- a/js/message_test.js
+++ b/js/message_test.js
@@ -145,7 +145,11 @@ describe('Message test suite', function() {
undefined, undefined, undefined, undefined]);
var result = foo.toObject();
assertObjectEquals({
- aNestedMessage: {},
+ aString: undefined,
+ anOutOfOrderBool: undefined,
+ aNestedMessage: {
+ anInt: undefined
+ },
// Note: JsPb converts undefined repeated fields to empty arrays.
aRepeatedMessageList: [],
aRepeatedStringList: []
@@ -180,7 +184,14 @@ describe('Message test suite', function() {
var response = new proto.jspb.test.DefaultValues();
// Test toObject
- var expectedObject = {};
+ var expectedObject = {
+ stringField: defaultString,
+ boolField: true,
+ intField: 11,
+ enumField: 13,
+ emptyField: '',
+ bytesField: 'bW9v'
+ };
assertObjectEquals(expectedObject, response.toObject());
@@ -748,7 +759,12 @@ describe('Message test suite', function() {
assertObjectEquals({id: 'g1', someBoolList: [true, false]},
groups[0].toObject());
assertObjectEquals({
- repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}]
+ repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}],
+ requiredGroup: {id: undefined},
+ optionalGroup: undefined,
+ requiredSimple: {aRepeatedStringList: [], aString: undefined},
+ optionalSimple: undefined,
+ id: undefined
}, group.toObject());
var group1 = new proto.jspb.test.TestGroup1();
group1.setGroup(someGroup);
diff --git a/src/google/protobuf/compiler/js/js_generator.cc b/src/google/protobuf/compiler/js/js_generator.cc
index 53067739..58c77d00 100755
--- a/src/google/protobuf/compiler/js/js_generator.cc
+++ b/src/google/protobuf/compiler/js/js_generator.cc
@@ -1952,7 +1952,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
" * @return {!Object}\n"
" */\n"
"$classname$.toObject = function(includeInstance, msg) {\n"
- " var f, obj = {};",
+ " var f, obj = {",
"classname", GetPath(options, desc));
bool first = true;
@@ -1963,16 +1963,20 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
}
if (!first) {
- printer->Print("\n ");
+ printer->Print(",\n ");
} else {
- printer->Print("\n\n ");
+ printer->Print("\n ");
first = false;
}
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)) {
printer->Print(
@@ -1999,12 +2003,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
io::Printer* printer,
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));
if (field->is_map()) {
@@ -2034,12 +2033,21 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
printer->Print("msg.get$getter$()",
"getter", JSGetterName(options, field, BYTES_B64));
} 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 ||
field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE) {
if (field->is_repeated()) {
printer->Print("jspb.Message.getRepeatedFloatingPointField("
"msg, $index$)",
"index", JSFieldIndex(field));
+ } else if (field->is_optional() && !field->has_default_value()) {
+ printer->Print("jspb.Message.getOptionalFloatingPointField("
+ "msg, $index$)",
+ "index", JSFieldIndex(field));
} else {
// Convert "NaN" to NaN.
printer->Print("+jspb.Message.getField(msg, $index$)",