Skip to content

Commit

Permalink
New: Made .verify behave more like .encode, see #710
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Apr 4, 2017
1 parent 79d0ba2 commit 3d23eed
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 372 deletions.
14 changes: 4 additions & 10 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ function encoder(mtype) {
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type];
ref = "m" + util.safeProp(field.name);

// Map fields
if (field.map) {
gen
("if(%s&&m.hasOwnProperty(%j)){", ref, field.name)
("if(%s!=null&&m.hasOwnProperty(%j)){", ref, field.name) // !== undefined && !== null
("for(var ks=Object.keys(%s),i=0;i<ks.length;++i){", ref)
("w.uint32(%d).fork().uint32(%d).%s(ks[i])", (field.id << 3 | 2) >>> 0, 8 | types.mapKey[field.keyType], field.keyType);
if (wireType === undefined) gen
Expand All @@ -59,7 +59,7 @@ function encoder(mtype) {

// Repeated fields
} else if (field.repeated) { gen
("if(%s&&%s.length){", ref, ref);
("if(%s!=null&&%s.length){", ref, ref); // !== undefined && !== null

// Packed repeated
if (field.packed && types.packed[type] !== undefined) { gen
Expand All @@ -83,15 +83,9 @@ function encoder(mtype) {

// Non-repeated
} else {
if (field.optional) {

if (field.bytes || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
("if(%s&&m.hasOwnProperty(%j))", ref, field.name);
else gen
if (field.optional) gen
("if(%s!=null&&m.hasOwnProperty(%j))", ref, field.name); // !== undefined && !== null

}

if (wireType === undefined)
genTypePartial(gen, field, index, ref);
else gen
Expand Down
38 changes: 18 additions & 20 deletions src/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,33 +129,30 @@ function verifier(mtype) {
for (var i = 0; i < /* initializes */ mtype.fieldsArray.length; ++i) {
var field = mtype._fieldsArray[i].resolve(),
ref = "m" + util.safeProp(field.name);

if (field.optional) gen
("if(%s!=null&&m.hasOwnProperty(%j)){", ref, field.name); // !== undefined && !== null

// map fields
if (field.map) { gen
("if(%s!=null){", ref) // !== undefined && !== null
("if(!util.isObject(%s))", ref)
("return%j", invalid(field, "object"))
("var k=Object.keys(%s)", ref)
("for(var i=0;i<k.length;++i){");
genVerifyKey(gen, field, "k[i]");
genVerifyValue(gen, field, i, ref + "[k[i]]")
("}")
("if(!util.isObject(%s))", ref)
("return%j", invalid(field, "object"))
("var k=Object.keys(%s)", ref)
("for(var i=0;i<k.length;++i){");
genVerifyKey(gen, field, "k[i]");
genVerifyValue(gen, field, i, ref + "[k[i]]")
("}");

// repeated fields
} else if (field.repeated) { gen
("if(%s!=null){", ref) // !== undefined && !== null
("if(!Array.isArray(%s))", ref)
("return%j", invalid(field, "array"))
("for(var i=0;i<%s.length;++i){", ref);
genVerifyValue(gen, field, i, ref + "[i]")
("}")
("if(!Array.isArray(%s))", ref)
("return%j", invalid(field, "array"))
("for(var i=0;i<%s.length;++i){", ref);
genVerifyValue(gen, field, i, ref + "[i]")
("}");

// required or present fields
} else {
if (field.optional) gen
("if(%s!=null){", ref); // !== undefined && !== null
if (field.partOf) {
var oneofProp = util.safeProp(field.partOf.name);
if (seenFirstField[field.partOf.name] === 1) gen
Expand All @@ -165,11 +162,12 @@ function verifier(mtype) {
gen
("p%s=1", oneofProp);
}
genVerifyValue(gen, field, i, ref);
if (field.optional) gen
("}");
genVerifyValue(gen, field, i, ref);
}
} return gen
if (field.optional) gen
("}");
}
return gen
("return null");
/* eslint-enable no-unexpected-multiline */
}
6 changes: 3 additions & 3 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ $root.Test1 = (function() {
Test1.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.field1 != null)
if (message.field1 != null && message.hasOwnProperty("field1"))
if (!$util.isString(message.field1))
return "field1: string expected";
if (message.field2 != null)
if (message.field2 != null && message.hasOwnProperty("field2"))
if (!$util.isInteger(message.field2))
return "field2: integer expected";
if (message.field3 != null)
if (message.field3 != null && message.hasOwnProperty("field3"))
if (typeof message.field3 !== "boolean")
return "field3: boolean expected";
return null;
Expand Down
30 changes: 15 additions & 15 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,31 @@ $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)
if (message.stringRepeated != null && 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) {
if (message.uint64Repeated != null && 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"))
if (message.bytesVal != null && message.hasOwnProperty("bytesVal"))
writer.uint32(/* id 5, wireType 2 =*/42).bytes(message.bytesVal);
if (message.bytesRepeated && message.bytesRepeated.length)
if (message.bytesRepeated != null && 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) {
if (message.enumRepeated != null && 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]);
writer.ldelim();
}
if (message.int64Map && message.hasOwnProperty("int64Map"))
if (message.int64Map != null && message.hasOwnProperty("int64Map"))
for (var keys = Object.keys(message.int64Map), i = 0; i < keys.length; ++i)
writer.uint32(/* id 9, wireType 2 =*/74).fork().uint32(/* id 1, wireType 2 =*/10).string(keys[i]).uint32(/* id 2, wireType 0 =*/16).int64(message.int64Map[keys[i]]).ldelim();
return writer;
Expand Down Expand Up @@ -251,45 +251,45 @@ $root.Message = (function() {
Message.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.stringVal != null)
if (message.stringVal != null && message.hasOwnProperty("stringVal"))
if (!$util.isString(message.stringVal))
return "stringVal: string expected";
if (message.stringRepeated != null) {
if (message.stringRepeated != null && message.hasOwnProperty("stringRepeated")) {
if (!Array.isArray(message.stringRepeated))
return "stringRepeated: array expected";
for (var i = 0; i < message.stringRepeated.length; ++i)
if (!$util.isString(message.stringRepeated[i]))
return "stringRepeated: string[] expected";
}
if (message.uint64Val != null)
if (message.uint64Val != null && message.hasOwnProperty("uint64Val"))
if (!$util.isInteger(message.uint64Val) && !(message.uint64Val && $util.isInteger(message.uint64Val.low) && $util.isInteger(message.uint64Val.high)))
return "uint64Val: integer|Long expected";
if (message.uint64Repeated != null) {
if (message.uint64Repeated != null && message.hasOwnProperty("uint64Repeated")) {
if (!Array.isArray(message.uint64Repeated))
return "uint64Repeated: array expected";
for (var i = 0; i < message.uint64Repeated.length; ++i)
if (!$util.isInteger(message.uint64Repeated[i]) && !(message.uint64Repeated[i] && $util.isInteger(message.uint64Repeated[i].low) && $util.isInteger(message.uint64Repeated[i].high)))
return "uint64Repeated: integer|Long[] expected";
}
if (message.bytesVal != null)
if (message.bytesVal != null && message.hasOwnProperty("bytesVal"))
if (!(message.bytesVal && typeof message.bytesVal.length === "number" || $util.isString(message.bytesVal)))
return "bytesVal: buffer expected";
if (message.bytesRepeated != null) {
if (message.bytesRepeated != null && message.hasOwnProperty("bytesRepeated")) {
if (!Array.isArray(message.bytesRepeated))
return "bytesRepeated: array expected";
for (var i = 0; i < message.bytesRepeated.length; ++i)
if (!(message.bytesRepeated[i] && typeof message.bytesRepeated[i].length === "number" || $util.isString(message.bytesRepeated[i])))
return "bytesRepeated: buffer[] expected";
}
if (message.enumVal != null)
if (message.enumVal != null && message.hasOwnProperty("enumVal"))
switch (message.enumVal) {
default:
return "enumVal: enum value expected";
case 1:
case 2:
break;
}
if (message.enumRepeated != null) {
if (message.enumRepeated != null && message.hasOwnProperty("enumRepeated")) {
if (!Array.isArray(message.enumRepeated))
return "enumRepeated: array expected";
for (var i = 0; i < message.enumRepeated.length; ++i)
Expand All @@ -301,7 +301,7 @@ $root.Message = (function() {
break;
}
}
if (message.int64Map != null) {
if (message.int64Map != null && message.hasOwnProperty("int64Map")) {
if (!$util.isObject(message.int64Map))
return "int64Map: object expected";
var key = Object.keys(message.int64Map);
Expand Down
Loading

0 comments on commit 3d23eed

Please sign in to comment.