From 7b2988978c4f4cd8c471835c9e87613f9337e6cb Mon Sep 17 00:00:00 2001 From: ab Date: Tue, 11 Feb 2020 23:15:35 +0800 Subject: [PATCH 1/4] enum valuesOptions support --- src/enum.js | 31 ++++++++++++++++++++++++------- src/parse.js | 11 +++++++++-- tests/comp_options-textformat.js | 9 +++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/enum.js b/src/enum.js index b11014be3..eaff787f0 100644 --- a/src/enum.js +++ b/src/enum.js @@ -18,8 +18,9 @@ var Namespace = require("./namespace"), * @param {Object.} [options] Declared options * @param {string} [comment] The comment for this enum * @param {Object.} [comments] The value comments for this enum + * @param {Object.>|undefined} [valuesOptions] The value options for this enum */ -function Enum(name, values, options, comment, comments) { +function Enum(name, values, options, comment, comments, valuesOptions) { ReflectionObject.call(this, name, options); if (values && typeof values !== "object") @@ -49,6 +50,12 @@ function Enum(name, values, options, comment, comments) { */ this.comments = comments || {}; + /** + * Values options, if any + * @type {Object>|undefined} + */ + this.valuesOptions = valuesOptions; + /** * Reserved ranges, if any. * @type {Array.} @@ -93,11 +100,12 @@ Enum.fromJSON = function fromJSON(name, json) { Enum.prototype.toJSON = function toJSON(toJSONOptions) { var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false; return util.toObject([ - "options" , this.options, - "values" , this.values, - "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined, - "comment" , keepComments ? this.comment : undefined, - "comments" , keepComments ? this.comments : undefined + "options" , this.options, + "valuesOptions" , this.valuesOptions, + "values" , this.values, + "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined, + "comment" , keepComments ? this.comment : undefined, + "comments" , keepComments ? this.comments : undefined ]); }; @@ -106,11 +114,12 @@ Enum.prototype.toJSON = function toJSON(toJSONOptions) { * @param {string} name Value name * @param {number} id Value id * @param {string} [comment] Comment, if any + * @param {Object.} [options] Options, if any * @returns {Enum} `this` * @throws {TypeError} If arguments are invalid * @throws {Error} If there is already a value with this name or id */ -Enum.prototype.add = function add(name, id, comment) { +Enum.prototype.add = function add(name, id, comment, options) { // utilized by the parser but not by .fromJSON if (!util.isString(name)) @@ -135,6 +144,12 @@ Enum.prototype.add = function add(name, id, comment) { } else this.valuesById[this.values[name] = id] = name; + if (options) { + if (this.valuesOptions === undefined) + this.valuesOptions = {}; + this.valuesOptions[name] = options || null; + } + this.comments[name] = comment || null; return this; }; @@ -158,6 +173,8 @@ Enum.prototype.remove = function remove(name) { delete this.valuesById[val]; delete this.values[name]; delete this.comments[name]; + if (this.valuesOptions) + delete this.valuesOptions[name]; return this; }; diff --git a/src/parse.js b/src/parse.js index 47f94e494..be2a0ba13 100644 --- a/src/parse.js +++ b/src/parse.js @@ -516,7 +516,14 @@ function parse(source, root, options) { skip("="); var value = parseId(next(), true), - dummy = {}; + dummy = { + options: undefined + }; + dummy.setOption = function(name, value) { + if (this.options === undefined) + this.options = {}; + this.options[name] = value; + }; ifBlock(dummy, function parseEnumValue_block(token) { /* istanbul ignore else */ @@ -529,7 +536,7 @@ function parse(source, root, options) { }, function parseEnumValue_line() { parseInlineOptions(dummy); // skip }); - parent.add(token, value, dummy.comment); + parent.add(token, value, dummy.comment, dummy.options); } function parseOption(parent, token) { diff --git a/tests/comp_options-textformat.js b/tests/comp_options-textformat.js index b7788e48f..7d4dd5a3e 100644 --- a/tests/comp_options-textformat.js +++ b/tests/comp_options-textformat.js @@ -11,10 +11,16 @@ message MyOptions {\ extend google.protobuf.FieldOptions {\ MyOptions my_options = 50000;\ }\ +extend google.protobuf.EnumValueOptions {\ + MyOptions my_value_option = 50001;\ +}\ message Test {\ string value = 1 [(my_options) = { a: \"foo\" b: \"bar\" }];\ string value2 = 2 [(my_options) = { a: \"foo\" b { c: \"bar\" } }];\ string value3 = 3 [(my_options) = { a: \"foo\", b: \"bar\" }];\ +}\ +enum TestEnum {\ + TEST_ITEM = 0 [(my_value_option) = { a: \"foo\", b: \"bar\" }];\ }"; tape.test("options in textformat", function(test) { @@ -24,4 +30,7 @@ tape.test("options in textformat", function(test) { test.same(Test.fields.value2.options, { "(my_options).a": "foo", "(my_options).b.c": "bar" }, "should parse correctly when nested"); test.same(Test.fields.value3.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly when comma-separated"); test.end(); + + var TestEnum = root.lookup("TestEnum"); + test.same(TestEnum.valuesOptions["TEST_ITEM"], { "(my_value_option).a": "foo", "(my_value_option).b": "bar" }, "should parse correctly in enum"); }); From 2741a1bd2b2a6ac12d5be04549205add7bea8ebe Mon Sep 17 00:00:00 2001 From: ab Date: Tue, 11 Feb 2020 23:24:42 +0800 Subject: [PATCH 2/4] update tests a little, fix api docs --- src/enum.js | 2 +- tests/api_enum.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/enum.js b/src/enum.js index eaff787f0..1c016209f 100644 --- a/src/enum.js +++ b/src/enum.js @@ -114,7 +114,7 @@ Enum.prototype.toJSON = function toJSON(toJSONOptions) { * @param {string} name Value name * @param {number} id Value id * @param {string} [comment] Comment, if any - * @param {Object.} [options] Options, if any + * @param {Object.|undefined} [options] Options, if any * @returns {Enum} `this` * @throws {TypeError} If arguments are invalid * @throws {Error} If there is already a value with this name or id diff --git a/tests/api_enum.js b/tests/api_enum.js index 705491331..051bc51c4 100644 --- a/tests/api_enum.js +++ b/tests/api_enum.js @@ -80,5 +80,12 @@ tape.test("reflected enums", function(test) { b: 0 }); + enm.add('e', 4, undefined, {'(test_option)': 'test_value'}); + test.same( enm.valuesOptions, { + 'e': { + '(test_option)': 'test_value' + } + }); + test.end(); }); From c8252e5422bcbbeb47399b6d231987c5ac0a102d Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 17 Aug 2022 10:04:14 -0700 Subject: [PATCH 3/4] tests: reverting bad conflict resolution --- tests/comp_options-textformat.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/comp_options-textformat.js b/tests/comp_options-textformat.js index eec4f3183..e001d270a 100644 --- a/tests/comp_options-textformat.js +++ b/tests/comp_options-textformat.js @@ -18,6 +18,7 @@ message Test {\ string value = 1 [(my_options) = { a: \"foo\" b: \"bar\" }];\ string value2 = 2 [(my_options) = { a: \"foo\" b { c: \"bar\" } }];\ string value3 = 3 [(my_options) = { a: \"foo\", b: \"bar\" }];\ + string value4 = 4 [(my_options) = { a: \"foo\"; b: \"bar\" }];\ }\ enum TestEnum {\ TEST_ITEM = 0 [(my_value_option) = { a: \"foo\", b: \"bar\" }];\ From 50bbd24dad2246bdfef10bf4c27f57fb75e2714d Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 17 Aug 2022 10:14:58 -0700 Subject: [PATCH 4/4] test: put test.end() in its proper place --- tests/comp_options-textformat.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/comp_options-textformat.js b/tests/comp_options-textformat.js index e001d270a..83e6ac08c 100644 --- a/tests/comp_options-textformat.js +++ b/tests/comp_options-textformat.js @@ -27,12 +27,11 @@ enum TestEnum {\ tape.test("options in textformat", function(test) { var root = protobuf.parse(proto).root; var Test = root.lookup("Test"); + var TestEnum = root.lookup("TestEnum"); 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.same(Test.fields.value3.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly when comma-separated"); test.same(Test.fields.value4.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly when semicolon-separated"); - test.end(); - - var TestEnum = root.lookup("TestEnum"); test.same(TestEnum.valuesOptions["TEST_ITEM"], { "(my_value_option).a": "foo", "(my_value_option).b": "bar" }, "should parse correctly in enum"); + test.end(); });