Skip to content

Commit c7cee06

Browse files
committed
add default feature resolution at the end
1 parent 062caa5 commit c7cee06

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

src/object.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ var util = require("./util");
77

88
var Root; // cyclic
99

10+
var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};
11+
var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"};
12+
var proto3Defaults = {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};
13+
1014
/**
1115
* Constructs a new reflection object instance.
1216
* @classdesc Base class of all reflection objects.
@@ -168,18 +172,30 @@ ReflectionObject.prototype.resolve = function resolve() {
168172
* @returns {undefined}
169173
*/
170174
ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() {
175+
var defaults = {};
176+
177+
if (this.root.getOption('syntax') === 'proto2') {
178+
defaults = Object.assign({}, proto2Defaults);
179+
} else if (this.root.getOption('syntax') === 'proto3') {
180+
defaults = Object.assign({}, proto3Defaults)
181+
} else if (this.root.getOption('edition') === '2023') {
182+
defaults = Object.assign({}, editions2023Defaults);
183+
}
184+
171185
if (this.parent) {
172186
// This is an annoying workaround since we can't use the spread operator
173187
// (Breaks the bundler and eslint)
174188
// If we don't create a shallow copy, we end up also altering the parent's
175189
// features
176-
var parentFeatures = Object.assign({}, this.parent._proto_features);
177-
this._features = Object.assign(parentFeatures, this._proto_features || {});
190+
var parentFeaturesMerged = Object.assign(defaults, this.parent._proto_features);
191+
this._features = Object.assign(parentFeaturesMerged, this._proto_features || {});
192+
this._proto_features = this._features;
178193
this.parent._resolveFeatures();
179194
} else {
180-
this._features = Object.assign({}, this._proto_features);
195+
this._features = Object.assign(defaults, this._proto_features || {});
181196
}
182197
this._proto_features = this._features;
198+
183199
};
184200

185201
/**

src/parse.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ var base10Re = /^[1-9][0-9]*$/,
2727
nameRe = /^[a-zA-Z_][a-zA-Z_0-9]*$/,
2828
typeRefRe = /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/;
2929

30-
var editions2023Defaults = {features: {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}};
31-
var proto2Defaults = {features: {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"}};
32-
var proto3Defaults = {features: {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}};
33-
3430
/**
3531
* Result object returned from {@link parse}.
3632
* @interface IParserResult
@@ -278,16 +274,6 @@ function parse(source, root, options) {
278274
// Otherwise the meaning is ambiguous between proto2 and proto3
279275
root.setOption("syntax", syntax);
280276

281-
if (isProto3) {
282-
for (var proto3Key of Object.keys(proto3Defaults)) {
283-
setParsedOption(root, proto3Key, proto3Defaults[proto3Key]);
284-
}
285-
} else {
286-
for (var proto2Key of Object.keys(proto2Defaults)) {
287-
setParsedOption(root, proto2Key, proto2Defaults[proto2Key]);
288-
}
289-
}
290-
291277
skip(";");
292278
}
293279

@@ -302,9 +288,6 @@ function parse(source, root, options) {
302288

303289
root.setOption("edition", edition);
304290

305-
for (var key of Object.keys(editions2023Defaults)) {
306-
setParsedOption(root, key, editions2023Defaults[key]);
307-
}
308291
skip(";");
309292
}
310293

tests/comp_options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ message Message {
3030
tape.test("complex options", function (test) {
3131
var root = protobuf.parse(proto).root;
3232

33-
test.deepEqual(root.parsedOptions[1], {
33+
test.deepEqual(root.parsedOptions[0], {
3434
"(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger)": {
3535
info: {
3636
title: "Some info",

tests/feature_resolution_editions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ service MyService {
193193
};
194194
}
195195
`
196-
tape.test("feautre resolution defaults", function(test) {
196+
tape.test("feature resolution defaults", function(test) {
197197
var rootEditions = protobuf.parse(protoEditions2023).root;
198198
rootEditions.resolveAll();
199199
test.same(rootEditions._features, editions2023Defaults);

0 commit comments

Comments
 (0)