Closed gudjonragnar closed 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.
I think that prototype could provide a good grounds for a solution. I especially like the use of lazy static.
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?
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.
Fixed in 0.11.0
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.