Skip to content

Commit

Permalink
add default feature resolution at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
sofisl committed Oct 2, 2024
1 parent 062caa5 commit c7cee06
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
22 changes: 19 additions & 3 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var util = require("./util");

var Root; // cyclic

var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};
var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"};
var proto3Defaults = {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};

/**
* Constructs a new reflection object instance.
* @classdesc Base class of all reflection objects.
Expand Down Expand Up @@ -168,18 +172,30 @@ ReflectionObject.prototype.resolve = function resolve() {
* @returns {undefined}
*/
ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() {
var defaults = {};

if (this.root.getOption('syntax') === 'proto2') {

Check warning on line 177 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use doublequote

Check warning on line 177 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use doublequote
defaults = Object.assign({}, proto2Defaults);
} else if (this.root.getOption('syntax') === 'proto3') {

Check warning on line 179 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use doublequote

Check warning on line 179 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use doublequote
defaults = Object.assign({}, proto3Defaults)

Check warning on line 180 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
} else if (this.root.getOption('edition') === '2023') {

Check warning on line 181 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use doublequote

Check warning on line 181 in src/object.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use doublequote
defaults = Object.assign({}, editions2023Defaults);
}

if (this.parent) {
// This is an annoying workaround since we can't use the spread operator
// (Breaks the bundler and eslint)
// If we don't create a shallow copy, we end up also altering the parent's
// features
var parentFeatures = Object.assign({}, this.parent._proto_features);
this._features = Object.assign(parentFeatures, this._proto_features || {});
var parentFeaturesMerged = Object.assign(defaults, this.parent._proto_features);
this._features = Object.assign(parentFeaturesMerged, this._proto_features || {});
this._proto_features = this._features;
this.parent._resolveFeatures();
} else {
this._features = Object.assign({}, this._proto_features);
this._features = Object.assign(defaults, this._proto_features || {});
}
this._proto_features = this._features;

};

/**
Expand Down
17 changes: 0 additions & 17 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ var base10Re = /^[1-9][0-9]*$/,
nameRe = /^[a-zA-Z_][a-zA-Z_0-9]*$/,
typeRefRe = /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/;

var editions2023Defaults = {features: {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}};
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"}};
var proto3Defaults = {features: {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}};

/**
* Result object returned from {@link parse}.
* @interface IParserResult
Expand Down Expand Up @@ -278,16 +274,6 @@ function parse(source, root, options) {
// Otherwise the meaning is ambiguous between proto2 and proto3
root.setOption("syntax", syntax);

if (isProto3) {
for (var proto3Key of Object.keys(proto3Defaults)) {
setParsedOption(root, proto3Key, proto3Defaults[proto3Key]);
}
} else {
for (var proto2Key of Object.keys(proto2Defaults)) {
setParsedOption(root, proto2Key, proto2Defaults[proto2Key]);
}
}

skip(";");
}

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

root.setOption("edition", edition);

for (var key of Object.keys(editions2023Defaults)) {
setParsedOption(root, key, editions2023Defaults[key]);
}
skip(";");
}

Expand Down
2 changes: 1 addition & 1 deletion tests/comp_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ message Message {
tape.test("complex options", function (test) {
var root = protobuf.parse(proto).root;

test.deepEqual(root.parsedOptions[1], {
test.deepEqual(root.parsedOptions[0], {
"(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger)": {
info: {
title: "Some info",
Expand Down
2 changes: 1 addition & 1 deletion tests/feature_resolution_editions.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ service MyService {
};
}
`
tape.test("feautre resolution defaults", function(test) {
tape.test("feature resolution defaults", function(test) {
var rootEditions = protobuf.parse(protoEditions2023).root;
rootEditions.resolveAll();
test.same(rootEditions._features, editions2023Defaults);
Expand Down

0 comments on commit c7cee06

Please sign in to comment.