Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化:proto支持空数组 #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/*
*.log
.DS_Store
.DS_Store
*.idea
14 changes: 9 additions & 5 deletions lib/client/protobuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,17 @@
}

function decodeArray(array, type, protos){
if(util.isSimpleType(type)){
var length = codec.decodeUInt32(getBytes());

for(var i = 0; i < length; i++){
var length = codec.decodeUInt32(peekBytes());
if(length === 0){
getBytes();// 纯偏移
return ;
}
if (util.isSimpleType(type)) {
getBytes();// 简单类型也要偏移
for (var i = 0; i < length; i++) {
array.push(decodeProp(type));
}
}else{
} else {
array.push(decodeProp(type, protos));
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ function decodeProp(type, protos){
}

function decodeArray(array, type, protos){
var length = codec.decodeUInt32(peekBytes());
if(length === 0){
getBytes();// 纯偏移
return ;
}
if(util.isSimpleType(type)){
var length = codec.decodeUInt32(getBytes());

getBytes();// 简单类型也要偏移
for(var i = 0; i < length; i++){
array.push(decodeProp(type));
}
Expand Down
8 changes: 7 additions & 1 deletion lib/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function encodeMsg(buffer, offset, protos, msg){
offset = encodeProp(msg[name], proto.type, offset, buffer, protos);
break;
case 'repeated' :
if(!!msg[name] && msg[name].length > 0){
if (!!msg[name]) {
offset = encodeArray(msg[name], proto, offset, buffer, protos);
}
break;
Expand Down Expand Up @@ -161,6 +161,12 @@ function encodeProp(value, type, offset, buffer, protos){
* Encode reapeated properties, simple msg and object are decode differented
*/
function encodeArray(array, proto, offset, buffer, protos){
if(array.length === 0){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, codec.encodeUInt32(array.length));
return offset;
}

var i = 0;
if(util.isSimpleType(proto.type)){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
Expand Down
Loading