Skip to content

Commit

Permalink
Other: Add an option to always create default vals
Browse files Browse the repository at this point in the history
  • Loading branch information
Fathy Boundjadj committed Mar 13, 2017
1 parent 5041fad commit 792755f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
Empty file modified bin/pbjs
100644 → 100755
Empty file.
9 changes: 8 additions & 1 deletion cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +42,7 @@ exports.main = function main(args, callback) {
delimited : true,
beautify : true,
comments : true,
defaults : false,
es6 : null,
lint : lintDefault
}
Expand All @@ -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, "..")) || ".");

Expand Down Expand Up @@ -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] -",
""
Expand Down
23 changes: 16 additions & 7 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<ks.length;++i)")
Expand Down
11 changes: 9 additions & 2 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var converter = exports;
var Enum = require("./enum"),
util = require("./util");

// see cli/pbjs.js
converter.defaults = false;

/**
* Generates a partial value fromObject conveter.
* @param {Codegen} gen Codegen instance
Expand Down Expand Up @@ -123,8 +126,12 @@ converter.fromObject = function fromObject(mtype) {
} else if (field.repeated) { gen
("if(d%s){", prop)
("if(!Array.isArray(d%s))", prop)
("throw TypeError(%j)", field.fullName + ": array expected")
("m%s=[]", prop)
("throw TypeError(%j)", field.fullName + ": array expected");

if (!converter.defaults) gen
("m%s=[]", prop);

gen
("for(var i=0;i<d%s.length;++i){", prop);
genValuePartial_fromObject(gen, field, i, prop + "[i]")
("}")
Expand Down
7 changes: 5 additions & 2 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = decoder;

decoder.compat = true;

// see cli/pbjs.js
decoder.defaults = false;

var Enum = require("./enum"),
types = require("./types"),
util = require("./util");
Expand Down Expand Up @@ -52,8 +55,8 @@ function decoder(mtype) {
("%s[typeof k===\"object\"?util.longToHash(k):k]=r.%s()", ref, type);

// Repeated fields
} else if (field.repeated) { gen

} else if (field.repeated) {
if (!decoder.defaults) gen
("if(!(%s&&%s.length))", ref, ref)
("%s=[]", ref);

Expand Down
13 changes: 11 additions & 2 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = encoder;

encoder.compat = true;

// see cli/pbjs.js
encoder.defaults = false;

var Enum = require("./enum"),
types = require("./types"),
util = require("./util");
Expand Down Expand Up @@ -90,9 +93,15 @@ function encoder(mtype) {
("}");

// Non-packed
} else { gen
} else {
if(encoder.defaults) { gen
("if(%s!==undefined&&%s.length){", ref, ref);
}
else { gen
("if(%s!==undefined&&m.hasOwnProperty(%j)){", ref, field.name);
}

("if(%s!==undefined&&m.hasOwnProperty(%j)){", ref, field.name)
gen
("for(var i=0;i<%s.length;++i)", ref);
if (wireType === undefined)
genTypePartial(gen, field, index, ref + "[i]");
Expand Down

0 comments on commit 792755f

Please sign in to comment.