avro-kotlin / avro4k

Avro format support for Kotlin
Apache License 2.0
188 stars 36 forks source link

Value classes not working (feature request ?) #131

Closed Chuckame closed 2 months ago

Chuckame commented 1 year ago

Hey,

Is there a way to have value classes ? (previously called inlined classes)

The goal is to wrap primitive types (string, int, in our example) inside a value class, to be used with a primitive avro schema.

error:

Non-serializable class kotlin.String is not supported by class com.github.avrokotlin.avro4k.encoder.RootRecordEncoder encoder

To reproduce:

@JvmInline
@Serializable
value class StringValueClass(val value: String)

val kotlinSerializer = serializer<StringValueClass>() // working well
val avroSchema = Avro.default.schema(kotlinSerializer) // working well, creates a "string" schema
Avro.default.toRecord(kotlinSerializer,avroSchema, StringValueClass("awesome"))

I can help if you want, since this feature should be awesome for handling primitive keys while having a readable code (instead of ask "hey, what's inside the string key ? hexadecimal ? the name ? from whhich other field it comes ?" 😄 )

thake commented 1 year ago

Should be solved together with #80 as both cases require an extension of the RootRecordEncoder. The problem is that primitives are not wrappable in an Avro record. Therefore the API of avro4k needs to change to support this use case. I've not yet had the time to come up with a good API design that supports both the use cases (record and primitives). Feel free to contribute a proposal.

thake commented 1 year ago

Maybe #116 can also be solved together. The API change should be capable of this.

Chuckame commented 1 year ago

I begun my work on avro4k to handle primitive types, but as you said, this lib is totally independant from kafka libs. This lib is more an avro encoder, while we need another lib for kafka. I think this lib is not dedicated for this kind of thing, because it will be too much generic. Like a decode func that will returns a T? or Any? that is not type-safe.

Maybe the best is to really have 2 different kafka serializers: one for records, and another for primitives. I'll create a PR if needed on your other avro4k-kafka-serializer lib

Chuckame commented 5 months ago

I just discovered that AvroFixed can be put on a value class as the documentation say. Since the value classes would be natively handled, then this annotation should just be possible on properties.

Chuckame commented 2 months ago

Done in #183