Permalink
Browse files

CLI: pbjs now generates more convenient dot-notation property accessors

  • Loading branch information...
dcodeIO committed Dec 27, 2016
1 parent 1e0ebc0 commit 3d84ecdb4788d71b5d3928e74db78e8e54695f0a
@@ -1,13 +1,15 @@
protobuf.js
===========
[![travis][travis-image]][travis-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url]
[![travis][travis-image]][travis-url] [![david][david-image]][david-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url]
**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).
**protobuf.js** is a pure JavaScript implementation for node and the browser. It efficiently encodes plain objects and custom classes and works out of the box with .proto files.
[travis-image]: https://img.shields.io/travis/dcodeIO/protobuf.js.svg
[travis-url]: https://travis-ci.org/dcodeIO/protobuf.js
[david-url]: https://david-dm.org/dcodeIO/protobuf.js
[david-image]: https://img.shields.io/david/dcodeIO/protobuf.js.svg
[npm-image]: https://img.shields.io/npm/v/protobufjs.svg
[npm-url]: https://npmjs.org/package/protobufjs
[npm-dl-image]: https://img.shields.io/npm/dm/protobufjs.svg
@@ -121,31 +121,30 @@ exports.main = function(args, callback) {
"keepCase": argv["keep-case"] || false
};
var root;
try {
root = root.loadSync(files, parseOptions); // sync is deterministic while async is not
root.loadSync(files, parseOptions); // sync is deterministic while async is not
} catch (err) {
if (callback) {
callback(err);
return;
} else
throw err;
return undefined;
}
throw err;
}
target(root, argv, function(err, output) {
target(root, argv, function targetCallback(err, output) {
if (err) {
if (callback)
return callback(err);
else
throw err;
throw err;
}
if (output !== "") {
if (argv.out)
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
else
process.stdout.write(output, "utf8");
}
if (callback)
return callback(null);
return callback
? callback(null)
: undefined;
});
};
@@ -3,7 +3,8 @@ module.exports = static_target;
// - Static code does not have any reflection or JSON features.
var protobuf = require("../..");
var protobuf = require("../.."),
cliUtil = require("../util");
var Type = protobuf.Type,
Service = protobuf.Service,
@@ -76,14 +77,23 @@ function pushComment(lines) {
push(" */");
}
var reservedRe = /^(?: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)$/;
function name(name) {
if (!name)
return "$root";
return reservedRe.test(name) ? name + "_" : name;
return cliUtil.reserved(name) ? name + "_" : 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;
@@ -113,3 +113,7 @@ exports.pad = function(str, len, l) {
str = l ? str + " " : " " + str;
return str;
};
exports.reserved = function(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);
};

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +4 Bytes (100%) dist/noparse/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +2 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +17 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -57,7 +57,7 @@
},
"devDependencies": {
"@types/long": "^3.0.31",
"@types/node": "6.0.53",
"@types/node": "6.0.54",
"benchmark": "^2.1.3",
"browserify": "^13.1.1",
"bundle-collapser": "^1.2.1",
@@ -5,8 +5,6 @@ var Enum = require("./enum"),
types = require("./types"),
util = require("./util");
var safeProp = util.safeProp;
function genEncodeType(gen, field, fieldIndex, ref) {
return field.resolvedType.group
? gen("types[%d].encode(%s,w.uint32(%d)).uint32(%d)", fieldIndex, ref, (field.id << 3 | 3) >>> 0, (field.id << 3 | 4) >>> 0)
@@ -30,7 +28,7 @@ function encoder(mtype) {
var field = fields[i].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type];
ref = "m" + safeProp(field.name);
ref = "m" + util.safeProp(field.name);
// Map fields
if (field.map) {
@@ -101,7 +99,7 @@ function encoder(mtype) {
var field = oneofFields[j],
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type];
ref = "m" + safeProp(field.name);
ref = "m" + util.safeProp(field.name);
gen
("case%j:", field.name);
@@ -57,8 +57,8 @@ $root.A = (function() {
var types; $lazyTypes.push(types = [null]);
return function encode(m, w) {
w||(w=Writer.create())
if(m["whatever"]!==undefined&&m["whatever"]!=="")
w.uint32(10).string(m["whatever"])
if(m.whatever!==undefined&&m.whatever!=="")
w.uint32(10).string(m.whatever)
return w
}
/* eslint-enable */
@@ -93,7 +93,7 @@ $root.A = (function() {
var t=r.uint32()
switch(t>>>3){
case 1:
m["whatever"]=r.string()
m.whatever=r.string()
break
default:
r.skipType(t&7)
@@ -126,8 +126,8 @@ $root.A = (function() {
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function verify(m) {
if(m["whatever"]!==undefined){
if(!util.isString(m["whatever"]))
if(m.whatever!==undefined){
if(!util.isString(m.whatever))
return"invalid value for field .A.whatever (string expected)"
}
return null
@@ -187,8 +187,8 @@ $root.B = (function() {
var types; $lazyTypes.push(types = ["A"]);
return function encode(m, w) {
w||(w=Writer.create())
if(m["A"]!==undefined&&m["A"]!==null)
types[0].encode(m["A"],w.uint32(10).fork()).ldelim()
if(m.A!==undefined&&m.A!==null)
types[0].encode(m.A,w.uint32(10).fork()).ldelim()
return w
}
/* eslint-enable */
@@ -223,7 +223,7 @@ $root.B = (function() {
var t=r.uint32()
switch(t>>>3){
case 1:
m["A"]=types[0].decode(r,r.uint32())
m.A=types[0].decode(r,r.uint32())
break
default:
r.skipType(t&7)
@@ -256,9 +256,9 @@ $root.B = (function() {
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function verify(m) {
if(m["A"]!==undefined&&m["A"]!==null){
if(m.A!==undefined&&m.A!==null){
var r;
if(r=types[0].verify(m["A"]))
if(r=types[0].verify(m.A))
return r
}
return null
Oops, something went wrong.

0 comments on commit 3d84ecd

Please sign in to comment.