From aff21a71e6bd949647b1b7721ea4e1fe16bcd933 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sun, 11 Dec 2016 14:13:39 +0100 Subject: [PATCH] static target: Basic support for oneof fields, see #542 --- cli/pbjs.js | 2 +- cli/targets/static.js | 48 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cli/pbjs.js b/cli/pbjs.js index a6c7b9a49..e4e4dc09e 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -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")); diff --git a/cli/targets/static.js b/cli/targets/static.js index 57fc65fb4..0d43e15e8 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -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(); @@ -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