Skip to content

Commit b1746a8

Browse files
NikhilVermaalexander-fenstermdouglassindutnyleon776
authored
feat: support parsing of complex options (#1744)
* feat: proto3 optional support * chore: pre-release v6.11.0-pre * fix: rebuild * fix: fromObject should not initialize oneof members (#1597) * test: adding test for pbjs static code generation * fix: fromObject should not initialize oneof members * chore: release v6.11.0 * chore: rebuild * feat: add --no-service option for pbjs static target (#1577) This option skips generation of service clients. Co-authored-by: Alexander Fenster <[email protected]> * deps: set @types/node to >= (#1575) * deps: set @types/node to star version When using `protobuf.js` as a dependency in a project it is important that `@types/node` package gets de-duped and has the same version as for the rest of the modules in the project. Otherwise, typing conflicts could happen as they do between v13 and v14 node types. * fix: use @types/node >=13.7.0 * fix: use @types/node >=13.7.0 Co-authored-by: Alexander Fenster <[email protected]> Co-authored-by: Alexander Fenster <[email protected]> * chore: rebuild * docs: update changelog * fix: parse.js "parent.add(oneof)“ error (#1602) Co-authored-by: xiaoweili <[email protected]> * chore: release v6.11.1 * fix(types): bring back Field.rule to .d.ts * fix: rebuild type, release v6.11.2 * build: configure backports * build: configure 6.x as default branch * fix: do not let setProperty change the prototype (#1731) * fix(deps): use eslint 8.x (#1728) * build: run tests if ci label added (#1734) * build: publish to main * chore(6.x): release 6.11.3 (#1737) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Support parsing of complex options * Use readValue to read the proto value and add better example * Fix lint issues * fix: rollback files * Re-do parse logic to take arrays into account and make it simpler Co-authored-by: Alexander Fenster <[email protected]> Co-authored-by: Matthew Douglass <[email protected]> Co-authored-by: Fedor Indutny <[email protected]> Co-authored-by: Alexander Fenster <[email protected]> Co-authored-by: leon <[email protected]> Co-authored-by: xiaoweili <[email protected]> Co-authored-by: Benjamin Coe <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 1d3c02a commit b1746a8

File tree

2 files changed

+91
-29
lines changed

2 files changed

+91
-29
lines changed

src/parse.js

+37-29
Original file line numberDiff line numberDiff line change
@@ -587,49 +587,57 @@ function parse(source, root, options) {
587587
}
588588

589589
function parseOptionValue(parent, name) {
590-
if (skip("{", true)) { // { a: "foo" b { c: "bar" } }
591-
var result = {};
590+
// { a: "foo" b { c: "bar" } }
591+
if (skip("{", true)) {
592+
var objectResult = {};
593+
592594
while (!skip("}", true)) {
593595
/* istanbul ignore if */
594-
if (!nameRe.test(token = next()))
596+
if (!nameRe.test(token = next())) {
595597
throw illegal(token, "name");
598+
}
596599

597600
var value;
598601
var propName = token;
602+
603+
skip(":", true);
604+
599605
if (peek() === "{")
600606
value = parseOptionValue(parent, name + "." + token);
601-
else {
602-
skip(":");
603-
if (peek() === "{")
604-
value = parseOptionValue(parent, name + "." + token);
605-
else if (peek() === "[") {
606-
// option (my_option) = {
607-
// repeated_value: [ "foo", "bar" ]
608-
// };
609-
value = [];
610-
var lastValue;
611-
if (skip("[", true)) {
612-
do {
613-
lastValue = readValue(true);
614-
value.push(lastValue);
615-
} while (skip(",", true));
616-
skip("]");
617-
if (typeof lastValue !== "undefined") {
618-
setOption(parent, name + "." + token, lastValue);
619-
}
607+
else if (peek() === "[") {
608+
// option (my_option) = {
609+
// repeated_value: [ "foo", "bar" ]
610+
// };
611+
value = [];
612+
var lastValue;
613+
if (skip("[", true)) {
614+
do {
615+
lastValue = readValue(true);
616+
value.push(lastValue);
617+
} while (skip(",", true));
618+
skip("]");
619+
if (typeof lastValue !== "undefined") {
620+
setOption(parent, name + "." + token, lastValue);
620621
}
621-
} else {
622-
value = readValue(true);
623-
setOption(parent, name + "." + token, value);
624622
}
623+
} else {
624+
value = readValue(true);
625+
setOption(parent, name + "." + token, value);
625626
}
626-
var prevValue = result[propName];
627+
628+
var prevValue = objectResult[propName];
629+
627630
if (prevValue)
628631
value = [].concat(prevValue).concat(value);
629-
result[propName] = value;
630-
skip(",", true) || skip(";", true);
632+
633+
objectResult[propName] = value;
634+
635+
// Semicolons and commas can be optional
636+
skip(",", true);
637+
skip(";", true);
631638
}
632-
return result;
639+
640+
return objectResult;
633641
}
634642

635643
var simpleValue = readValue(true);

tests/comp_options.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var tape = require("tape");
2+
3+
var protobuf = require("..");
4+
5+
var proto = `
6+
syntax = "proto3";
7+
8+
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
9+
info: {
10+
title: "Some info";
11+
version: "0";
12+
};
13+
host: "some.host";
14+
};
15+
16+
message Message {
17+
int32 regular_int32 = 1;
18+
optional int32 optional_int32 = 2;
19+
oneof _oneof_int32 {
20+
int32 oneof_int32 = 3;
21+
}
22+
actionType action = 4 [ (validate.rules).enum = {
23+
defined_only: true,
24+
not_in: [ 0 ],
25+
in: ["google","github","azuread"]
26+
} ];
27+
}
28+
`;
29+
30+
tape.test("complex options", function (test) {
31+
var root = protobuf.parse(proto).root;
32+
33+
test.deepEqual(root.parsedOptions[0], {
34+
"(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger)": {
35+
info: {
36+
title: "Some info",
37+
version: "0",
38+
},
39+
host: "some.host",
40+
},
41+
});
42+
43+
test.deepEqual(root.Message.fields.action.parsedOptions[0], {
44+
"(validate.rules)": {
45+
enum: {
46+
defined_only: true,
47+
not_in: [0],
48+
in: ["google", "github", "azuread"],
49+
},
50+
},
51+
});
52+
53+
test.end();
54+
});

0 commit comments

Comments
 (0)