From b1746a8c5e2b9c29644318090cc9e581f7e34b6e Mon Sep 17 00:00:00 2001 From: Nikhil Verma Date: Thu, 7 Jul 2022 15:18:25 +0530 Subject: [PATCH] feat: support parsing of complex options (#1744) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * 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 Co-authored-by: Alexander Fenster * chore: rebuild * docs: update changelog * fix: parse.js "parent.add(oneof)“ error (#1602) Co-authored-by: xiaoweili * 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 Co-authored-by: Matthew Douglass <5410142+mdouglass@users.noreply.github.com> Co-authored-by: Fedor Indutny Co-authored-by: Alexander Fenster Co-authored-by: leon Co-authored-by: xiaoweili Co-authored-by: Benjamin Coe Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/parse.js | 66 ++++++++++++++++++++++++------------------- tests/comp_options.js | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 tests/comp_options.js diff --git a/src/parse.js b/src/parse.js index 0e538be0b..26d923e3b 100644 --- a/src/parse.js +++ b/src/parse.js @@ -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); diff --git a/tests/comp_options.js b/tests/comp_options.js new file mode 100644 index 000000000..ec9888482 --- /dev/null +++ b/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(); +});