Skip to content

Commit

Permalink
Merge pull request #30 from Greg-GB/master
Browse files Browse the repository at this point in the history
Adding map schema type as an option to union schema types.
  • Loading branch information
mdlavin authored Apr 10, 2017
2 parents 6190ae4 + c541687 commit 1b9afd7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ DatumWriter.prototype = {
} else if (writersSchema.schemas[i].type === 'array' && writersSchema.schemas[i].validate(writersSchema.schemas[i].type, datum)) {
schemaIndex = i;
break;
} else if (writersSchema.schemas[i].type === 'map' && writersSchema.schemas[i].validate(writersSchema.schemas[i].type, datum)) {
schemaIndex = i;
break;
} else if (writersSchema.isPrimitive(writersSchema.schemas[i].type) && writersSchema.validate(writersSchema.schemas[i].type, datum)) {
schemaIndex = i;
break;
Expand Down
27 changes: 27 additions & 0 deletions test/io.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,33 @@ describe('IO', function(){
writer.write(record, encoder);
block.toBuffer()[0].should.equal(0);
});
it('should encode a union of a array with null type and map', function () {
var schema = Avro.Schema(
[
"null",
{
"type": "map",
"values": "string"
}
]
);
var block = DataFile.Block();
var writer = IO.DatumWriter(schema);
var encoder = IO.BinaryEncoder(block);
var record = {
"Archer": "Boop"
};

writer.write(record, encoder);
console.log(block.toBuffer().toJSON());
block.toBuffer().equals(new Buffer([ 2, 2, 12, 65, 114, 99, 104, 101, 114, 8, 66, 111, 111, 112, 0 ])).should.be.true;
block.toBuffer().slice(10,14).toString().should.equal(record.Archer);
block.flush();

var record = null;
writer.write(record, encoder);
block.toBuffer()[0].should.equal(0);
});



Expand Down

0 comments on commit 1b9afd7

Please sign in to comment.