Skip to content

Commit

Permalink
CLI: Also reuse specified root in pbjs for JSON modules, see #653
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 24, 2017
1 parent 3a05624 commit 735da43
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 21 deletions.
17 changes: 13 additions & 4 deletions cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ module.exports = json_module;

var util = require("../util");

json_module.description = "JSON representation as a module"
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() + ");";
output = util.wrap(output, options);
try {
var rootProp = util.safeProp(options.root || "default");
var output = [
"var $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
];
if (root.options) {
var optionsJson = util.jsonSafeProp(JSON.stringify(root.options, null, 2));
output.push(".setOptions(" + optionsJson + ")\n");
}
var json = util.jsonSafeProp(JSON.stringify(root.nested, null, 2).trim());
output.push(".addJSON(" + json + ");");
output = util.wrap(output.join(""), options);
process.nextTick(function() {
callback(null, output);
});
Expand Down
2 changes: 0 additions & 2 deletions cli/targets/json.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
module.exports = json_target;

var protobuf = require("../..");

json_target.description = "JSON representation"

function json_target(root, options, callback) {
Expand Down
4 changes: 1 addition & 3 deletions cli/targets/static-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ module.exports = static_module_target;
// - CommonJS modules depend on the minimal build for reduced package size with browserify.
// - AMD and global scope depend on the full library for now.

var path = require("path"),
fs = require("fs"),
util = require("../util");
var util = require("../util");

var protobuf = require("../..");

Expand Down
11 changes: 0 additions & 11 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,6 @@ function aOrAn(name) {
: "a ") + name;
}

// generate dot-notation property accessors where possible. this saves a few chars (i.e. m.hello
// instead of m["hello"]) but has no measurable performance impact (on V8). not present within the
// library itself because the reserved words check requires a rather longish regex.
util.safeProp = (function(safeProp) {
return function safeProp_dn(name) {
return !/^[$\w]+$/.test(name) || cliUtil.reserved(name)
? safeProp(name)
: "." + name;
}
})(util.safeProp);

function buildNamespace(ref, ns) {
if (!ns)
return;
Expand Down
21 changes: 20 additions & 1 deletion cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ exports.pad = function(str, len, l) {
return str;
};

exports.reserved = function(name) {
exports.reserved = function reserved(name) {
return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
};

// generate dot-notation property accessors where possible. this saves a few chars (i.e. m.hello
// instead of m["hello"]) but has no measurable performance impact (on V8). not present within the
// library itself because the reserved words check requires a rather longish regex.
exports.safeProp = protobuf.util.safeProp = (function(safeProp) {
return function safeProp_dn(name) {
return !/^[$\w]+$/.test(name) || exports.reserved(name)
? safeProp(name)
: "." + name;
}
})(protobuf.util.safeProp);

exports.jsonSafeProp = function(json) {
return json.replace(/^( +)"(\w+)":/mg, function($0, $1, $2) {
return exports.safeProp($2).charAt(0) === "."
? $1 + $2 + ":"
: $0;
});
};
43 changes: 43 additions & 0 deletions tests/split/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";

var $protobuf = require("protobufjs");

var $root = ($protobuf.roots.split || ($protobuf.roots.split = new $protobuf.Root()))
.setOptions({
"(foo)": "bar"
})
.addJSON({
com: {
nested: {
Outer: {
fields: {
inner: {
type: "Inner",
id: 1
},
other: {
type: "Other",
id: 2
}
},
nested: {
Inner: {
fields: {}
}
}
},
Other: {
fields: {
"var": {
rule: "required",
type: "uint32",
id: 1
}
}
}
}
}
});

module.exports = $root;
15 changes: 15 additions & 0 deletions tests/split/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
option (foo) = "bar";

package com;

message Outer {
optional Inner inner = 1;
optional Other other = 2;

message Inner {
}
}

message Other {
required uint32 var = 1;
}

0 comments on commit 735da43

Please sign in to comment.