lerouxrgd / rsgen-avro

Command line and library for generating Rust types from Avro schemas
MIT License
37 stars 29 forks source link

Add support for deserializing from avro-rs unions #19

Closed codehearts closed 3 years ago

codehearts commented 3 years ago

The use_avro_rs_unions option has been added to generate a custom serde::Deserialize implementation which can deserialize from avro-rs union types. Note that only int, long, float, double, and boolean values can be deserialized, strings and other types are not supported.

This is a pretty rough pass. It'll support the ["null", "int", "long"] union I'm trying to support in my own codebase, but it doesn't support strings because of the deserialize_any implementation in avro-rs. I also don't think this implementation could support variants with the same Rust type (i.e. Int and Date) because it doesn't know the variant name or the type being parsed.

I looked into implementing a Deserializer that could parse the avro_rs::types::Value enum instead of using avro-rs's deserializer, but enum parsing looks rather complex and I wasn't sure how to do it. I'll keep trying and will update this PR if I come up with anything.

lerouxrgd commented 3 years ago

I think this is a really good solution given what is doable with avro-rs !

If you're happy with it I think we can merge it, but if you'd like to spend more time on it there's no problem.

codehearts commented 3 years ago

I'd be happy to go ahead and merge it, it can always be improved further in the future as code changes and discussion happens on my avro-rs ticket!

lerouxrgd commented 3 years ago

I've released this in 0.9.6.

Thanks again !