lerouxrgd / rsgen-avro

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

Suggestion: Include schema in generated object #23

Closed gudjonragnar closed 2 years ago

gudjonragnar commented 2 years ago

Would it be possible to include the schema in the generated struct? This could then be used to e.g. validate the struct against the schema or write the struct to avro file.

lerouxrgd commented 2 years ago

It could be useful to add it indeed.

That could be gated by a flag (like your derive-builders param). I think someone made a prototype of it in the past but it didn't get contributed back here.

gudjonragnar commented 2 years ago

I think that prototype could provide a good grounds for a solution. I especially like the use of lazy static.

landreussi commented 2 years ago

Apache Avro team recently added in https://github.com/apache/avro/pull/1631 a derive macro that allows you to retrieve the Schema from the derived struct (IDK if this solves the issue):

#[derive(Debug, Serialize, AvroSchema)]
struct Test {
    a: i64,
    b: String,
}
// derived schema, always valid or code fails to compile with a descriptive message
let schema = Test::get_schema();

As I could see, you're already migrating to the apache version in #26, we could also derive AvroSchema in generated types, WDYT?

jklamer commented 2 years ago

For this, you absolutely could call the derive macro, but you don't need to! You could generate an implementation for the AvroSchema trait with the raw schema string much like I did here: https://github.com/apache/avro/blob/master/lang/rust/avro/src/writer.rs#L1084-L1111 . I don't know what would be easier lift. The advantage of implementing with that trait would be to get compatibility with the SpecificSingleObjectReader/Writer out of the box. (provided that the generated structs implement serde::Serialize/serde::Deserialize of course) The newest crate should be released soon and will let you know when that happens.

lerouxrgd commented 2 years ago

Fixed in 0.11.0