Skip to content

Commit

Permalink
New: Optional fields handle null just like undefined regardless of ty…
Browse files Browse the repository at this point in the history
…pe see #709
  • Loading branch information
dcodeIO committed Mar 20, 2017
1 parent fe91765 commit 228c882
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function encoder(mtype) {
else if (field.bytes || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
("if(%s&&m.hasOwnProperty(%j))", ref, field.name);
else gen
("if(%s!==undefined&&m.hasOwnProperty(%j))", ref, field.name);
("if(%s!==undefined&&%s!==null&&m.hasOwnProperty(%j))", ref, ref, field.name);

}

Expand Down
6 changes: 3 additions & 3 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ $root.Test1 = (function() {
Test1.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.field1 !== undefined && message.hasOwnProperty("field1"))
if (message.field1 !== undefined && message.field1 !== null && message.hasOwnProperty("field1"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.field1);
if (message.field2 !== undefined && message.hasOwnProperty("field2"))
if (message.field2 !== undefined && message.field2 !== null && message.hasOwnProperty("field2"))
writer.uint32(/* id 2, wireType 0 =*/16).uint32(message.field2);
if (message.field3 !== undefined && message.hasOwnProperty("field3"))
if (message.field3 !== undefined && message.field3 !== null && message.hasOwnProperty("field3"))
writer.uint32(/* id 3, wireType 0 =*/24).bool(message.field3);
return writer;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ $root.Message = (function() {
Message.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.stringVal !== undefined && message.hasOwnProperty("stringVal"))
if (message.stringVal !== undefined && message.stringVal !== null && message.hasOwnProperty("stringVal"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.stringVal);
if (message.stringRepeated !== undefined && message.hasOwnProperty("stringRepeated"))
for (var i = 0; i < message.stringRepeated.length; ++i)
Expand All @@ -122,7 +122,7 @@ $root.Message = (function() {
if (message.bytesRepeated !== undefined && message.hasOwnProperty("bytesRepeated"))
for (var i = 0; i < message.bytesRepeated.length; ++i)
writer.uint32(/* id 6, wireType 2 =*/50).bytes(message.bytesRepeated[i]);
if (message.enumVal !== undefined && message.hasOwnProperty("enumVal"))
if (message.enumVal !== undefined && 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")) {
writer.uint32(/* id 8, wireType 2 =*/66).fork();
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 @@ -302,19 +302,19 @@ $root.vector_tile = (function() {
Value.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.stringValue !== undefined && message.hasOwnProperty("stringValue"))
if (message.stringValue !== undefined && message.stringValue !== null && message.hasOwnProperty("stringValue"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.stringValue);
if (message.floatValue !== undefined && message.hasOwnProperty("floatValue"))
if (message.floatValue !== undefined && message.floatValue !== null && message.hasOwnProperty("floatValue"))
writer.uint32(/* id 2, wireType 5 =*/21).float(message.floatValue);
if (message.doubleValue !== undefined && message.hasOwnProperty("doubleValue"))
if (message.doubleValue !== undefined && message.doubleValue !== null && message.hasOwnProperty("doubleValue"))
writer.uint32(/* id 3, wireType 1 =*/25).double(message.doubleValue);
if (message.intValue !== undefined && message.intValue !== null && message.hasOwnProperty("intValue"))
writer.uint32(/* id 4, wireType 0 =*/32).int64(message.intValue);
if (message.uintValue !== undefined && message.uintValue !== null && message.hasOwnProperty("uintValue"))
writer.uint32(/* id 5, wireType 0 =*/40).uint64(message.uintValue);
if (message.sintValue !== undefined && message.sintValue !== null && message.hasOwnProperty("sintValue"))
writer.uint32(/* id 6, wireType 0 =*/48).sint64(message.sintValue);
if (message.boolValue !== undefined && message.hasOwnProperty("boolValue"))
if (message.boolValue !== undefined && message.boolValue !== null && message.hasOwnProperty("boolValue"))
writer.uint32(/* id 7, wireType 0 =*/56).bool(message.boolValue);
return writer;
};
Expand Down Expand Up @@ -620,7 +620,7 @@ $root.vector_tile = (function() {
writer.uint32(message.tags[i]);
writer.ldelim();
}
if (message.type !== undefined && message.hasOwnProperty("type"))
if (message.type !== undefined && 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")) {
writer.uint32(/* id 4, wireType 2 =*/34).fork();
Expand Down Expand Up @@ -949,7 +949,7 @@ $root.vector_tile = (function() {
if (message.values !== undefined && message.hasOwnProperty("values"))
for (var i = 0; i < message.values.length; ++i)
$types[4].encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
if (message.extent !== undefined && message.hasOwnProperty("extent"))
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
writer.uint32(/* id 5, wireType 0 =*/40).uint32(message.extent);
writer.uint32(/* id 15, wireType 0 =*/120).uint32(message.version);
return writer;
Expand Down
24 changes: 12 additions & 12 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,26 @@ $root.Package = (function() {
Package.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.name !== undefined && message.hasOwnProperty("name"))
if (message.name !== undefined && message.name !== null && message.hasOwnProperty("name"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
if (message.version !== undefined && message.hasOwnProperty("version"))
if (message.version !== undefined && message.version !== null && message.hasOwnProperty("version"))
writer.uint32(/* id 2, wireType 2 =*/18).string(message.version);
if (message.description !== undefined && message.hasOwnProperty("description"))
if (message.description !== undefined && message.description !== null && message.hasOwnProperty("description"))
writer.uint32(/* id 3, wireType 2 =*/26).string(message.description);
if (message.author !== undefined && message.hasOwnProperty("author"))
if (message.author !== undefined && message.author !== null && message.hasOwnProperty("author"))
writer.uint32(/* id 4, wireType 2 =*/34).string(message.author);
if (message.license !== undefined && message.hasOwnProperty("license"))
if (message.license !== undefined && message.license !== null && message.hasOwnProperty("license"))
writer.uint32(/* id 5, wireType 2 =*/42).string(message.license);
if (message.repository && message.hasOwnProperty("repository"))
$types[6].encode(message.repository, writer.uint32(/* id 6, wireType 2 =*/50).fork()).ldelim();
if (message.bugs !== undefined && message.hasOwnProperty("bugs"))
if (message.bugs !== undefined && message.bugs !== null && message.hasOwnProperty("bugs"))
writer.uint32(/* id 7, wireType 2 =*/58).string(message.bugs);
if (message.homepage !== undefined && message.hasOwnProperty("homepage"))
if (message.homepage !== undefined && message.homepage !== null && message.hasOwnProperty("homepage"))
writer.uint32(/* id 8, wireType 2 =*/66).string(message.homepage);
if (message.keywords !== undefined && message.hasOwnProperty("keywords"))
for (var i = 0; i < message.keywords.length; ++i)
writer.uint32(/* id 9, wireType 2 =*/74).string(message.keywords[i]);
if (message.main !== undefined && message.hasOwnProperty("main"))
if (message.main !== undefined && message.main !== null && message.hasOwnProperty("main"))
writer.uint32(/* id 10, wireType 2 =*/82).string(message.main);
if (message.bin && message.hasOwnProperty("bin"))
for (var keys = Object.keys(message.bin), i = 0; i < keys.length; ++i)
Expand All @@ -193,12 +193,12 @@ $root.Package = (function() {
if (message.devDependencies && message.hasOwnProperty("devDependencies"))
for (var keys = Object.keys(message.devDependencies), i = 0; i < keys.length; ++i)
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 !== undefined && message.hasOwnProperty("types"))
if (message.types !== undefined && message.types !== null && message.hasOwnProperty("types"))
writer.uint32(/* id 17, wireType 2 =*/138).string(message.types);
if (message.cliDependencies !== undefined && message.hasOwnProperty("cliDependencies"))
for (var i = 0; i < message.cliDependencies.length; ++i)
writer.uint32(/* id 18, wireType 2 =*/146).string(message.cliDependencies[i]);
if (message.versionScheme !== undefined && message.hasOwnProperty("versionScheme"))
if (message.versionScheme !== undefined && message.versionScheme !== null && message.hasOwnProperty("versionScheme"))
writer.uint32(/* id 19, wireType 2 =*/154).string(message.versionScheme);
return writer;
};
Expand Down Expand Up @@ -682,9 +682,9 @@ $root.Package = (function() {
Repository.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.type !== undefined && message.hasOwnProperty("type"))
if (message.type !== undefined && message.type !== null && message.hasOwnProperty("type"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.type);
if (message.url !== undefined && message.hasOwnProperty("url"))
if (message.url !== undefined && message.url !== null && message.hasOwnProperty("url"))
writer.uint32(/* id 2, wireType 2 =*/18).string(message.url);
return writer;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ $root.MyRequest = (function() {
MyRequest.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.path !== undefined && message.hasOwnProperty("path"))
if (message.path !== undefined && message.path !== null && message.hasOwnProperty("path"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.path);
return writer;
};
Expand Down Expand Up @@ -274,7 +274,7 @@ $root.MyResponse = (function() {
MyResponse.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.status !== undefined && message.hasOwnProperty("status"))
if (message.status !== undefined && message.status !== null && message.hasOwnProperty("status"))
writer.uint32(/* id 2, wireType 0 =*/16).int32(message.status);
return writer;
};
Expand Down
Loading

0 comments on commit 228c882

Please sign in to comment.