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

* support default value for map key & value #1087

Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ cli/node_modules/
docs/
coverage/
sandbox/
/.vs
/cli/.vs
63 changes: 48 additions & 15 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function decoder(mtype) {
var gen = util.codegen(["r", "l"], mtype.name + "$decode")
("if(!(r instanceof Reader))")
("r=Reader.create(r)")
("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k" : ""))
("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k,value" : ""))
("while(r.pos<c){")
("var t=r.uint32()");
if (mtype.group) gen
Expand All @@ -37,22 +37,55 @@ function decoder(mtype) {

// Map fields
if (field.map) { gen
("r.skip().pos++") // assumes id 1 + key wireType
("{")
("if(%s===util.emptyObject)", ref)
("%s={}", ref)
("k=r.%s()", field.keyType)
("r.pos++"); // assumes id 2 + value wireType
if (types.long[field.keyType] !== undefined) {
if (types.basic[type] === undefined) gen
("%s[typeof k===\"object\"?util.longToHash(k):k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
else gen
("%s[typeof k===\"object\"?util.longToHash(k):k]=r.%s()", ref, type);
} else {
if (types.basic[type] === undefined) gen
("%s[k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
else gen
("%s[k]=r.%s()", ref, type);
}
("var len_%i = r.uint32()", i)
("var end_%i = r.pos + len_%i", i, i);
if (field.keyType === "string") { gen
("k=\"%s\"", types.defaults[field.keyType]);
} else {
if (types.defaults[field.keyType] !== undefined) { gen
("k=%s", types.defaults[field.keyType]);
}
else { gen
("k=null");
}
}

if (type === "string") { gen
("value=\"%s\"", types.defaults[type]);
} else {
if (types.defaults[type] !== undefined) { gen
("value=%s", types.defaults[type]);
}
else { gen
("value=null");
}
} gen
("while(r.pos<end_%i){", i) // assumes id 1 + key wireType
("var t=r.uint32()")
("switch(t>>>3){")
("case 1: k=r.%s(); break", field.keyType)
("case 2:");
if (types.basic[type] === undefined) { gen
("value=types[%i].decode(r,r.uint32())", i); // can't be groups
} else { gen
("value=r.%s()", type);
} gen
("break"); gen
("default:")
("r.skipType(t&7)")
("break")
("}")
("}");

if (types.long[field.keyType] !== undefined) { gen
("%s[typeof k===\"object\"?util.longToHash(k):k]=value", ref);
} else { gen
("%s[k]=value", ref);
} gen
("}");

// Repeated fields
} else if (field.repeated) { gen
Expand Down