-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix asJSON defaults option, make it work for repeated fields.
Fixing two issues in here: 1. When the `defaults` option was enabled, we were using `Object.keys(this)` which ends up returning an array that includes things like `$root` and asJSON. Instead we can use the `fields` we already have available. 2. For repeated values we were not setting the empty list default because the length check would break earlier. We're now passing through when the defaults option is set as well. Added a test on this stuff as well.
- Loading branch information
1 parent
c33835c
commit 5f9bede
Showing
3 changed files
with
36 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
syntax = "proto3"; | ||
|
||
package test; | ||
|
||
message TestRepeatedDefaults { | ||
required string aString = 1; | ||
repeated string repeatString = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var tape = require("tape"); | ||
var protobuf = require(".."); | ||
|
||
tape.test("test asJSON repeated defaults", function(test) { | ||
|
||
protobuf.load("tests/data/message.proto", function(err, root) { | ||
if (err) | ||
return test.fail(err.message); | ||
test.ok(true, "should parse without errors"); | ||
|
||
var TestMessage = root.lookup("test.TestRepeatedDefaults"); | ||
var msg1 = TestMessage.create({ aString: 'foo' }); | ||
|
||
var defaultsOn = msg1.asJSON({ defaults: true }); | ||
test.equal(defaultsOn.aString, 'foo', "should set aString value"); | ||
test.same(defaultsOn.repeatString, [], "should set repeated default"); | ||
|
||
var defaultsOff = msg1.asJSON({ defaults: false }); | ||
test.equal(defaultsOff.aString, 'foo', "should set aString value"); | ||
test.same(defaultsOff.repeatString, undefined, "should not set repeated default"); | ||
|
||
test.end(); | ||
}); | ||
|
||
}); |