From 7b723c1b492649120d971f279046cb1b2401bbf3 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 6 Nov 2019 18:08:21 +0000 Subject: [PATCH 1/2] Use undefined instead of null as default value This fixes the type issues in #107. Also it makes more sense. I'm unsure about the `return undefined`. --- compile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compile.js b/compile.js index e4e4007..c09ae25 100644 --- a/compile.js +++ b/compile.js @@ -101,7 +101,7 @@ function compileDest(ctx) { for (var i = 0; i < ctx._proto.fields.length; i++) { var field = ctx._proto.fields[i]; props[field.name + ': ' + JSON.stringify(ctx._defaults[field.name])] = true; - if (field.oneof) props[field.oneof + ': null'] = true; + if (field.oneof) props[field.oneof + ': undefined'] = true; } return '{' + Object.keys(props).join(', ') + '}'; } @@ -295,7 +295,7 @@ function getDefaultValue(field, value) { case 'string': return value || ''; case 'bool': return value === 'true'; case 'map': return {}; - default: return null; + default: return undefined; } } From 3104c7b2dc8c699b3e72ce4a07f9e65cc42b0600 Mon Sep 17 00:00:00 2001 From: Tim Hutt Date: Thu, 7 Nov 2019 12:41:20 +0000 Subject: [PATCH 2/2] Fix tests --- test/compile.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/compile.test.js b/test/compile.test.js index 4d7bd52..4021bf6 100644 --- a/test/compile.test.js +++ b/test/compile.test.js @@ -222,8 +222,8 @@ test('handles all implicit default values', function(t) { t.equals(data.id, 0); t.deepEqual(data.tags, []); t.deepEqual(data.numbers, []); - t.equals(data.bytes, null); - t.equals(data.custom, null); + t.equals(data.bytes, undefined); + t.equals(data.custom, undefined); t.deepEqual(data.types, []); t.end(); @@ -237,7 +237,7 @@ test('sets oneof field name', function(t) { Envelope.write({}, pbf); var data = Envelope.read(new Pbf(pbf.finish())); - t.equals(data.value, null); + t.equals(data.value, undefined); t.equals(data.id, 0); pbf = new Pbf();