Permalink
Browse files

CLI: Also reuse specified root in pbjs for JSON modules, see #653

  • Loading branch information...
dcodeIO committed Jan 24, 2017
1 parent 3a05624 commit 735da4315a98a6960f3b5089115e308548b91c07
Showing with 92 additions and 21 deletions.
  1. +13 −4 cli/targets/json-module.js
  2. +0 −2 cli/targets/json.js
  3. +1 −3 cli/targets/static-module.js
  4. +0 −11 cli/targets/static.js
  5. +20 −1 cli/util.js
  6. +43 −0 tests/split/root.js
  7. +15 −0 tests/split/test.proto
@@ -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);
});
@@ -1,8 +1,6 @@
"use strict";
module.exports = json_target;
var protobuf = require("../..");
json_target.description = "JSON representation"
function json_target(root, options, callback) {
@@ -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("../..");
@@ -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;
@@ -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;
});
};
@@ -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;
@@ -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.