Skip to content

Commit

Permalink
fix: support for options with repeated_value: [ "foo", "bar" ] (#1574)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Fenster <fenster@google.com>
  • Loading branch information
doochik and alexander-fenster authored May 14, 2021
1 parent 6e713ba commit f5b893c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,23 @@ function parse(source, root, options) {
skip(":");
if (peek() === "{")
value = parseOptionValue(parent, name + "." + token);
else {
else if (peek() === "[") {
// option (my_option) = {
// repeated_value: [ "foo", "bar" ]
// };
value = [];
var lastValue;
if (skip("[", true)) {
do {
lastValue = readValue(true);
value.push(lastValue);
} while (skip(",", true));
skip("]");
if (typeof lastValue !== "undefined") {
setOption(parent, name + "." + token, lastValue);
}
}
} else {
value = readValue(true);
setOption(parent, name + "." + token, value);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/comp_options-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ tape.test("Options", function (test) {

test.test(test.name + " - message options (Message)", function (test) {
var TestMessageOptionsMsg = root.lookup("TestMessageOptionsMsg");
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).value"], 4, "should take second repeated message option");
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).rep_value"], 6, "should take second repeated int in second repeated option");
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).value"], 5, "should take last repeated message option");
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).rep_value"], 8, "should take last repeated int in last repeated option");
test.equal(TestMessageOptionsMsg.options["(mo_single_msg).value"], 7, "should correctly parse single msg option");
test.equal(TestMessageOptionsMsg.options["(mo_single_msg).rep_value"], 9, "should take second repeated int in single msg option");
test.same(TestMessageOptionsMsg.parsedOptions, [
{"(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_single_msg)": {value: 7, rep_value: [8, 9]}},
], "should take all message options");
test.end();
Expand Down
4 changes: 4 additions & 0 deletions tests/data/options_test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ message TestMessageOptionsMsg {
rep_value: 5
rep_value: 6
};
option (mo_rep_msg) = {
value: 5
rep_value: [ 7, 8 ]
};
option (mo_single_msg).value = 7;
option (mo_single_msg).rep_value = 8;
option (mo_single_msg).rep_value = 9;
Expand Down

0 comments on commit f5b893c

Please sign in to comment.