Skip to content

Commit

Permalink
feat: support parsing of complex options (#1744)
Browse files Browse the repository at this point in the history
* 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 <fenster@google.com>

* 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 <fenster@google.com>
Co-authored-by: Alexander Fenster <github@fenster.name>

* chore: rebuild

* docs: update changelog

* fix: parse.js "parent.add(oneof)“ error (#1602)

Co-authored-by: xiaoweili <xiaoweili@tencent.com>

* 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 <fenster@google.com>
Co-authored-by: Matthew Douglass <5410142+mdouglass@users.noreply.github.com>
Co-authored-by: Fedor Indutny <fedor.indutny@gmail.com>
Co-authored-by: Alexander Fenster <github@fenster.name>
Co-authored-by: leon <leon776@users.noreply.github.com>
Co-authored-by: xiaoweili <xiaoweili@tencent.com>
Co-authored-by: Benjamin Coe <bencoe@google.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
9 people committed Jul 7, 2022
1 parent 1d3c02a commit b1746a8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 29 deletions.
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();
});

0 comments on commit b1746a8

Please sign in to comment.