Skip to content

Commit

Permalink
Use undefined instead of null as the default value (#112)
Browse files Browse the repository at this point in the history
* 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`.

* Fix tests
  • Loading branch information
Timmmm authored and mourner committed Nov 12, 2019
1 parent b4eab64 commit edcc792
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ') + '}';
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/compile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit edcc792

Please sign in to comment.