Skip to content

Commit

Permalink
Merge pull request #99 from cit-consulting/default_avro
Browse files Browse the repository at this point in the history
Use UUIDSerializer as contextual in Avro.default
  • Loading branch information
thake committed Apr 13, 2021
2 parents f91bdda + 9bdd638 commit bf7066f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.github.avrokotlin.avro4k.serializer.UUIDSerializer
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.contextual
import org.apache.avro.Schema
import org.apache.avro.file.CodecFactory
import org.apache.avro.generic.GenericRecord
Expand All @@ -18,7 +19,6 @@ import java.nio.ByteBuffer
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.*

open class AvroInputStreamBuilder<T>(
private val converter: (Any) -> T
Expand Down Expand Up @@ -125,9 +125,7 @@ class Avro(
constructor(configuration: AvroConfiguration) : this(defaultModule, configuration)
companion object {
val defaultModule = SerializersModule {
mapOf(
UUID::class to UUIDSerializer()
)
contextual(UUIDSerializer())
}
val default = Avro(defaultModule)

Expand Down Expand Up @@ -271,4 +269,4 @@ class Avro(
return schema(serializer.descriptor)
}

}
}
20 changes: 20 additions & 0 deletions src/test/kotlin/com/github/avrokotlin/avro4k/DefaultAvroTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.avrokotlin.avro4k

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import java.util.UUID

class DefaultAvroTest : FunSpec({

test("encoding UUID") {

@Serializable
data class Foo(@Contextual val a: UUID)

val uuid = UUID.randomUUID()
val record = Avro.default.toRecord(Foo.serializer(), Foo(uuid))
record.get("a").toString() shouldBe uuid.toString()
}
})

0 comments on commit bf7066f

Please sign in to comment.