Skip to content

Commit 6f7d540

Browse files
committed
chore: respond to comments
1 parent a9eebac commit 6f7d540

File tree

7 files changed

+176
-166
lines changed

7 files changed

+176
-166
lines changed

src/enum.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,14 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
8989
* @returns {Enum} `this`
9090
*/
9191
Enum.prototype.resolve = function resolve() {
92-
93-
if (this.resolved)
94-
return this;
92+
ReflectionObject.prototype.resolve.call(this);
9593

9694
for (var key of Object.keys(this._valuesProtoFeatures)) {
97-
98-
if (this.parent) {
99-
var parentFeaturesCopy = Object.assign({}, this.parent._features);
100-
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._valuesProtoFeatures[key] || {});
101-
} else {
102-
this._valuesFeatures[key] = Object.assign({}, this._valuesProtoFeatures[key]);
103-
}
95+
var parentFeaturesCopy = Object.assign({}, this._features);
96+
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._valuesProtoFeatures[key] || {});
10497
}
105-
return ReflectionObject.prototype.resolve.call(this);
98+
99+
return this;
106100
};
107101

108102

src/field.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ Field.prototype.resolve = function resolve() {
319319
if (this.parent instanceof Type)
320320
this.parent.ctor.prototype[this.name] = this.defaultValue;
321321

322+
323+
if (this.parent) {
324+
var parentFeaturesCopy = Object.assign({}, this.parent._features);
325+
this._features = Object.assign(parentFeaturesCopy, this._protoFeatures || {});
326+
} else {
327+
this._features = Object.assign({}, this._protoFeatures);
328+
}
322329
return ReflectionObject.prototype.resolve.call(this);
323330
};
324331

src/object.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = ReflectionObject;
33

44
ReflectionObject.className = "ReflectionObject";
55

6+
const OneOf = require("./oneof");
67
var util = require("./util");
78

