Repository navigation
feat(derive): allow overriding the schema for a field and change the schema for uuid::Uuid - #397
Conversation
|
For the |
I prefer the no-quotes one. It looks cleaner and more type safe to me. Although the quoted one in Serde is equally type safe, I guess. |
8028f15 to
3ee13c8
Compare
|
Accepting a closure seems to be quite difficult, looking at the Bon code. Let's not do that for now |
|
Please add a test for deriving a Schema for: #[derive(AvroSchema)]
struct Foo {
#[serde(rename="baz")]
bar: uuid::Uuid
} |
1146996 to
21ba64b
Compare
21ba64b to
8df071b
Compare
8df071b to
10e8522
Compare
| With::Serde(path) => { | ||
| quote! {#path::get_schema_in_ctxt(named_schemas, enclosing_namespace)} | ||
| } | ||
| With::Expr(expr) => quote! {#expr(named_schemas, enclosing_namespace)}, |
There was a problem hiding this comment.
Is there anything else that mandates the expr to be a function ?
I don't see any restrictions in avro.rs and mod.rs to verify that the provided expression is a syn::Expr::Call.
My main question is: What leads to expected function in errors like error[E0618]: expected function, found apache_avro::Schema ?
I am experimenting with
quote! {#path::get_schema_in_ctxt(named_schemas, enclosing_namespace)}
}
- With::Expr(expr) => quote! {#expr(named_schemas, enclosing_namespace)},
+ With::Expr(expr) => match expr {
+ Expr::Closure(closure) => {
+ dbg!("==========");
+ quote! {#closure()}
+ }
+ _ => quote! {#expr(named_schemas, enclosing_namespace)},
+ },
};but it seems something earlier makes the check for a function.
There was a problem hiding this comment.
Actually the dbg!() code is executed ! I'm continuing my debug session
There was a problem hiding this comment.
- With::Expr(expr) => quote! {#expr(named_schemas, enclosing_namespace)},
+ With::Expr(expr) => match expr {
+ Expr::Closure(closure) => {
+ let inputs = &closure.inputs;
+ let body = &closure.body;
+ dbg!("==========", &closure);
+ dbg!(">>>>>", quote! {#body});
+ quote! {#body}
+ }
+ _ => quote! {#expr(named_schemas, enclosing_namespace)},
+ },
};This seems to work for || Schema::Bytes !
I guess we could make it work also for |named_schemas, enclosing_namespace| Schema::Bytes
There was a problem hiding this comment.
I have pushed the support for lambda/closure.
I think we should not support inputs for now.
How about supporting also #[avro(with = Schema::Bytes)] too ? Seems reasonable to me.
Using the lambda should be good enough!
There was a problem hiding this comment.
I don't think it's possible to distinguish between a function path path::to::function and a constructor Schema::Bytes.
Nice work adding the lambda!
|
@martin-g I've added validation that the closure has no inputs, and added an explicit error if the expression is not a function or a closure. |
A user can now override the schema used by
#[derive(AvroSchema)]using#[avro(with)].If no argument is supplied to
with, then it will use the same module as specified in#[serde(with = ".."]. It is a compile error if the Serde attribute is missing.If an argument is supplied to
with, it will call this function with the same arguments asAvroSchemaComponent::get_schema_in_ctxt.I've also replaced the
AvroSchemaComponentimplementation foruuid::Uuidwith theSchema::Uuid(UuidSchema::Fixed)representation. Users who want the old schema can use#[avro(with)]to revert back.User-facing documentation will follow in a separate PR, which also contains an overhaul of the entire Serde/derive documentation of these crates.