avro-rs icon indicating copy to clipboard operation
avro-rs copied to clipboard

Auto Schema Creation from the struct

Open jdeschenes opened this issue 6 years ago • 2 comments

It would be beneficial to generate the schema automatically from the structure. For example,

#[derive(Serializable, Deserializable)]
struct Test {
    a: i32,
    b: f32,
}
let schema = schema::from_ser(Test::avro_schema()).unwrap();

I am not 100% sure on how the design should look like. Do you have any suggestions?

jdeschenes avatar Jan 04 '19 21:01 jdeschenes

Agreed that would be nice to have.

I remember giving some thought to it a few months ago:

  • Have a new trait:
pub trait AvroSchema {
  fn avro_schema(&self) -> Schema;
}
  • Extend the crate with a custom derive macro, using dyn to automatically generate code for the trait

Another approach (not as fancy as the first one, but maybe easier to implement) would be to implement a new Serializer (e.g: SchemaSerializer) which, instead of generating Values would generate the schema.

fn serialize_i64(&self, ...) -> ... {
  "long"
}

Not sure about the actual feasibility though 😄

flavray avatar Jan 07 '19 10:01 flavray

I tried the second approach because I could in principle do it inside my own crate, but it doesn't seem to be possible.

I required a Default implementation for the type to be serialized, then created a dummy default() value to pass through my Serializer. However, you don't get enough data that way to construct the schema. For example, to get the type of an optional value, you need a Some value, and to get the type of a sequence, you need at least one element in the sequence.

ttencate avatar Nov 20 '19 10:11 ttencate