Kriskras99 commented on issue #434:
URL: https://github.com/apache/avro-rs/issues/434#issuecomment-3796299670

   Maybe we could do the following:
   1. Extend `AvroSchemaComponent` with a `default` function
   ```rust
   /// Set the default value for this type if it is used in a record.
   ///
   /// Provided implementation returns `None` indicating that this type does 
not have a default value.
   fn default() -> Option<serde_json::Value> {
       None
   }
   ```
   2. Implement it for `Option<T>`
   ```rust
   impl<T: AvroSchemaComponent> AvroSchemaComponent for T {
       fn default() -> Option<serde_json::Value> {
           Some(serde_json::Value::Null)
       }
   }
   ```
   3. Enable users to derive the implementation by adding the container 
attribute `default`
   ```rust
   #[derive(AvroSchema)]
   #[avro(default = "A")]
   enum Foo {
       A,
       B,
   }
   ```
   4. Enable users to disable using the default for a type
   ```rust
   #[derive(AvroSchema)]
   struct Bar {
       #[avro(default = false)]
       foo: Foo
   }
   ```
   5. In the derive implementation we would first look if the user set a 
override via the field attribute `default` and then fall back to the trait. 
This would mean that all `Option`s would have a default in the next release.
   
   @martin-g what do you think?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to