Skip to content

Commit 1ca7586

Browse files
committed
respond to comments
1 parent c4c486d commit 1ca7586

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

src/object.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ function ReflectionObject(name, options) {
4444
/**
4545
* Resolved Features.
4646
*/
47-
this._features = null;
47+
this._features = {};
48+
49+
/**
50+
* Resolved Features.
51+
*/
52+
this._proto_features = null;
4853

4954
/**
5055
* Parent namespace.
@@ -152,7 +157,8 @@ ReflectionObject.prototype.resolve = function resolve() {
152157
if (this.resolved)
153158
return this;
154159
this._resolveFeatures();
155-
this.resolved = true;
160+
if (this.root instanceof Root || this.parent)
161+
this.resolved = true;
156162
return this;
157163
};
158164

@@ -166,10 +172,14 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() {
166172
// (Breaks the bundler and eslint)
167173
// If we don't create a shallow copy, we end up also altering the parent's
168174
// features
169-
var parentFeatures = Object.assign({}, this.parent._features);
170-
this._features = Object.assign(parentFeatures, this._features || {});
175+
var parentFeatures = Object.assign({}, this.parent._proto_features);
176+
this._features = Object.assign(parentFeatures, this._proto_features || {});
177+
// this._proto_features = this._features;
171178
this.parent._resolveFeatures();
179+
} else {
180+
this._features = Object.assign({}, this._proto_features);
172181
}
182+
this._proto_features = this._features;
173183
};
174184

175185
/**
@@ -207,7 +217,7 @@ ReflectionObject.prototype.setParsedOption = function setParsedOption(name, valu
207217
if (!this.parsedOptions) {
208218
this.parsedOptions = [];
209219
}
210-
var isFeature = /features/.test(name);
220+
var isFeature = /^features/.test(name);
211221
var parsedOptions = this.parsedOptions;
212222
if (propName) {
213223
// If setting a sub property of an option then try to merge it
@@ -236,8 +246,9 @@ ReflectionObject.prototype.setParsedOption = function setParsedOption(name, valu
236246

237247
if (isFeature) {
238248
var features = parsedOptions.find(x => {return Object.prototype.hasOwnProperty.call(x, "features");});
239-
this._features = features.features || {};
249+
this._proto_features = features.features || {};
240250
}
251+
241252
return this;
242253
};
243254

src/parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ function parse(source, root, options) {
370370
break;
371371

372372
case "required":
373-
case "repeated":
374373
if (edition)
375374
throw illegal(token);
375+
case "repeated":
376376
parseField(type, token);
377377
break;
378378

@@ -632,7 +632,7 @@ function parse(source, root, options) {
632632
dummy.setParsedOption = function(name, value, propName) {
633633
// In order to not change existing behavior, only calling
634634
// this for features
635-
if (/features/.test(name)) {
635+
if (/^features/.test(name)) {
636636
return ReflectionObject.prototype.setParsedOption.call(dummy, name, value, propName);
637637
}
638638
return undefined;

src/util.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ util.decorateEnum = function decorateEnum(object) {
171171
* @param {Object.<string,*>} dst Destination object
172172
* @param {string} path dot '.' delimited path of the property to set
173173
* @param {Object} value the value to set
174-
* @param {boolean} isFeature whether the option being set is a feature (matches feature regex)
174+
* @param {boolean} overWrite whether or not to concatenate the values into an array or overwrite
175175
* @returns {Object.<string,*>} Destination object
176176
*/
177-
util.setProperty = function setProperty(dst, path, value, isFeature) {
177+
util.setProperty = function setProperty(dst, path, value, overWrite) {
178178
function setProp(dst, path, value) {
179179
var part = path.shift();
180180
if (part === "__proto__" || part === "prototype") {
@@ -184,7 +184,7 @@ util.setProperty = function setProperty(dst, path, value, isFeature) {
184184
dst[part] = setProp(dst[part] || {}, path, value);
185185
} else {
186186
var prevValue = dst[part];
187-
if (prevValue && !isFeature)
187+
if (prevValue && !overWrite)
188188
value = [].concat(prevValue).concat(value);
189189
dst[part] = value;
190190
}

tests/api_object.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tape.test("reflection objects", function(test) {
1010
var obj = new protobuf.ReflectionObject("Test");
1111

1212
obj.resolve();
13+
test.equal(obj.resolved, false, "should not resolve when not part of a root");
1314

1415
obj.resolved = 2;
1516
obj.resolve();

tests/feature_resolution_editions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ message Message {
3131
`
3232
tape.test("feautre resolution defaults", function(test) {
3333
var rootEditions = protobuf.parse(protoEditions2023).root;
34+
rootEditions.resolveAll();
3435
test.same(rootEditions._features, editions2023Defaults);
3536

3637
var rootProto2 = protobuf.parse(proto2).root;
38+
rootProto2.resolveAll();
3739
test.same(rootProto2._features, proto2Defaults);
3840

3941
var rootProto3 = protobuf.parse(proto3).root;
42+
rootProto3.resolveAll();
4043
test.same(rootProto3._features, proto3Defaults);
4144

4245
test.end();
@@ -71,6 +74,7 @@ tape.test("feature resolution editions precedence", function(test) {
7174
protobuf.load("tests/data/feature-resolution.proto", function(err, root) {
7275
if (err)
7376
return test.fail(err.message);
77+
root.resolveAll();
7478
test.same(root._features.amazing_feature, 'A');
7579
test.same(root.lookup("Message")._features.amazing_feature, 'B')
7680
test.same(root.lookupEnum("SomeEnum")._features.amazing_feature, 'C')

0 commit comments

Comments
 (0)