Scala Avro library
Currently in active development
Auto-generate Avro schemas from case classes
type CP = String :+: Boolean :+: Int :+: CNil
case class CoproductUnion(cupcat: CP)
AvroSchema[CoproductUnion].schema
Will, if successful give a Right containing an Avro schema
Right({
"type": "record",
"name": "CoproductUnion",
"namespace": "unit.schema.CoproductUnionSpec",
"doc": "",
"fields": [{
"name": "cupcat",
"type": ["int", "boolean", "string"]
}]
})
Decode an Avro GenericRecord to a case class
case class RecordWithMultipleFields(field1: Boolean, field2: String, field3: Int)
case class ReaderStringRecord(field2: String)
AvroSchema[RecordWithMultipleFields].schema.map { writerSchema =>
val genericRecord = new GenericData.Record(writerSchema)
genericRecord.put(0, true)
genericRecord.put(1, "cupcat")
genericRecord.put(2, 45)
genericRecord
}.flatMap(record => Decoder.decode[ReaderStringRecord](record))
Will, if successful, decode the relevent fields of the GenericRecord to your caseclass, using the provided reader schema
Right(ReaderStringRecord("cupcat"))
Encode a case class to an Avro GenericRecord
case class RecordWithUnion(field: Option[String])
val record = RecordWithUnion("cupcat".some)
Encoder.encode[RecordWithUnion](record)
Will, if successful, encode the case class to a Right of GenericRecord
Right({"field":"cupcat"})