Skip to content

Commit

Permalink
Support optional fields using question mark syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
yehonatanz committed Jan 16, 2024
1 parent 9330777 commit 3cfd2bc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ class Reader {
delete schema.type; // Always overwrite the type.
utils.copyOwnProperties(ref, schema);
}
return Object.keys(schema).length > 1 ? schema : schema.type;
const isOptional = this._tk.next({id: 'operator', val: '?', silent: true});
const result = Object.keys(schema).length > 1 ? schema : schema.type;
return isOptional ? ['null', result]: result;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/test_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ suite('specs', () => {
);
});

test('optional field', () => {
const usingQuestionMark = readSchema('record { int? optionalInt; }');
const usingUnion = readSchema('record { union{null,int} optionalInt; }');
assert.deepEqual(usingQuestionMark, usingUnion);
});

});

suite('readProtocol', () => {
Expand Down

0 comments on commit 3cfd2bc

Please sign in to comment.