89
var Root; // cyclic
@@ -162,10 +163,10 @@ ReflectionObject.prototype.onRemove = function onRemove(parent) {
162163
ReflectionObject.prototype.resolve = function resolve() {
163164
if (this.resolved)
164165
return this;
165-
if (this instanceof Root || this.parent && this.parent.resolved)
166+
if (this instanceof Root || this.parent && this.parent.resolved) {
166167
this._resolveFeatures();
167-
if (this.root instanceof Root)
168168
this.resolved = true;
169+
}
169170
return this;
170171
};
171172

@@ -188,6 +189,11 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() {
188189

189190
if (this instanceof Root) {
190191
this._features = Object.assign(defaults, this._protoFeatures || {});
192+
// fields in Oneofs aren't actually children of them, so we have to
193+
// special-case it
194+
} else if (this.partOf instanceof OneOf) {
195+
var lexicalParentFeaturesCopy = Object.assign({}, this.partOf._features);
196+
this._features = Object.assign(lexicalParentFeaturesCopy, this._protoFeatures || {});
191197
} else if (this.parent) {
192198
var parentFeaturesCopy = Object.assign({}, this.parent._features);
193199
this._features = Object.assign(parentFeaturesCopy, this._protoFeatures || {});

src/root.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,26 @@ Root.prototype.load = function load(filename, options, callback) {
8888
options = undefined;
8989
}
9090
var self = this;
91-
if (!callback)
91+
if (!callback) {
9292
return util.asPromise(load, self, filename, options);
93+
}
9394

9495
var sync = callback === SYNC; // undocumented
9596

9697
// Finishes loading by calling the callback (exactly once)
9798
function finish(err, root) {
9899
/* istanbul ignore if */
99-
if (!callback)
100+
if (!callback) {
100101
return;
101-
if (sync)
102+
}
103+
if (sync) {
102104
throw err;
105+
}
103106
var cb = callback;
104107
callback = null;
108+
if (root) {
109+
root.resolveAll();
110+
}
105111
cb(err, root);
106112
}
107113

@@ -139,24 +145,26 @@ Root.prototype.load = function load(filename, options, callback) {
139145
} catch (err) {
140146
finish(err);
141147
}
142-
if (!sync && !queued)
148+
if (!sync && !queued) {
143149
finish(null, self); // only once anyway
150+
}
144151
}
145152

146153
// Fetches a single file
147154
function fetch(filename, weak) {
148155
filename = getBundledFileName(filename) || filename;
149156

150157
// Skip if already loaded / attempted
151-
if (self.files.indexOf(filename) > -1)
158+
if (self.files.indexOf(filename) > -1) {
152159
return;
160+
}
153161
self.files.push(filename);
154162

155163
// Shortcut bundled definitions
156164
if (filename in common) {
157-
if (sync)
165+
if (sync) {
158166
process(filename, common[filename]);
159-
else {
167+
} else {
160168
++queued;
161169
setTimeout(function() {
162170
--queued;
@@ -182,8 +190,9 @@ Root.prototype.load = function load(filename, options, callback) {
182190
self.fetch(filename, function(err, source) {
183191
--queued;
184192
/* istanbul ignore if */
185-
if (!callback)
193+
if (!callback) {
186194
return; // terminated meanwhile
195+
}
187196
if (err) {
188197
/* istanbul ignore else */
189198
if (!weak)
@@ -200,17 +209,21 @@ Root.prototype.load = function load(filename, options, callback) {
200209

201210
// Assembling the root namespace doesn't require working type
202211
// references anymore, so we can load everything in parallel
203-
if (util.isString(filename))
212+
if (util.isString(filename)) {
204213
filename = [ filename ];
214+
}
205215
for (var i = 0, resolved; i < filename.length; ++i)
206216
if (resolved = self.resolvePath("", filename[i]))
207217
fetch(resolved);
208218
self.resolveAll();
209-
if (sync)
219+
if (sync) {
210220
return self;
211-
if (!queued)
221+
}
222+
if (!queued) {
212223
finish(null, self);
213-
return undefined;
224+
}
225+
226+
return self;
214227
};
215228
// function load(filename:string, options:IParseOptions, callback:LoadCallback):undefined
216229

src/type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
300300
*/
301301
Type.prototype.resolveAll = function resolveAll() {
302302
Namespace.prototype.resolveAll.call(this);
303-
var fields = this.fieldsArray, i = 0;
304-
while (i < fields.length)
305-
fields[i++].resolve();
306303
var oneofs = this.oneofsArray; i = 0;
307304
while (i < oneofs.length)
308305
oneofs[i++].resolve();
306+
var fields = this.fieldsArray, i = 0;
307+
while (i < fields.length)
308+
fields[i++].resolve();
309309
return this;
310310
};
311311

tests/data/feature-resolution.proto

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
edition = "2023";
2+
3+
option features.amazing_feature = A;
4+
option (mo_single_msg).nested.value = "x";
5+
service MyService {
6+
option features.amazing_feature = E;
7+
message MyRequest {};
8+
message MyResponse {};
9+
rpc MyMethod (MyRequest) returns (MyResponse) {
10+
option features.amazing_feature = L;
11+
};
12+
}
13+
14+
message Message {
15+
option features.amazing_feature = B;
16+
17+
string string_val = 1;
18+
string string_repeated = 2 [features.amazing_feature = F];
19+
20+
uint64 uint64_val = 3;
21+
uint64 uint64_repeated = 4;
22+
23+
bytes bytes_val = 5;
24+
bytes bytes_repeated = 6;
25+
26+
SomeEnum enum_val = 7;
27+
SomeEnum enum_repeated = 8;
28+
29+
extensions 10 to 100;
30+
extend Message {
31+
int32 bar = 10 [features.amazing_feature = I];
32+
}
33+
34+
message Nested {
35+
option features.amazing_feature = H;
36+
int64 count = 9;
37+
}
38+
39+
enum SomeEnumInMessage {
40+
option features.amazing_feature = G;
41+
ONE = 11;
42+
TWO = 12;
43+
}
44+
45+
oneof SomeOneOf {
46+
option features.amazing_feature = J;
47+
int32 a = 13;
48+
string b = 14;
49+
}
50+
51+
map<string,int64> int64_map = 15;
52+
}
53+
54+
extend Message {
55+
int32 bar = 16 [features.amazing_feature = D];
56+
}
57+
58+
enum SomeEnum {
59+
option features.amazing_feature = C;
60+
ONE = 1 [features.amazing_feature = K];
61+
TWO = 2;
62+
}

0 commit comments

Comments
 (0)