Repository navigation
feat: Allow types to provide default values - #477
Conversation
`AvroSchemaComponent` is extended with a function `field_default`
which will be called when deriving a record to set the default value
for a field. The default implementation is to return `None`, which
means no default.
On the derive side, it is now possible to specify a default for a type
using `#[avro(default = "..")]`. It is also possible to disable setting
a default for a field with `#[avro(default = false)]`.
This enables users to use `#[serde(skip_serializing{_if})]` on most fields
without having to provide a default value.
martin-g
left a comment
There was a problem hiding this comment.
IMO FieldDefault::Disabled should be the default.
By default AvroSchema shouldn't add default to the generated Schema.
Only when the user application sets one (via Value(e.g. #[avro(default = XYZ)]) or via Trait (returning Some from field_default())) it should be added.
Kriskras99
left a comment
There was a problem hiding this comment.
My goal for this PR is to allow users to have defaults for fields with as little effort as possible.
That's why I implemented AvroSchemaComponent::field_default() as Some(Default::default()) for all types we implement.
I can understand if some users don't want to have a default for a field, which is why I added #[avro(default = false)] to disable getting a default for that field. This allows for usecases like in #434
Co-authored-by: Martin Grigorov <[email protected]>
810ebb3 to
40a4506
Compare
|
I've removed the |
5b306d5 to
3915546
Compare
…ame as the default implementation
|
@Kriskras99 Please check my commits |
|
Looks good! |
AvroSchemaComponentis extended with a functionfield_defaultwhich will be called when deriving a record to set the default value for a field. The default implementation is to returnNone, which means no default.On the derive side, it is now possible to specify a default for a type using
#[avro(default = "..")]. It is also possible to disable setting a default for a field with#[avro(default = false)].This enables users to use
#[serde(skip_serializing{_if})]on most fields without having to provide a default value.Closes #434