Closed batconjurer closed 3 years ago
Thank you for this feature.
However I'd see the implementation more like this:
use_variant_access
to Templater
, defaults to falseUNION_TEMPLATE
#[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize{%- if use_variant_access %},variant_access_derive::VariantAccess{%- endif %})]
use_variant_access
through GeneratorBuilder
(like precision and nullable options)Also, do you need to add use variant_access_traits::*;
in the generated file ? I think it is only necessary in your main (or where you use the actual generated code) isn't it ?
Besides, there is no need for a cargo feature.
Let me know if you want to give the it a try with the above plan, otherwise I should be able to do it this week.
I have made the above changes. Please double check the docopt string to make sure it is clear. I also needed to make a quick patch to the variant_access crate so that the use variant_access_traits::*;
line was not necessary in the generated code. So this means that anyone using this feature should use variant_access version 0.3.1
or later.
Perfect, thank you ! This has been released in 0.9.1
.
Recently, this crate added support for Avro union types (hooray!), but was forced to create enums with computer generated names and computer generated field names and it is not clear how to avoid that. There are unfortunately a few downsides to that.
Type and fields names can be long an unwieldy. This means that users will have to put up with a constantly typing out these names. In the best case, the define a type alias somewhere, in the worst case, they might modify the generated code.
To address this, I've been working on a crate called variant_access which allows interaction with Rust enums in a way that avoids specifically writing out the type name. It is modeled on the C++ API for
std::variant
. This can be derived using a macro, but use by the end user will also require them to use thevariant_access_traits
crate. For this reason, I've included this as an opt-in feature.I have included an example of an input avro schema (that I stole from one of the unit tests), and some code interacting with the generated types, both with and without the
variant_access_traits
.The input schema:
The generate code:
[serde(default)]
[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
pub struct Contact { pub extra: ::std::collections::HashMap<String, Option>,
}
impl Default for Contact { fn default() -> Contact { Contact { extra: ::std::collections::HashMap::new(), } } }