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

feat: support parsing of complex options #1744

Merged
merged 28 commits into from Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0f88cb1
feat: proto3 optional support
alexander-fenster Apr 9, 2021
2d48c59
chore: pre-release v6.11.0-pre
alexander-fenster Apr 9, 2021
c39195c
fix: rebuild
alexander-fenster Apr 9, 2021
e87b9d3
fix: fromObject should not initialize oneof members (#1597)
alexander-fenster Apr 29, 2021
703d0f6
chore: release v6.11.0
alexander-fenster Apr 29, 2021
9201173
chore: rebuild
alexander-fenster Apr 29, 2021
b37b296
feat: add --no-service option for pbjs static target (#1577)
mdouglass Apr 14, 2021
2de8d50
deps: set @types/node to >= (#1575)
indutny Apr 14, 2021
18fb310
chore: rebuild
alexander-fenster Apr 29, 2021
49b19fb
docs: update changelog
alexander-fenster Apr 29, 2021
ecd6a48
fix: parse.js "parent.add(oneof)“ error (#1602)
leon776 Apr 29, 2021
a2ccda1
chore: release v6.11.1
alexander-fenster Apr 29, 2021
da9fdd2
fix(types): bring back Field.rule to .d.ts
alexander-fenster Apr 30, 2021
d127871
fix: rebuild type, release v6.11.2
alexander-fenster Apr 30, 2021
37285d0
build: configure backports
bcoe May 20, 2022
7afd0a3
build: configure 6.x as default branch
bcoe May 20, 2022
b5f1391
fix: do not let setProperty change the prototype (#1731)
alexander-fenster May 20, 2022
a8681ce
fix(deps): use eslint 8.x (#1728)
alexander-fenster May 19, 2022
b2c6a5c
build: run tests if ci label added (#1734)
bcoe May 20, 2022
c2c17ae
build: publish to main
bcoe May 20, 2022
b130dfd
chore(6.x): release 6.11.3 (#1737)
github-actions[bot] May 20, 2022
26d0135
Support parsing of complex options
NikhilVerma Jun 14, 2022
7ef4203
Use readValue to read the proto value and add better example
NikhilVerma Jun 14, 2022
7186394
Fix lint issues
NikhilVerma Jun 18, 2022
20cab5b
Merge branch 'master' into comp_options
alexander-fenster Jul 7, 2022
06a993d
fix: rollback files
alexander-fenster Jul 7, 2022
108fa38
Merge branch 'master' into comp_options
alexander-fenster Jul 7, 2022
bda81ee
Re-do parse logic to take arrays into account and make it simpler
NikhilVerma Jul 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 37 additions & 29 deletions src/parse.js
Expand Up @@ -587,49 +587,57 @@ function parse(source, root, options) {
}

function parseOptionValue(parent, name) {
if (skip("{", true)) { // { a: "foo" b { c: "bar" } }
var result = {};
// { a: "foo" b { c: "bar" } }
if (skip("{", true)) {
var objectResult = {};

while (!skip("}", true)) {
/* istanbul ignore if */
if (!nameRe.test(token = next()))
if (!nameRe.test(token = next())) {
throw illegal(token, "name");
}

var value;
var propName = token;

skip(":", true);

if (peek() === "{")
value = parseOptionValue(parent, name + "." + token);
else {
skip(":");
if (peek() === "{")
value = parseOptionValue(parent, name + "." + token);
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 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);
}
} else {
value = readValue(true);
setOption(parent, name + "." + token, value);
}
var prevValue = result[propName];

var prevValue = objectResult[propName];

if (prevValue)
value = [].concat(prevValue).concat(value);
result[propName] = value;
skip(",", true) || skip(";", true);

objectResult[propName] = value;

// Semicolons and commas can be optional
skip(",", true);
skip(";", true);
}
return result;

return objectResult;
}

var simpleValue = readValue(true);
Expand Down
54 changes: 54 additions & 0 deletions tests/comp_options.js
@@ -0,0 +1,54 @@
var tape = require("tape");

var protobuf = require("..");

var proto = `
syntax = "proto3";

option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
info: {
title: "Some info";
version: "0";
};
host: "some.host";
};

message Message {
int32 regular_int32 = 1;
optional int32 optional_int32 = 2;
oneof _oneof_int32 {
int32 oneof_int32 = 3;
}
actionType action = 4 [ (validate.rules).enum = {
defined_only: true,
not_in: [ 0 ],
in: ["google","github","azuread"]
} ];
}
`;

tape.test("complex options", function (test) {
var root = protobuf.parse(proto).root;

test.deepEqual(root.parsedOptions[0], {
"(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger)": {
info: {
title: "Some info",
version: "0",
},
host: "some.host",
},
});

test.deepEqual(root.Message.fields.action.parsedOptions[0], {
"(validate.rules)": {
enum: {
defined_only: true,
not_in: [0],
in: ["google", "github", "azuread"],
},
},
});

test.end();
});