Skip to content

Commit

Permalink
CLI: pbjs now generates more convenient dot-notation property accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 27, 2016
1 parent 1e0ebc0 commit 3d84ecd
Show file tree
Hide file tree
Showing 25 changed files with 329 additions and 311 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 9 additions & 10 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
};
18 changes: 14 additions & 4 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
21 changes: 11 additions & 10 deletions dist/noparse/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/noparse/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/noparse/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/noparse/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/noparse/protobuf.min.js.map

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
20 changes: 10 additions & 10 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 3d84ecd

Please sign in to comment.