From b734152cfbc28e63c5cde37e663214115440e630 Mon Sep 17 00:00:00 2001 From: Assaf Oren Date: Sun, 15 Apr 2018 16:51:05 +0300 Subject: [PATCH 1/5] Converter: Fix 'bytes' field decoding into an empty array, now decoded into an empty Buffer --- src/converter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/converter.js b/src/converter.js index 074610285..08db61892 100644 --- a/src/converter.js +++ b/src/converter.js @@ -245,7 +245,7 @@ converter.toObject = function toObject(mtype) { ("}else") ("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber()); else if (field.bytes) gen - ("d%s=o.bytes===String?%j:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]"); + ("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]", "Buffer.from([])"); else gen ("d%s=%j", prop, field.typeDefault); // also messages (=null) } gen From 025045ff28df09d7137a565262c1c073d04c3e9c Mon Sep 17 00:00:00 2001 From: Assaf Oren Date: Sun, 15 Apr 2018 17:22:30 +0300 Subject: [PATCH 2/5] Converter: fix tests (Buffer.from -> util.newBuffer) --- src/converter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/converter.js b/src/converter.js index 08db61892..4fa1e1833 100644 --- a/src/converter.js +++ b/src/converter.js @@ -245,7 +245,7 @@ converter.toObject = function toObject(mtype) { ("}else") ("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber()); else if (field.bytes) gen - ("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]", "Buffer.from([])"); + ("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]", "util.newBuffer([])"); else gen ("d%s=%j", prop, field.typeDefault); // also messages (=null) } gen From feafa546401e1c4acf6951f1d66c2807cd07180d Mon Sep 17 00:00:00 2001 From: Assaf Oren Date: Tue, 17 Apr 2018 17:57:59 +0300 Subject: [PATCH 3/5] CR Fix: converter.toObject, use default types value for 'bytes' fields when wrapping with 'util.newBuffer' --- src/converter.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/converter.js b/src/converter.js index 4fa1e1833..535defcd5 100644 --- a/src/converter.js +++ b/src/converter.js @@ -244,9 +244,11 @@ converter.toObject = function toObject(mtype) { ("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():n", prop) ("}else") ("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber()); - else if (field.bytes) gen - ("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]", "util.newBuffer([])"); - else gen + else if (field.bytes) { + var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]"; + gen + ("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), arrayDefault, "util.newBuffer(" + arrayDefault + ")"); + } else gen ("d%s=%j", prop, field.typeDefault); // also messages (=null) } gen ("}"); From 2f0c5d9cd4155f8985b1c94eafb232baf61183ba Mon Sep 17 00:00:00 2001 From: Assaf Oren Date: Tue, 17 Apr 2018 22:43:10 +0300 Subject: [PATCH 4/5] CR Fix: converter.toObject, create array default types value once in generated code --- src/converter.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/converter.js b/src/converter.js index 535defcd5..358399063 100644 --- a/src/converter.js +++ b/src/converter.js @@ -247,7 +247,11 @@ converter.toObject = function toObject(mtype) { else if (field.bytes) { var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]"; gen - ("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), arrayDefault, "util.newBuffer(" + arrayDefault + ")"); + ("if(o.bytes===String) d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault)) + ("else {") + ("d%s=%s", prop, arrayDefault) + ("if(o.bytes!==Array) d%s=util.newBuffer(d%s)", prop, prop) + ("}") } else gen ("d%s=%j", prop, field.typeDefault); // also messages (=null) } gen From 719662bc55df520ecb93007e35fb8bbf3e2abcb0 Mon Sep 17 00:00:00 2001 From: Assaf Oren Date: Wed, 25 Apr 2018 22:40:55 +0300 Subject: [PATCH 5/5] Converter: remove whitespaces --- src/converter.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/converter.js b/src/converter.js index 358399063..f819123f5 100644 --- a/src/converter.js +++ b/src/converter.js @@ -247,10 +247,10 @@ converter.toObject = function toObject(mtype) { else if (field.bytes) { var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]"; gen - ("if(o.bytes===String) d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault)) - ("else {") + ("if(o.bytes===String)d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault)) + ("else{") ("d%s=%s", prop, arrayDefault) - ("if(o.bytes!==Array) d%s=util.newBuffer(d%s)", prop, prop) + ("if(o.bytes!==Array)d%s=util.newBuffer(d%s)", prop, prop) ("}") } else gen ("d%s=%j", prop, field.typeDefault); // also messages (=null)