bin-protocol
Advanced tools
Comparing version 2.0.12 to 3.0.0
@@ -458,5 +458,5 @@ 'use strict'; | ||
fname = field.name; | ||
fname = [field.name]; | ||
if (field.repeated && field.options.packed !== 'true') { // repeated, not packed | ||
fname += '[' + (self.context[field.name] ? self.context[field.name].length : 0) + ']'; | ||
fname.push(self.context[field.name] ? self.context[field.name].length : 0); | ||
} | ||
@@ -506,3 +506,3 @@ if (type === 2) { | ||
while (self.offset < len) { | ||
_.get(self, field.type).call(self, 'items[' + (i++) + ']'); | ||
_.get(self, field.type).call(self, ['items', i++]); | ||
} | ||
@@ -509,0 +509,0 @@ return self.context.items; |
@@ -23,32 +23,26 @@ 'use strict'; | ||
} | ||
return _.get(this.result, this.path.join('.')); | ||
return _.get(this.result, this.path); | ||
}, set: function (v) { | ||
_.set(this.result, this.path.join('.'), v); | ||
_.set(this.result, this.path, v); | ||
} | ||
}); | ||
function _read(name, read, path) { | ||
var _path, r; | ||
function _read(fn, path) { | ||
var r, i, _path = []; | ||
if (typeof path !== 'string') { path = undefined; } | ||
if (typeof path === 'string') { | ||
_path = [path]; | ||
} else if (Array.isArray(path)) { | ||
_path = path; | ||
} | ||
if (path) { | ||
this.path.push(path); | ||
for (i = 0; i < _path.length; i++) { | ||
this.path.push(_path[i]); | ||
} | ||
_path = this.path.join('.'); | ||
r = fn.apply(this, Array.prototype.slice.call(arguments, 2)); | ||
try { | ||
r = read.apply(this, Array.prototype.slice.call(arguments, 3)); | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
if (path) { | ||
this.path.pop(); | ||
} | ||
} | ||
if (r !== undefined) { | ||
if (_path) { | ||
_.set(this.result, _path, r); | ||
if (this.path.length) { | ||
_.set(this.result, this.path, r); | ||
} else { | ||
@@ -59,6 +53,10 @@ this.result = r; | ||
for (i = 0; i < _path.length; i++) { | ||
this.path.pop(); | ||
} | ||
return this; | ||
} | ||
Reader.define = function (name, read, namespace, _proto) { | ||
Reader.define = function (name, fn, namespace, _proto) { | ||
_proto = _proto || Reader; | ||
@@ -69,7 +67,7 @@ | ||
} | ||
_proto.prototype.__methods.push([name, read, namespace]); | ||
_proto.prototype.__methods.push([name, fn, namespace]); | ||
}; | ||
Reader.prototype.define = function (name, read, namespace) { | ||
_.set(this, namespace ? namespace + '.' + name : name, _.bind(_read, this, name, read)); | ||
Reader.prototype.define = function (name, fn, namespace) { | ||
_.set(this, namespace ? namespace + '.' + name : name, _.bind(_read, this, fn)); | ||
}; | ||
@@ -97,10 +95,7 @@ | ||
while (!_break && (iterations !== undefined ? iterations-- : true)) { | ||
this.path.push(path + '[' + i++ + ']'); | ||
try { | ||
fn.call(this, end); | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
this.path.pop(); | ||
} | ||
this.path.push(path); | ||
this.path.push(i++); | ||
fn.call(this, end); | ||
this.path.pop(); | ||
this.path.pop(); | ||
} | ||
@@ -107,0 +102,0 @@ return this; |
@@ -27,8 +27,8 @@ 'use strict'; | ||
function _write(name, write, value) { | ||
write.apply(this, [value].concat(Array.prototype.slice.call(arguments, 3))); | ||
function _write(fn, value) { | ||
fn.apply(this, [value].concat(Array.prototype.slice.call(arguments, 2))); | ||
return this; | ||
} | ||
Writer.define = function (name, write, namespace, _proto) { | ||
Writer.define = function (name, fn, namespace, _proto) { | ||
_proto = _proto || Writer; | ||
@@ -39,7 +39,7 @@ | ||
} | ||
_proto.prototype.__methods.push([name, write, namespace]); | ||
_proto.prototype.__methods.push([name, fn, namespace]); | ||
}; | ||
Writer.prototype.define = function (name, write, namespace) { | ||
_.set(this, namespace ? namespace + '.' + name : name, _.bind(_write, this, name, write)); | ||
Writer.prototype.define = function (name, fn, namespace) { | ||
_.set(this, namespace ? namespace + '.' + name : name, _.bind(_write, this, fn)); | ||
}; | ||
@@ -46,0 +46,0 @@ |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "2.0.12", | ||
"version": "3.0.0", | ||
"main": "./lib/index.js", | ||
@@ -12,0 +12,0 @@ "keywords": [ |
44
test.js
'use strict'; | ||
var Protocol = require('./lib/index'); | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var protocol = new Protocol(); | ||
var result; | ||
var proto = fs.readFileSync('/Users/oleksiy/src/binary-protocol/test/proto/complex.proto'); | ||
var buffer = new Buffer(16); | ||
var GameProtocol = Protocol.createProtobufProtocol(proto); | ||
buffer.writeInt32BE(3, 0); | ||
buffer.writeInt32BE(2, 4); | ||
buffer.writeInt32BE(3, 8); | ||
buffer.writeInt32BE(4, 12); | ||
var protocol = new GameProtocol(); | ||
/*var result = protocol.read(new Buffer([0, 1, 2, 3])) | ||
.Int8('num1') | ||
.Int8('num2') | ||
.Int8('num3') | ||
.Int8('num4'); | ||
var buffer, r; | ||
console.log(result); // => { num1: 0, num2: 1, num3: 2, num4: 3 }*/ | ||
/*protocol.parseProto(fs.readFileSync('/Users/oleksiy/src/riakproto/riak_pb/src/riak.proto')); | ||
protocol.parseProto(fs.readFileSync('/Users/oleksiy/src/riakproto/riak_pb/src/riak_dt.proto')); | ||
protocol.parseProto(fs.readFileSync('/Users/oleksiy/src/riakproto/riak_pb/src/riak_kv.proto')); | ||
protocol.parseProto(fs.readFileSync('/Users/oleksiy/src/riakproto/riak_pb/src/riak_search.proto')); | ||
protocol.parseProto(fs.readFileSync('/Users/oleksiy/src/riakproto/riak_pb/src/riak_ts.proto')); | ||
protocol.parseProto(fs.readFileSync('/Users/oleksiy/src/riakproto/riak_pb/src/riak_yokozuna.proto'));*/ | ||
protocol.define('customArray', { | ||
read: function () { | ||
var i = 0; | ||
this.Int32BE('length'); | ||
buffer = new Buffer([10, 5, 82, 117, 115, 116, 121, 18, 17, 10, 9, 73, 114, 111, 110, 32, 73, 110, 99, 46, 18, 4, 10, 2, 85, 83, 24, 2]); | ||
for (i = 0; i < this.context.length; i++) { | ||
this.Int32BE('items[' + i + ']'); // yes, it works | ||
} | ||
r = protocol.read(buffer).Game.Cars.Car().result; | ||
return this.context.items; | ||
} | ||
}); | ||
console.log(r); | ||
result = protocol.read(buffer).customArray('items'); // => { items: [2, 3, 4] } | ||
console.log(result._temp); |
@@ -120,3 +120,3 @@ 'use strict'; | ||
for (i = 0; i < this.context.length; i++) { | ||
this.Int32BE('items[' + i + ']'); | ||
this.Int32BE(['items', i]); | ||
} | ||
@@ -144,4 +144,4 @@ | ||
protocol.read(buffer).loopArray('items').result.should.be.eql({ | ||
items: [2, 3, 4] | ||
protocol.read(buffer).loopArray('array').result.should.be.eql({ | ||
array: [2, 3, 4] | ||
}); | ||
@@ -148,0 +148,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
137371
35
2064