Skip to content

Commit

Permalink
fix: support for empty repeated options (protobufjs#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinybot committed May 12, 2022
1 parent d13d5d5 commit 6904a93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ function parse(source, root, options) {
// };
value = [];
var lastValue;
if (skip("[", true)) {
if (skip("[", true) && !skip("]", true)) {
do {
lastValue = readValue(true);
value.push(lastValue);
Expand Down
10 changes: 10 additions & 0 deletions tests/comp_options-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,21 @@ tape.test("Options", function (test) {
{"(mo_rep_msg)": {value: 1, rep_value: [2, 3]}},
{"(mo_rep_msg)": {value: 4, rep_value: [5, 6]}},
{"(mo_rep_msg)": {value: 5, rep_value: [7, 8]}},
{"(mo_rep_msg)": {rep_value: []}},
{"(mo_single_msg)": {value: 7, rep_value: [8, 9]}},
], "should take all message options");
test.end();
});

test.test(test.name + " - message options (Empty)", function (test) {
var TestMessageOptionsMsg = root.lookup("TestMessageOptionsEmpty");
test.equal("(mo_rep_msg).rep_value" in TestMessageOptionsMsg.options, false, "should treat empty repeated option as missing");
test.same(TestMessageOptionsMsg.parsedOptions, [
{"(mo_rep_msg)": {value: 1, rep_value: []}},
], "should parse empty options correctly");
test.end();
});

test.test(test.name + " - field options (Nested)", function (test) {
var TestFieldOptionsNested = root.lookup("TestFieldOptionsNested");
test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).value"], 1, "should merge repeated options messages");
Expand Down
10 changes: 10 additions & 0 deletions tests/data/options_test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,21 @@ message TestMessageOptionsMsg {
value: 5
rep_value: [ 7, 8 ]
};
option (mo_rep_msg) = {
rep_value: [ ]
};
option (mo_single_msg).value = 7;
option (mo_single_msg).rep_value = 8;
option (mo_single_msg).rep_value = 9;
}

message TestMessageOptionsEmpty {
option (mo_rep_msg) = {
value: 1
rep_value: [ ]
};
}

message TestFieldOptionsNested {
string field1 = 1 [(fo_rep_msg) = {value: 1 nested { nested {value: "x"} } rep_nested { value: "y"} rep_nested { value: "z" } rep_value: 3}, (fo_rep_msg) = { nested { value: "w" }}];
string field2 = 2 [(fo_single_msg).nested.value = "x", (fo_single_msg).rep_nested = {value : "x"}, (fo_single_msg).rep_nested = {value : "y"}];
Expand Down

0 comments on commit 6904a93

Please sign in to comment.