Skip to content

Commit

Permalink
fixed: ProtoBuf "repeated bytes".
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Mar 16, 2016
1 parent 8f1522f commit 6e88d9d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 41 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jpacks",
"description": "Binary data packing and unpacking.",
"version": "0.6.5",
"version": "0.6.10",
"homepage": "https://github.com/zswang/jpacks",
"authors": {
"name": "zswang",
Expand Down
4 changes: 2 additions & 2 deletions jpacks.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Binary data packing and unpacking.
* @author
* zswang (http://weibo.com/zswang)
* @version 0.6.5
* @date 2016-03-01
* @version 0.6.10
* @date 2016-03-16
*/
function createSchema() {
/**
Expand Down
4 changes: 2 additions & 2 deletions jpacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Binary data packing and unpacking.
* @author
* zswang (http://weibo.com/zswang)
* @version 0.6.5
* @date 2016-03-01
* @version 0.6.10
* @date 2016-03-16
*/
function createSchema() {
/**
Expand Down
2 changes: 1 addition & 1 deletion jpacks.min.js

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 @@ -2,7 +2,7 @@
"name": "jpacks",
"title": "c struct packer",
"description": "Binary data packing and unpacking.",
"version": "0.6.5",
"version": "0.6.10",
"homepage": "http://github.com/zswang/jpacks",
"main": "jpacks.js",
"author": {
Expand Down
114 changes: 80 additions & 34 deletions schemas-extend/protobuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ var protobufjs = require('protobufjs');

module.exports = function(Schema) {
/*<define>*/
function bytesify(value, options) {
if (value instanceof Array) {
return new Buffer(value);
} else if (typeof value === 'string') {
return new Buffer(Schema.stringBytes(value, options));
} else {
return new Buffer([]);
}
}
/**
* 将 JSON 数据填入 protobuf 结构中
*
Expand All @@ -25,42 +34,53 @@ module.exports = function(Schema) {
return;
}
var value = json[key];
if (type.type.name === 'bytes') {
if (value instanceof Array) {
result[key] = new Buffer(value);
return;
} else if (typeof value === 'string') {
result[key] = new Buffer(Schema.stringBytes(value, options));
return;
}
}

if (!type.resolvedType) { // 基础类型
result[key] = value;
return;
}

if (type.repeated) { // 数组类型
var items = [];
value = value || [];
for (var i = 0; i < value.length; i++) {
items.push(protoify(type.resolvedType.clazz, value[i], options));
if (!type.resolvedType) {
if (type.type.name === 'bytes') {
items.push(bytesify(value[i], options));
} else {
items.push(value[i]);
}
} else {
items.push(protoify(type.resolvedType.clazz, value[i], options));
}
}
result[key] = items;
} else {
result[key] = protoify(type.resolvedType.clazz, json[key], options);
return;
}

if (type.type.name === 'bytes') {
result[key] = bytesify(value, options);
return;
}

if (!type.resolvedType) { // 基础类型
result[key] = value;
return;
}

result[key] = protoify(type.resolvedType.clazz, json[key], options);
});
return new messager(result);
}

function bytesprase(value, options) {
if (options.protobuf_bytesAsString) {
return Schema.unpack(Schema.string(value.length), value, options);
} else {
return Schema.unpack(Schema.bytes(value.length), value, options);
}
}
/**
* 清洗 protobuf 数据
*
* @param {Message of class} messager protobuf 数据类型
* @param {Object} json JSON 数据
*/
function jsonify(messager, json, options) {
function jsonparse(messager, json, options) {
if (!json || !messager) {
return json;
}
Expand All @@ -84,25 +104,29 @@ module.exports = function(Schema) {
delete json[key];
return;
}
if (type.type.name === 'bytes') {
if (options.protobuf_bytesAsString) {
json[key] = Schema.unpack(Schema.string(value.length), value, options);
} else {
json[key] = Schema.unpack(Schema.bytes(value.length), value, options);
if (type.repeated) { // 数组类型
value = value || [];
for (var i = 0; i < value.length; i++) {
if (!type.resolvedType) {
if (type.type.name === 'bytes') {
value[i] = bytesprase(value[i], options);
}
} else {
jsonparse(type.resolvedType.clazz, value[i], options);
}
}
return;
}
if (!type.resolvedType) { // 基础类型

if (type.type.name === 'bytes') {
json[key] = bytesprase(value, options);
return;
}
if (type.repeated) { // 数组类型
value = value || [];
for (var i = 0; i < value.length; i++) {
jsonify(type.resolvedType.clazz, value[i], options);
}
} else {
jsonify(type.resolvedType.clazz, json[key], options);

if (!type.resolvedType) { // 基础类型
return;
}
jsonparse(type.resolvedType.clazz, json[key], options);
});
return json;
}
Expand Down Expand Up @@ -203,7 +227,6 @@ module.exports = function(Schema) {
'int8'
);
console.log(_.stringify(_schema))
// > array(protobuf('message Value { required string text = 1; }','Value','uint16'),'int8')
_.setDefaultOptions({
Expand All @@ -222,6 +245,29 @@ module.exports = function(Schema) {
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > [{"text":"a"},{"text":"b"}]
```
* @example protobufCreator():repeated bytes
```js
var _ = jpacks;
var _schema =
_.protobuf('message BytesArray { repeated bytes items = 1; }', 'BytesArray', 'uint16');
console.log(_.stringify(_schema))
// > protobuf('message BytesArray { repeated bytes items = 1; }','BytesArray','uint16')
_.setDefaultOptions({
protobuf_bytesAsString: false
});
var buffer = _.pack(_schema, {
items: [[1, 2, 3, 4], [5, 6, 7, 8], '12345678']
});
console.log(buffer.join(' '));
// > 22 0 10 4 1 2 3 4 10 4 5 6 7 8 10 8 49 50 51 52 53 54 55 56
console.log(JSON.stringify(_.unpack(_schema, buffer)));
// > {"items":[[1,2,3,4],[5,6,7,8],[49,50,51,52,53,54,55,56]]}
```
'''</example>'''
*/
function protobufCreator(prototext, messagepath, size) {
Expand All @@ -240,7 +286,7 @@ module.exports = function(Schema) {
if (byteSize <= 0) {
return null;
}
return jsonify(messager, rs.toRaw(false, true), options);
return jsonparse(messager, rs.toRaw(false, true), options);
},
pack: function _pack(value, options, buffer) {
if (!value) {
Expand Down
17 changes: 17 additions & 0 deletions test/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,23 @@ describe("./schemas-extend/protobuf.js", function () {
print(JSON.stringify(_.unpack(_schema, buffer)));
assert.equal(printValue, "[{\"text\":\"a\"},{\"text\":\"b\"}]"); printValue = undefined;
});
it("protobufCreator():repeated bytes", function () {
var _ = jpacks;
var _schema =
_.protobuf('message BytesArray { repeated bytes items = 1; }', 'BytesArray', 'uint16');
print(_.stringify(_schema))
assert.equal(printValue, "protobuf('message BytesArray { repeated bytes items = 1; }','BytesArray','uint16')"); printValue = undefined;
_.setDefaultOptions({
protobuf_bytesAsString: false
});
var buffer = _.pack(_schema, {
items: [[1, 2, 3, 4], [5, 6, 7, 8], '12345678']
});
print(buffer.join(' '));
assert.equal(printValue, "22 0 10 4 1 2 3 4 10 4 5 6 7 8 10 8 49 50 51 52 53 54 55 56"); printValue = undefined;
print(JSON.stringify(_.unpack(_schema, buffer)));
assert.equal(printValue, "{\"items\":[[1,2,3,4],[5,6,7,8],[49,50,51,52,53,54,55,56]]}"); printValue = undefined;
});
});
describe("./schemas-extend/zlib.js", function () {
printValue = undefined;
Expand Down

0 comments on commit 6e88d9d

Please sign in to comment.