Skip to content

Commit 27f9fc8

Browse files
committed
Support for options with repeated_value: [ "foo", "bar" ]
1 parent 95b5681 commit 27f9fc8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/parse.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,23 @@ function parse(source, root, options) {
577577
skip(":");
578578
if (peek() === "{")
579579
value = parseOptionValue(parent, name + "." + token);
580-
else {
580+
else if (peek() === "[") {
581+
// option (my_option) = {
582+
// repeated_value: [ "foo", "bar" ]
583+
// };
584+
value = [];
585+
var lastValue;
586+
if (skip("[", true)) {
587+
do {
588+
lastValue = readValue(true);
589+
value.push(lastValue);
590+
} while (skip(",", true));
591+
skip("]");
592+
if (typeof lastValue !== 'undefined') {
593+
setOption(parent, name + "." + token, lastValue);
594+
}
595+
}
596+
} else {
581597
value = readValue(true);
582598
setOption(parent, name + "." + token, value);
583599
}

tests/comp_options-parse.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ tape.test("Options", function (test) {
3737

3838
test.test(test.name + " - message options (Message)", function (test) {
3939
var TestMessageOptionsMsg = root.lookup("TestMessageOptionsMsg");
40-
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).value"], 4, "should take second repeated message option");
41-
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).rep_value"], 6, "should take second repeated int in second repeated option");
40+
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).value"], 5, "should take last repeated message option");
41+
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).rep_value"], 8, "should take last repeated int in last repeated option");
4242
test.equal(TestMessageOptionsMsg.options["(mo_single_msg).value"], 7, "should correctly parse single msg option");
4343
test.equal(TestMessageOptionsMsg.options["(mo_single_msg).rep_value"], 9, "should take second repeated int in single msg option");
4444
test.same(TestMessageOptionsMsg.parsedOptions, [
4545
{"(mo_rep_msg)": {value: 1, rep_value: [2, 3]}},
4646
{"(mo_rep_msg)": {value: 4, rep_value: [5, 6]}},
47+
{"(mo_rep_msg)": {value: 5, rep_value: [7, 8]}},
4748
{"(mo_single_msg)": {value: 7, rep_value: [8, 9]}},
4849
], "should take all message options");
4950
test.end();

tests/data/options_test.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ message TestMessageOptionsMsg {
7474
rep_value: 5
7575
rep_value: 6
7676
};
77+
option (mo_rep_msg) = {
78+
value: 5
79+
rep_value: [ 7, 8 ]
80+
};
7781
option (mo_single_msg).value = 7;
7882
option (mo_single_msg).rep_value = 8;
7983
option (mo_single_msg).rep_value = 9;

0 commit comments

Comments
 (0)