Skip to content

Commit

Permalink
CLI: Check upfront if key-var is required in static decoders with map…
Browse files Browse the repository at this point in the history
…s, see #726
  • Loading branch information
dcodeIO committed Mar 24, 2017
1 parent 16adff0 commit 8d99815
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 105 deletions.
6 changes: 2 additions & 4 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ exports.main = function(args, callback) {
out = out.join("").replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
process.stderr.write(out);
var err = Error("code " + code);
if (callback) {
callback(err);
return;
}
if (callback)
return callback(err);
throw err;
}

Expand Down
17 changes: 11 additions & 6 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")
("if(!(r instanceof Reader))")
("r=Reader.create(r)")
("var c=l===undefined?r.len:r.pos+l,m=new this.ctor")
("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k" : ""))
("while(r.pos<c){")
("var t=r.uint32()");
if (mtype.group) gen
Expand All @@ -37,16 +37,22 @@ function decoder(mtype) {

// Map fields
if (field.map) { gen

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

// Repeated fields
} else if (field.repeated) { gen
Expand Down Expand Up @@ -77,7 +83,6 @@ function decoder(mtype) {
("%s=r.%s()", ref, type);
gen
("break");

// Unknown fields
} gen
("default:")
Expand Down
2 changes: 1 addition & 1 deletion src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function encoder(mtype) {

// Repeated fields
} else if (field.repeated) { gen
("if(%s&&%s.length&&m.hasOwnProperty(%j)){", ref, ref, field.name);
("if(%s&&%s.length){", ref, ref);

// Packed repeated
if (field.packed && types.packed[type] !== undefined) { gen
Expand Down
12 changes: 6 additions & 6 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ $root.Message = (function() {
writer = $Writer.create();
if (message.stringVal != null && message.hasOwnProperty("stringVal"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.stringVal);
if (message.stringRepeated && message.stringRepeated.length && message.hasOwnProperty("stringRepeated"))
if (message.stringRepeated && message.stringRepeated.length)
for (var i = 0; i < message.stringRepeated.length; ++i)
writer.uint32(/* id 2, wireType 2 =*/18).string(message.stringRepeated[i]);
if (message.uint64Val != null && message.hasOwnProperty("uint64Val"))
writer.uint32(/* id 3, wireType 0 =*/24).uint64(message.uint64Val);
if (message.uint64Repeated && message.uint64Repeated.length && message.hasOwnProperty("uint64Repeated")) {
if (message.uint64Repeated && message.uint64Repeated.length) {
writer.uint32(/* id 4, wireType 2 =*/34).fork();
for (var i = 0; i < message.uint64Repeated.length; ++i)
writer.uint64(message.uint64Repeated[i]);
writer.ldelim();
}
if (message.bytesVal && message.hasOwnProperty("bytesVal"))
writer.uint32(/* id 5, wireType 2 =*/42).bytes(message.bytesVal);
if (message.bytesRepeated && message.bytesRepeated.length && message.hasOwnProperty("bytesRepeated"))
if (message.bytesRepeated && message.bytesRepeated.length)
for (var i = 0; i < message.bytesRepeated.length; ++i)
writer.uint32(/* id 6, wireType 2 =*/50).bytes(message.bytesRepeated[i]);
if (message.enumVal != null && message.hasOwnProperty("enumVal"))
writer.uint32(/* id 7, wireType 0 =*/56).uint32(message.enumVal);
if (message.enumRepeated && message.enumRepeated.length && message.hasOwnProperty("enumRepeated")) {
if (message.enumRepeated && message.enumRepeated.length) {
writer.uint32(/* id 8, wireType 2 =*/66).fork();
for (var i = 0; i < message.enumRepeated.length; ++i)
writer.uint32(message.enumRepeated[i]);
Expand Down Expand Up @@ -160,7 +160,7 @@ $root.Message = (function() {
Message.decode = function decode(reader, length) {
if (!(reader instanceof $Reader))
reader = $Reader.create(reader);
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Message();
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Message(), key;
while (reader.pos < end) {
var tag = reader.uint32();
switch (tag >>> 3) {
Expand Down Expand Up @@ -210,7 +210,7 @@ $root.Message = (function() {
reader.skip().pos++;
if (message.int64Map === $util.emptyObject)
message.int64Map = {};
var key = reader.string();
key = reader.string();
reader.pos++;
message.int64Map[typeof key === "object" ? $util.longToHash(key) : key] = reader.int64();
break;
Expand Down
12 changes: 6 additions & 6 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $root.vector_tile = (function() {
Tile.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.layers && message.layers.length && message.hasOwnProperty("layers"))
if (message.layers && message.layers.length)
for (var i = 0; i < message.layers.length; ++i)
$root.vector_tile.Tile.Layer.encode(message.layers[i], writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
return writer;
Expand Down Expand Up @@ -625,15 +625,15 @@ $root.vector_tile = (function() {
writer = $Writer.create();
if (message.id != null && message.hasOwnProperty("id"))
writer.uint32(/* id 1, wireType 0 =*/8).uint64(message.id);
if (message.tags && message.tags.length && message.hasOwnProperty("tags")) {
if (message.tags && message.tags.length) {
writer.uint32(/* id 2, wireType 2 =*/18).fork();
for (var i = 0; i < message.tags.length; ++i)
writer.uint32(message.tags[i]);
writer.ldelim();
}
if (message.type != null && message.hasOwnProperty("type"))
writer.uint32(/* id 3, wireType 0 =*/24).uint32(message.type);
if (message.geometry && message.geometry.length && message.hasOwnProperty("geometry")) {
if (message.geometry && message.geometry.length) {
writer.uint32(/* id 4, wireType 2 =*/34).fork();
for (var i = 0; i < message.geometry.length; ++i)
writer.uint32(message.geometry[i]);
Expand Down Expand Up @@ -955,13 +955,13 @@ $root.vector_tile = (function() {
if (!writer)
writer = $Writer.create();
writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
if (message.features && message.features.length && message.hasOwnProperty("features"))
if (message.features && message.features.length)
for (var i = 0; i < message.features.length; ++i)
$root.vector_tile.Tile.Feature.encode(message.features[i], writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim();
if (message.keys && message.keys.length && message.hasOwnProperty("keys"))
if (message.keys && message.keys.length)
for (var i = 0; i < message.keys.length; ++i)
writer.uint32(/* id 3, wireType 2 =*/26).string(message.keys[i]);
if (message.values && message.values.length && message.hasOwnProperty("values"))
if (message.values && message.values.length)
for (var i = 0; i < message.values.length; ++i)
$root.vector_tile.Tile.Value.encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
if (message.extent != null && message.hasOwnProperty("extent"))
Expand Down
26 changes: 13 additions & 13 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ $root.Package = (function() {
writer.uint32(/* id 7, wireType 2 =*/58).string(message.bugs);
if (message.homepage != null && message.hasOwnProperty("homepage"))
writer.uint32(/* id 8, wireType 2 =*/66).string(message.homepage);
if (message.keywords && message.keywords.length && message.hasOwnProperty("keywords"))
if (message.keywords && message.keywords.length)
for (var i = 0; i < message.keywords.length; ++i)
writer.uint32(/* id 9, wireType 2 =*/74).string(message.keywords[i]);
if (message.main != null && message.hasOwnProperty("main"))
Expand All @@ -201,7 +201,7 @@ $root.Package = (function() {
writer.uint32(/* id 15, wireType 2 =*/122).fork().uint32(/* id 1, wireType 2 =*/10).string(keys[i]).uint32(/* id 2, wireType 2 =*/18).string(message.devDependencies[keys[i]]).ldelim();
if (message.types != null && message.hasOwnProperty("types"))
writer.uint32(/* id 17, wireType 2 =*/138).string(message.types);
if (message.cliDependencies && message.cliDependencies.length && message.hasOwnProperty("cliDependencies"))
if (message.cliDependencies && message.cliDependencies.length)
for (var i = 0; i < message.cliDependencies.length; ++i)
writer.uint32(/* id 18, wireType 2 =*/146).string(message.cliDependencies[i]);
if (message.versionScheme != null && message.hasOwnProperty("versionScheme"))
Expand Down Expand Up @@ -230,7 +230,7 @@ $root.Package = (function() {
Package.decode = function decode(reader, length) {
if (!(reader instanceof $Reader))
reader = $Reader.create(reader);
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Package();
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Package(), key;
while (reader.pos < end) {
var tag = reader.uint32();
switch (tag >>> 3) {
Expand Down Expand Up @@ -273,41 +273,41 @@ $root.Package = (function() {
reader.skip().pos++;
if (message.bin === $util.emptyObject)
message.bin = {};
var key = reader.string();
key = reader.string();
reader.pos++;
message.bin[typeof key === "object" ? $util.longToHash(key) : key] = reader.string();
message.bin[k] = reader.string();
break;
case 12:
reader.skip().pos++;
if (message.scripts === $util.emptyObject)
message.scripts = {};
var key = reader.string();
key = reader.string();
reader.pos++;
message.scripts[typeof key === "object" ? $util.longToHash(key) : key] = reader.string();
message.scripts[k] = reader.string();
break;
case 13:
reader.skip().pos++;
if (message.dependencies === $util.emptyObject)
message.dependencies = {};
var key = reader.string();
key = reader.string();
reader.pos++;
message.dependencies[typeof key === "object" ? $util.longToHash(key) : key] = reader.string();
message.dependencies[k] = reader.string();
break;
case 14:
reader.skip().pos++;
if (message.optionalDependencies === $util.emptyObject)
message.optionalDependencies = {};
var key = reader.string();
key = reader.string();
reader.pos++;
message.optionalDependencies[typeof key === "object" ? $util.longToHash(key) : key] = reader.string();
message.optionalDependencies[k] = reader.string();
break;
case 15:
reader.skip().pos++;
if (message.devDependencies === $util.emptyObject)
message.devDependencies = {};
var key = reader.string();
key = reader.string();
reader.pos++;
message.devDependencies[typeof key === "object" ? $util.longToHash(key) : key] = reader.string();
message.devDependencies[k] = reader.string();
break;
case 17:
message.types = reader.string();
Expand Down
Loading

0 comments on commit 8d99815

Please sign in to comment.