Permalink
Browse files

Fixed: Use common utility for virtual oneof getters and setters in bo…

…th reflection and static code, see #644
  • Loading branch information...
dcodeIO committed Jan 14, 2017
1 parent d4272db commit c7e14b1d684aaba2080195cc83900288c5019bbc
@@ -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
@@ -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("");
@@ -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) {
@@ -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"
]);
@@ -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([
@@ -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("});");
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +65 Bytes (100%) dist/noparse/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +82 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +104 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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
@@ -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
@@ -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)
});
});
Oops, something went wrong.

0 comments on commit c7e14b1

Please sign in to comment.