Skip to content

Commit

Permalink
Fixed: Use common utility for virtual oneof getters and setters in bo…
Browse files Browse the repository at this point in the history
…th reflection and static code, see #644
  • Loading branch information
dcodeIO committed Jan 14, 2017
1 parent d4272db commit c7e14b1
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 136 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protobuf.load("bundle.json", function(err, root) {
});
```

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

### Generating TypeScript definitions from static modules

Expand Down
43 changes: 18 additions & 25 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ function static_target(root, options, callback) {
push("// Lazily resolved type references");
push("var $lazyTypes = [];");
push("");
if (config.comments)
push("// Exported root namespace");
if (config.comments) {
if (root.comment)
pushComment("@fileoverview " + root.comment);
else
push("// Exported root namespace");
}
push("var $root = {};");
buildNamespace(null, root);
push("");
Expand Down Expand Up @@ -76,7 +80,7 @@ function pushComment(lines) {
return;
var split = [];
for (var i = 0; i < lines.length; ++i)
if (lines[i] !== null)
if (lines[i] !== null && lines[i].substring(0, 8) !== "@exclude")
Array.prototype.push.apply(split, lines[i].split(/\r?\n/g));
push("/**");
split.forEach(function(line) {
Expand Down Expand Up @@ -126,7 +130,7 @@ function buildNamespace(ref, ns) {
else if (ns.name !== "") {
push("");
pushComment([
"Namespace " + ns.name + ".",
ns.comment || "Namespace " + ns.name + ".",
"@exports " + ns.fullName.substring(1),
"@namespace"
]);
Expand Down Expand Up @@ -301,7 +305,15 @@ function buildType(ref, type) {
});

// virtual oneof fields
var firstOneOf = true;;
type.oneofsArray.forEach(function(oneof) {
if (firstOneOf) {
firstOneOf = false;
push("");
if (config.comments)
push("// OneOf field names bound to virtual getters and setters");
push("var $oneOfFields;");
}
oneof.resolve();
push("");
pushComment([
Expand All @@ -311,27 +323,8 @@ function buildType(ref, type) {
]);
push("Object.defineProperty($prototype, " + JSON.stringify(oneof.name) +", {");
++indent;
push("get: function() {");
++indent;
oneof.oneof.forEach(function(name) {
push("if (this[" + JSON.stringify(name) + "] !== undefined)");
++indent;
push("return " + JSON.stringify(name) + ";");
--indent;
});
push("return undefined;");
--indent;
push("},");
push("set: function(value) {");
++indent;
oneof.oneof.forEach(function(name) {
push("if (value !== " + JSON.stringify(name) + ")");
++indent;
push("delete this[" + JSON.stringify(name) + "];");
--indent;
});
--indent;
push("}");
push("get: $protobuf.util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
push("set: $protobuf.util.oneOfSetter($oneOfFields)");
--indent;
push("});");
});
Expand Down
64 changes: 50 additions & 14 deletions dist/noparse/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/noparse/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/noparse/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/noparse/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/noparse/protobuf.min.js.map

Large diffs are not rendered by default.

64 changes: 50 additions & 14 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

44 changes: 43 additions & 1 deletion dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,9 @@ export class Root extends NamespaceBase {
load(filename: (string|string[]), options?: ParseOptions): Promise<Root>;

/**
* Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace.
* Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
* @name Root#loadSync
* @function
* @param {string|string[]} filename Names of one or multiple files to load
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {Root} Root namespace
Expand Down Expand Up @@ -2358,6 +2360,20 @@ export namespace util {
*/
function merge(dst: { [k: string]: any }, src: { [k: string]: any }, ifNotSet?: boolean): { [k: string]: any };

/**
* Builds a getter for a oneof's present field name.
* @param {string[]} fieldNames Field names
* @returns {function():string|undefined} Unbound getter
*/
function oneOfGetter(fieldNames: string[]): () => any;

/**
* Builds a setter for a oneof's present field name.
* @param {string[]} fieldNames Field names
* @returns {function(?string):undefined} Unbound setter
*/
function oneOfSetter(fieldNames: string[]): () => any;

/**
* A minimal UTF8 implementation for number arrays.
* @memberof util
Expand Down
14 changes: 2 additions & 12 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,8 @@ function create(type, ctor) {
// Messages have non-enumerable getters and setters for each virtual oneof field
type.oneofsArray.forEach(function(oneof) {
Object.defineProperty(prototype, oneof.resolve().name, {
get: function() {
// > If the parser encounters multiple members of the same oneof on the wire, only the last member seen is used in the parsed message.
for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
if (oneof.oneof.indexOf(keys[i]) > -1)
return keys[i];
return undefined;
},
set: function(value) {
for (var keys = oneof.oneof, i = 0; i < keys.length; ++i)
if (keys[i] !== value)
delete this[keys[i]];
}
get: util.oneOfGetter(oneof.oneof),
set: util.oneOfSetter(oneof.oneof)
});
});

Expand Down
Loading

0 comments on commit c7e14b1

Please sign in to comment.