Skip to content

Commit aff21a7

Browse files
committed
static target: Basic support for oneof fields, see #542
1 parent 691231f commit aff21a7

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

cli/pbjs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ exports.main = function(args) {
4949
"",
5050
" -w, --wrap Specifies an alternative wrapper for *-module targets.",
5151
"",
52-
" -r, --root Specifies an alternative root name for *-module targets.",
52+
" -r, --root Specifies an alternative protobuf.roots name for *-module targets.",
5353
"",
5454
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
5555
].join("\n"));

cli/targets/static.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ function buildType(ref, type) {
166166
--indent;
167167
push("}");
168168

169+
push("");
170+
push("/** @alias " + fullName + ".prototype */");
171+
push("var $prototype = " + name(type.name) + ".prototype;");
172+
169173
// default values
170174
type.fieldsArray.forEach(function(field) {
171175
field.resolve();
@@ -210,16 +214,52 @@ function buildType(ref, type) {
210214
jsType = "Array.<" + jsType + ">";
211215
push("");
212216
pushComment([
213-
field.name + ".",
217+
type.name + " " + field.name + ".",
214218
"@name " + fullName + "#" + name(field.name),
215219
"@type {" + jsType + "}"
216220
]);
217221
if (Array.isArray(field.defaultValue)) {
218-
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyArray;");
222+
push("$prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyArray;");
219223
} else if (util.isObject(field.defaultValue))
220-
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyObject;");
224+
push("$prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyObject;");
221225
else
222-
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = " + JSON.stringify(field.defaultValue) + ";");
226+
push("$prototype[" + JSON.stringify(field.name) + "] = " + JSON.stringify(field.defaultValue) + ";");
227+
});
228+
229+
// virtual oneof fields
230+
type.oneofsArray.forEach(function(oneof) {
231+
oneof.resolve();
232+
push("");
233+
pushComment([
234+
type.name + " " + oneof.name + ".",
235+
"@name " + fullName + "#" + name(oneof.name),
236+
"@type {string|undefined}"
237+
]);
238+
push("$protobuf.util.prop($prototype, " + JSON.stringify(oneof.name) +", {");
239+
++indent;
240+
push("get: function getVirtual() {");
241+
++indent;
242+
oneof.oneof.forEach(function(name) {
243+
push("if (this[" + JSON.stringify(name) + "] !== undefined)");
244+
++indent;
245+
push("return " + JSON.stringify(name) + ";");
246+
--indent;
247+
});
248+
push("return undefined;");
249+
--indent;
250+
push("},");
251+
push("set: function setVirtual(value) {");
252+
++indent;
253+
oneof.oneof.forEach(function(name) {
254+
push("if (value !== " + JSON.stringify(name) + ")");
255+
++indent;
256+
push("delete this[" + JSON.stringify(name) + "];");
257+
--indent;
258+
});
259+
--indent;
260+
push("}");
261+
--indent;
262+
push("});");
223263
});
224264

225265
// #encode

0 commit comments

Comments
 (0)