Skip to content

Commit d286a8e

Browse files
Rework feature resolution to not rely on resolved types
1 parent cb92b16 commit d286a8e

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

bench/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ newSuite("fromJSON")
102102
protobuf.Root.fromJSON(json).resolveAll();
103103
})
104104
.add("shared (unique)", function() {
105-
var jsonCopy = {
105+
var jsonCopy = {
106106
options: json.options,
107107
nested: {}
108108
};
109109
Object.keys(json).forEach(key => {
110-
jsonCopy.nested[key + fromJsonIndex] = json[key]
110+
jsonCopy.nested[key + fromJsonIndex] = json[key];
111111
});
112112
fromJsonIndex++;
113113

@@ -125,5 +125,5 @@ newSuite("load")
125125
.add("sync", function() {
126126
protobuf.loadSync("bench/data/bench.proto");
127127
})
128-
.run()
128+
.run();
129129

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
"build:bundle": "gulp --gulpfile scripts/gulpfile.js",
3434
"build:types": "node cli/bin/pbts --main --global protobuf --out index.d.ts src/ lib/aspromise/index.js lib/base64/index.js lib/codegen/index.js lib/eventemitter/index.js lib/float/index.js lib/fetch/index.js lib/inquire/index.js lib/path/index.js lib/pool/index.js lib/utf8/index.js",
3535
"changelog": "node scripts/changelog -w",
36-
"coverage": "nyc tape -r ./lib/tape-adapter tests/*.js tests/node/*.js",
36+
"coverage": "npm run coverage:test && npm run coverage:report",
37+
"coverage:test": "nyc --silent tape -r ./lib/tape-adapter tests/*.js tests/node/*.js",
38+
"coverage:report": "nyc report --reporter=lcov --reporter=text",
3739
"docs": "jsdoc -c config/jsdoc.json -R README.md --verbose --pedantic",
3840
"lint": "npm run lint:sources && npm run lint:types",
3941
"lint:sources": "eslint \"**/*.js\" -c config/eslint.json",

src/field.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,18 @@ Field.prototype._inferLegacyProtoFeatures = function _inferLegacyProtoFeatures(e
370370
}
371371

372372
var features = {};
373-
this.resolve();
373+
374374
if (this.rule === "required") {
375375
features.field_presence = "LEGACY_REQUIRED";
376376
}
377-
if (this.resolvedType instanceof Type && this.resolvedType.group) {
378-
features.message_encoding = "DELIMITED";
377+
if (this.parent && types.defaults[this.type] === undefined) {
378+
// We can't use resolvedType because types may not have been resolved yet. However,
379+
// legacy groups are always in the same scope as the field so we don't have to do a
380+
// full scan of the tree.
381+
var type = this.parent.get(this.type.split(".").pop());
382+
if (type && type instanceof Type && type.group) {
383+
features.message_encoding = "DELIMITED";
384+
}
379385
}
380386
if (this.getOption("packed") === true) {
381387
features.repeated_field_encoding = "PACKED";

src/root.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Root.fromJSON = function fromJSON(json, root) {
5151
root = new Root();
5252
if (json.options)
5353
root.setOptions(json.options);
54-
return root.addJSON(json.nested).resolveAll();
54+
return root.addJSON(json.nested)._resolveFeaturesRecursive();
5555
};
5656

5757
/**
@@ -99,6 +99,9 @@ Root.prototype.load = function load(filename, options, callback) {
9999

100100
// Finishes loading by calling the callback (exactly once)
101101
function finish(err, root) {
102+
if (root) {
103+
root._resolveFeaturesRecursive();
104+
}
102105
/* istanbul ignore if */
103106
if (!callback) {
104107
return;
@@ -108,9 +111,6 @@ Root.prototype.load = function load(filename, options, callback) {
108111
}
109112
var cb = callback;
110113
callback = null;
111-
if (root) {
112-
root.resolveAll();
113-
}
114114
cb(err, root);
115115
}
116116

@@ -218,8 +218,8 @@ Root.prototype.load = function load(filename, options, callback) {
218218
for (var i = 0, resolved; i < filename.length; ++i)
219219
if (resolved = self.resolvePath("", filename[i]))
220220
fetch(resolved);
221-
self.resolveAll();
222221
if (sync) {
222+
self._resolveFeaturesRecursive();
223223
return self;
224224
}
225225
if (!queued) {

0 commit comments

Comments
 (0)