Skip to content

Commit

Permalink
Added .create to statically generated types and uppercase nested elem…
Browse files Browse the repository at this point in the history
…ents to reflection namespaces, see #576; Also added Namespace#getEnum for completeness, see #576; Made pbjs use loadSync for deterministic outputs, see #573
  • Loading branch information
dcodeIO committed Dec 19, 2016
1 parent ef43acf commit 99ad9cc
Show file tree
Hide file tree
Showing 33 changed files with 342 additions and 99 deletions.
2 changes: 1 addition & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var protobuf = require(".."),
// To experience the impact by yourself, increase string lengths within bench.json.

var root = protobuf.loadSync(require.resolve("./bench.proto"));
var Test = root.lookup("Test");
var Test = root.resolveAll().lookup("Test");

// protobuf.util.codegen.verbose = true;

Expand Down
2 changes: 1 addition & 1 deletion bench/prof.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (process.execArgv.indexOf("--prof") < 0) {
fs.unlink(file);
});
console.log("generating profile (may take a while) ...");
var child = child_process.execSync("node --prof --trace-deopt " + process.argv.slice(1).join(' '), {
var child = child_process.execSync("node --prof --trace-deopt " + process.execArgv.join(" ") + " " + process.argv.slice(1).join(' '), {
cwd: process.cwd(),
stdio: 'inherit'
});
Expand Down
41 changes: 23 additions & 18 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ exports.main = function(args, callback) {
root : "r"
},
string: [ "target", "out", "path", "wrap", "root" ],
boolean: [ "keep-case", "encode", "decode", "verify", "delimited" ],
boolean: [ "keep-case", "create", "encode", "decode", "verify", "delimited" ],
default: {
target: "json",
create: true,
encode: true,
decode: true,
verify: true,
Expand Down Expand Up @@ -76,6 +77,7 @@ exports.main = function(args, callback) {
"",
" Static targets only:",
"",
" --no-create Does not generate create functions used for runtime compatibility.",
" --no-encode Does not generate encode functions.",
" --no-decode Does not generate decode functions.",
" --no-verify Does not generate verify functions.",
Expand Down Expand Up @@ -119,28 +121,31 @@ exports.main = function(args, callback) {
"keepCase": argv["keep-case"] || false
};

root.load(files, parseOptions, function(err) {
var root;
try {
root = root.loadSync(files, parseOptions);
} catch (err) {
if (callback) {
callback(err);
return;
} else
throw err;
}

target(root, argv, function(err, output) {
if (err) {
if (callback)
return callback(err);
else
throw err;
}
target(root, argv, function(err, output) {
if (err) {
if (callback)
return callback(err);
else
throw err;
}
if (output !== "") {
if (argv.out)
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
else
process.stdout.write(output, "utf8");
}
if (callback)
callback(null);
});
if (output !== "") {
if (argv.out)
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
else
process.stdout.write(output, "utf8");
}
if (callback)
return callback(null);
});
};
2 changes: 1 addition & 1 deletion cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ json_module.description = "JSON representation as a module"
function json_module(root, options, callback) {

try {
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ").resolveAll();";
output = util.wrap(options.wrap || "default", output, options.root);
process.nextTick(function() {
callback(null, output);
Expand Down
25 changes: 15 additions & 10 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,23 @@ function buildType(ref, type) {
--indent;
push("});");
});

if (config.create) {
push("");
pushComment([
"Creates a new " + type.name + " instance using the specified properties.",
"@param {Object} [properties] Properties to set",
"@returns {" + fullName + "} " + type.name + " instance"
]);
push(name(type.name) + ".create = function create(properties) {");
++indent;
push("return new " + name(type.name) + "(properties);");
--indent;
push("};");
}


if (config.encode) {

// #encode
push("");
pushComment([
"Encodes the specified " + type.name + ".",
Expand All @@ -288,8 +301,6 @@ function buildType(ref, type) {
});

if (config.delimited) {

// #encodeDelimited
push("");
pushComment([
"Encodes the specified " + type.name + ", length delimited.",
Expand All @@ -308,8 +319,6 @@ function buildType(ref, type) {
}

if (config.decode) {

// #decode
push("");
pushComment([
"Decodes a " + type.name + " from the specified reader or buffer.",
Expand All @@ -324,8 +333,6 @@ function buildType(ref, type) {
});

if (config.delimited) {

// #decodeDelimited
push("");
pushComment([
"Decodes a " + type.name + " from the specified reader or buffer, length delimited.",
Expand All @@ -343,8 +350,6 @@ function buildType(ref, type) {
}

if (config.verify) {

// #verify
push("");
pushComment([
"Verifies a " + type.name + ".",
Expand Down
Loading

0 comments on commit 99ad9cc

Please sign in to comment.