Skip to content

Commit

Permalink
static target: Basic support for oneof fields, see #542
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 11, 2016
1 parent 691231f commit aff21a7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/pbjs.js
Expand Up @@ -49,7 +49,7 @@ exports.main = function(args) {
"",
" -w, --wrap Specifies an alternative wrapper for *-module targets.",
"",
" -r, --root Specifies an alternative root name for *-module targets.",
" -r, --root Specifies an alternative protobuf.roots name for *-module targets.",
"",
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
].join("\n"));
Expand Down
48 changes: 44 additions & 4 deletions cli/targets/static.js
Expand Up @@ -166,6 +166,10 @@ function buildType(ref, type) {
--indent;
push("}");

push("");
push("/** @alias " + fullName + ".prototype */");
push("var $prototype = " + name(type.name) + ".prototype;");

// default values
type.fieldsArray.forEach(function(field) {
field.resolve();
Expand Down Expand Up @@ -210,16 +214,52 @@ function buildType(ref, type) {
jsType = "Array.<" + jsType + ">";
push("");
pushComment([
field.name + ".",
type.name + " " + field.name + ".",
"@name " + fullName + "#" + name(field.name),
"@type {" + jsType + "}"
]);
if (Array.isArray(field.defaultValue)) {
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyArray;");
push("$prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyArray;");
} else if (util.isObject(field.defaultValue))
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyObject;");
push("$prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyObject;");
else
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = " + JSON.stringify(field.defaultValue) + ";");
push("$prototype[" + JSON.stringify(field.name) + "] = " + JSON.stringify(field.defaultValue) + ";");
});

// virtual oneof fields
type.oneofsArray.forEach(function(oneof) {
oneof.resolve();
push("");
pushComment([
type.name + " " + oneof.name + ".",
"@name " + fullName + "#" + name(oneof.name),
"@type {string|undefined}"
]);
push("$protobuf.util.prop($prototype, " + JSON.stringify(oneof.name) +", {");
++indent;
push("get: function getVirtual() {");
++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 setVirtual(value) {");
++indent;
oneof.oneof.forEach(function(name) {
push("if (value !== " + JSON.stringify(name) + ")");
++indent;
push("delete this[" + JSON.stringify(name) + "];");
--indent;
});
--indent;
push("}");
--indent;
push("});");
});

// #encode
Expand Down

0 comments on commit aff21a7

Please sign in to comment.