Skip to content

Commit d6eb4e0

Browse files
Rework feature resolution to not rely on resolved types
1 parent 9462954 commit d6eb4e0

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

bench/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ newSuite("load")
123123
.add("sync", function() {
124124
protobuf.loadSync("bench/data/bench.proto");
125125
})
126-
.run()
126+
.run();
127127

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)