Skip to content

Commit

Permalink
java compatibility fix (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
davideicardi authored Oct 15, 2020
1 parent 8104735 commit 2a06c79
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object AvroSingleObjectEncoding {
* Note: this is not the same encoding using by Confluent Avro serializer.
* Confluent Avro serializer uses 0x00 as magic byte and and id of 4 bytes (incremental? not an hash).
*/
val default : AvroSingleObjectEncoding = {
val AVRO_OFFICIAL : AvroSingleObjectEncoding = {
new AvroSingleObjectEncoding(
Array(0xC3.toByte, 0x01.toByte),
ByteOrder.LITTLE_ENDIAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.davideicardi.kaa.SchemaNotFoundException
class AvroSingleObjectSerializer[T >: Null : SchemaFor : Encoder : Decoder]
(
schemaRegistry: SchemaRegistry,
encoding: AvroSingleObjectEncoding = AvroSingleObjectEncoding.default
encoding: AvroSingleObjectEncoding = AvroSingleObjectEncoding.AVRO_OFFICIAL
){
private val binarySerializer = new AvroBinarySerializer[T]()
private lazy val currentSchemaId = schemaRegistry.put(binarySerializer.currentSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import org.apache.avro.generic.GenericRecord
class GenericAvroSingleObjectSerializer
(
schemaRegistry: SchemaRegistry,
encoding: AvroSingleObjectEncoding = AvroSingleObjectEncoding.default
encoding: AvroSingleObjectEncoding,
){
def this(schemaRegistry: SchemaRegistry) = {
this(schemaRegistry, AvroSingleObjectEncoding.AVRO_OFFICIAL)
}

private val binarySerializer = new GenericAvroBinarySerializer()

Expand All @@ -19,7 +22,10 @@ class GenericAvroSingleObjectSerializer
encoding.encode(bytes, currentSchemaId)
}

def deserialize(bytes: Array[Byte], readerSchema: Option[Schema] = None): GenericRecord = {
def deserialize(bytes: Array[Byte]): GenericRecord = {
deserialize(bytes, None)
}
def deserialize(bytes: Array[Byte], readerSchema: Option[Schema]): GenericRecord = {
val (schemaId, serialized) = encoding.decode(bytes)
val schema = schemaRegistry.get(schemaId)
.getOrElse(throw new SchemaNotFoundException(s"Schema $schemaId not found in registry"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AvroSingleObjectSerializerSpec extends AnyFlatSpec with should.Matchers {
it should "serialize a class with a single object encoding" in {
val encoded = singleObjectSerializer.serialize(dragonite)

val (schemaId, bin) = AvroSingleObjectEncoding.default.decode(encoded)
val (schemaId, bin) = AvroSingleObjectEncoding.AVRO_OFFICIAL.decode(encoded)
val binarySerializer = new AvroBinarySerializer[Pokemon]

schemaId should be (AvroUtils.calcFingerprint(AvroSchema[Pokemon]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class GenericAvroSingleObjectSerializerSpec extends AnyFlatSpec with should.Matc

val encoded = singleObjectSerializer.serialize(record)

val (schemaId, bin) = AvroSingleObjectEncoding.default.decode(encoded)
val (schemaId, bin) = AvroSingleObjectEncoding.AVRO_OFFICIAL.decode(encoded)
val binarySerializer = new GenericAvroBinarySerializer

schemaId should be (AvroUtils.calcFingerprint(schemaV1))
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/scala/sampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ object SampleApp {
val bytesV2 = serializerV2.serialize(SuperheroV2("Spiderman", "Peter Parker"))

// v1 schema
val (schemaIdv1, _) = AvroSingleObjectEncoding.default.decode(bytesV1)
val (schemaIdv1, _) = AvroSingleObjectEncoding.AVRO_OFFICIAL.decode(bytesV1)
println(s"v1 $schemaIdv1")
val (schemaIdv2, _) = AvroSingleObjectEncoding.default.decode(bytesV2)
val (schemaIdv2, _) = AvroSingleObjectEncoding.AVRO_OFFICIAL.decode(bytesV2)
println(s"v2 $schemaIdv2")

// normal deserialization
Expand Down

0 comments on commit 2a06c79

Please sign in to comment.