Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support for options with repeated_value: [ "foo", "bar" ] #1574

Merged
merged 3 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,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