Skip to content

Commit c7e14b1

Browse files
committed
Fixed: Use common utility for virtual oneof getters and setters in both reflection and static code, see #644
1 parent d4272db commit c7e14b1

23 files changed

+258
-136
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ protobuf.load("bundle.json", function(err, root) {
307307
});
308308
```
309309

310-
**ProTip!** Documenting your .proto files with `/** ... */`-blocks translates to generated static code.
310+
**ProTip!** Documenting your .proto files with `/** ... */`-blocks or (trailing) `/// ...` lines translates to generated static code.
311311

312312
### Generating TypeScript definitions from static modules
313313

cli/targets/static.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ function static_target(root, options, callback) {
2525
push("// Lazily resolved type references");
2626
push("var $lazyTypes = [];");
2727
push("");
28-
if (config.comments)
29-
push("// Exported root namespace");
28+
if (config.comments) {
29+
if (root.comment)
30+
pushComment("@fileoverview " + root.comment);
31+
else
32+
push("// Exported root namespace");
33+
}
3034
push("var $root = {};");
3135
buildNamespace(null, root);
3236
push("");
@@ -76,7 +80,7 @@ function pushComment(lines) {
7680
return;
7781
var split = [];
7882
for (var i = 0; i < lines.length; ++i)
79-
if (lines[i] !== null)
83+
if (lines[i] !== null && lines[i].substring(0, 8) !== "@exclude")
8084
Array.prototype.push.apply(split, lines[i].split(/\r?\n/g));
8185
push("/**");
8286
split.forEach(function(line) {
@@ -126,7 +130,7 @@ function buildNamespace(ref, ns) {
126130
else if (ns.name !== "") {
127131
push("");
128132
pushComment([
129-
"Namespace " + ns.name + ".",
133+
ns.comment || "Namespace " + ns.name + ".",
130134
"@exports " + ns.fullName.substring(1),
131135
"@namespace"
132136
]);
@@ -301,7 +305,15 @@ function buildType(ref, type) {
301305
});
302306

303307
// virtual oneof fields
308+
var firstOneOf = true;;
304309
type.oneofsArray.forEach(function(oneof) {
310+
if (firstOneOf) {
311+
firstOneOf = false;
312+
push("");
313+
if (config.comments)
314+
push("// OneOf field names bound to virtual getters and setters");
315+
push("var $oneOfFields;");
316+
}
305317
oneof.resolve();
306318
push("");
307319
pushComment([
@@ -311,27 +323,8 @@ function buildType(ref, type) {
311323
]);
312324
push("Object.defineProperty($prototype, " + JSON.stringify(oneof.name) +", {");
313325
++indent;
314-
push("get: function() {");
315-
++indent;
316-
oneof.oneof.forEach(function(name) {
317-
push("if (this[" + JSON.stringify(name) + "] !== undefined)");
318-
++indent;
319-
push("return " + JSON.stringify(name) + ";");
320-
--indent;
321-
});
322-
push("return undefined;");
323-
--indent;
324-
push("},");
325-
push("set: function(value) {");
326-
++indent;
327-
oneof.oneof.forEach(function(name) {
328-
push("if (value !== " + JSON.stringify(name) + ")");
329-
++indent;
330-
push("delete this[" + JSON.stringify(name) + "];");
331-
--indent;
332-
});
333-
--indent;
334-
push("}");
326+
push("get: $protobuf.util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
327+
push("set: $protobuf.util.oneOfSetter($oneOfFields)");
335328
--indent;
336329
push("});");
337330
});

dist/noparse/protobuf.js

Lines changed: 50 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js.gz

65 Bytes
Binary file not shown.

dist/noparse/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js

Lines changed: 50 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)