From 792755f44387008d293a70d2e8136319c7e80120 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Sun, 12 Mar 2017 21:05:38 +0100 Subject: [PATCH] Other: Add an option to always create default vals --- bin/pbjs | 0 cli/pbjs.js | 9 ++++++++- src/class.js | 23 ++++++++++++++++------- src/converter.js | 11 +++++++++-- src/decoder.js | 7 +++++-- src/encoder.js | 13 +++++++++++-- 6 files changed, 49 insertions(+), 14 deletions(-) mode change 100644 => 100755 bin/pbjs diff --git a/bin/pbjs b/bin/pbjs old mode 100644 new mode 100755 diff --git a/cli/pbjs.js b/cli/pbjs.js index 7a5e7f8fb..acaeaad55 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -31,7 +31,7 @@ exports.main = function main(args, callback) { lint : "l" }, string: [ "target", "out", "path", "wrap", "root", "lint" ], - boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse" ], + boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "defaults" ], default: { target : "json", create : true, @@ -42,6 +42,7 @@ exports.main = function main(args, callback) { delimited : true, beautify : true, comments : true, + defaults : false, es6 : null, lint : lintDefault } @@ -51,6 +52,11 @@ exports.main = function main(args, callback) { files = argv._, paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || []; + protobuf.encoder.defaults = argv.defaults; + protobuf.decoder.defaults = argv.defaults; + protobuf.converter.defaults = argv.defaults; + protobuf.Class.defaults = argv.defaults; + // protobuf.js package directory contains additional, otherwise non-bundled google types paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || "."); @@ -107,6 +113,7 @@ exports.main = function main(args, callback) { " --no-delimited Does not generate delimited encode/decode functions.", " --no-beautify Does not beautify generated code.", " --no-comments Does not output any JSDoc comments.", + " --defaults Does set default values when using .decode() or new()", "", "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -", "" diff --git a/src/class.js b/src/class.js index 8c9db1bb6..90fe3af13 100644 --- a/src/class.js +++ b/src/class.js @@ -6,6 +6,9 @@ var Message = require("./message"), var Type; // cyclic +// see cli/pbjs.js +Class.defaults = false; + /** * Constructs a new message prototype for the specified reflected type and sets up its constructor. * @classdesc Runtime class providing the tools to create your own custom classes. @@ -79,13 +82,19 @@ Class.generate = function generate(type) { // eslint-disable-line no-unused-vars var gen = util.codegen("p"); // see issue #700: the following would add explicitly initialized mutable object/array fields // so that these aren't just inherited from the prototype. will break test cases. - /* - for (var i = 0, field; i < type.fieldsArray.length; ++i) - if ((field = type._fieldsArray[i]).map) gen - ("this%s={}", util.safeProp(field.name)); - else if (field.repeated) gen - ("this%s=[]", util.safeProp(field.name)); - */ + + if (Class.defaults) { + for (var i = 0; i < type.fieldsArray.length; ++i) { + var field = type.fieldsArray[i]; + var prop = util.safeProp(field.name) + + if (field.map) gen + ("this%s={}", prop); + else if (field.repeated) gen + ("this%s=[]", prop); + } + } + return gen ("if(p){") ("for(var ks=Object.keys(p),i=0;i