avro-kotlin / avro4k

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

Added support for decoding with avro alias #171

Closed trdw closed 5 months ago

trdw commented 7 months ago

Problem: We want to read some data using Avro evolution mechanism for field renaming. Current behavior does not account for AvroAlias annotation.

Proposed solution: fieldValue() function iterates through list of possible field names, starting with the resolvedFieldName. For each attempt, if record contains the field name, return the value returned from record.get(...), else continue.

Closes #170

Chuckame commented 5 months ago

Hello, sorry for the delay, I'll take a lot at it to see if it is not changing some other behavior. Thanks for the PR 🚀

Chuckame commented 5 months ago

AFAIK, without your PR, we are able to decode an old schema written with an alias while the kotlin data class uses the real field name (not alias).

What is not working is when you read a new schema where the kotlin data class is using an alias for its field name instead of the real field name. That's because apache avro library "normalize" the field name, and whatever the alias name or the real name, it creates a GenericRecord using field's real name (that is totally normal).

I don't see any particular issue, I approve it. @thake it's ok for you also ?

Chuckame commented 5 months ago

Side note: We should cache somewhere with a map like Map<alias, real name> to prevent useless aliases iterations. Let's plan it for v2 @thake