Skip to content

Commit

Permalink
Fixed: Properly parse nested textformat options, also tackles #655
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 21, 2017
1 parent 27b1635 commit ef7be35
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/minimal/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/minimal/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
32 changes: 18 additions & 14 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,24 +494,28 @@ function parse(source, root, options) {
}

function parseOptionValue(parent, name) {
if (skip("{", true)) {
while ((token = next()) !== "}") {

/* istanbul ignore next */
if (!isName(token))
if (skip("{", true)) { // { a: "foo" b { c: "bar" } }
/* istanbul ignore next */
do {
if (!isName(token = next()))
throw illegal(token, "name");

skip(":");
// if (skip(":", true))
parent.setOption(name + "." + token, readValue(true));
// else
// parseOptionValue(parent, name + "." + token);
}
if (peek() === "{")
parseOptionValue(parent, name + "." + token);
else {
skip(":");
setOption(parent, name + "." + token, readValue(true));
}
} while (!skip("}", true));
} else
parent.setOption(name, readValue(true));
setOption(parent, name, readValue(true));
// Does not enforce a delimiter to be universal
}

function setOption(parent, name, value) {
if (parent.setOption)
parent.setOption(name, value);
}

function parseInlineOptions(parent) {
if (skip("[", true)) {
do {
Expand Down
2 changes: 2 additions & 0 deletions tests/comp_options-textformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ extend google.protobuf.FieldOptions {\
}\
message Test {\
string value = 1 [(my_options) = { a: \"foo\" b: \"bar\" }];\
string value2 = 2 [(my_options) = { a: \"foo\" b { c: \"bar\" } }];\
}";

tape.test("options in textformat", function(test) {
var root = protobuf.parse(proto).root;
var Test = root.lookup("Test");
test.same(Test.fields.value.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly");
test.same(Test.fields.value2.options, { "(my_options).a": "foo", "(my_options).b.c": "bar" }, "should parse correctly when nested");
test.end();
});
2 changes: 2 additions & 0 deletions tests/data/uncommon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ enum Test3;

enum Test4{
option (custom).foo = "";
ONE = 1 [foo="bar"];
TWO = 2 [(my_options) = { a: "foo" b { c: "bar" } }];
};

service Test5;
Expand Down

0 comments on commit ef7be35

Please sign in to comment.