avro-kotlin / avro4k

Avro format support for Kotlin
Apache License 2.0
198 stars 37 forks source link

Use Kotlinx.Serialization BinaryFormat Interface #223

Closed SmarTTYs closed 4 months ago

SmarTTYs commented 5 months ago

Hi in advance, thanks for the great work in avro4k 2.0 so far.

Is your feature request related to a problem? Please describe. I am currently developing a simple library at my workplace which also contains some bindings between the java kafka library and kotlinx.serialization. This library provides the ability to use Kotlin.serialization BinaryFormat's in order to create Headers and Serializers / Deserializers. I want the core of the library to be as abstract as possible (in the scope of Kotlin.serialization) and therefor I don't want to provide any specific format implementation (like avro4k). The user should decide whether to use Protobuf / Avro or what ever binary representation for their encoding / decoding. The 1.0 of Avro4k used Kotlin.serialization's BinaryFormat interface which allowed me to access extensions functions defined in kotlinx.serialization core.

Describe the solution you'd like Avro4k 2.0 should implement the BinaryFormat like the other binary formats (f.e. Protobuf) do.

Describe alternatives you've considered Currently I am wrapping an Avro instance like this:

open class AvroBinary(private val avro: Avro) : BinaryFormat {
    override val serializersModule: SerializersModule
        get() = avro.serializersModule

    override fun <T> encodeToByteArray(serializer: SerializationStrategy<T>, value: T): ByteArray {
        val schema = avro.schema(serializer.descriptor)
        return avro.encodeToByteArray(schema, serializer, value)
    }

    override fun <T> decodeFromByteArray(deserializer: DeserializationStrategy<T>, bytes: ByteArray): T {
        val schema = avro.schema(deserializer.descriptor)
        return avro.decodeFromByteArray(schema, deserializer, bytes)
    }

    companion object Default : AvroBinary(Avro)
}

Additional context As the Avro-schema gets constructed by passing a SerialDescriptor instance it should not be very hard to implement the interface. Of course, I am also willing to open a PR with the required changes :)

Chuckame commented 5 months ago

Hello, thanks for the feedback.

I stopped using the BinaryFormat as it is indicated as an internal interface of kotlinx.serialization. But I agree implementing it could simplify abstraction.

You can provide a PR yes.

Btw, there is already extension methods with this signature, so you should not need to call schema method yourself

Chuckame commented 5 months ago

I've did it, wait for the next release candidate. Do you also have some feedback regarding the avro4k v2 release?

Chuckame commented 5 months ago

Has been released in 2.0.0-rc6

SmarTTYs commented 5 months ago

Hi Thanks for the quick implementation! :)

I am currently testing out the new Avro4k 2.0 release candidates and I am very happy with the changes so far. If I notice anything, I will of course open an issue.

This issue can be closed for all I care

Edit: Next week I will be switching some projects in our development environment to Avro4k 2.0. If I find a bug, I will report it as soon as possible

Chuckame commented 5 months ago

Thanks, I'll close it when v2 is released (not in release candidate).