martin-g commented on code in PR #448:
URL: https://github.com/apache/avro-rs/pull/448#discussion_r2745671691
##########
avro/src/serde/derive.rs:
##########
@@ -82,7 +83,134 @@ pub trait AvroSchema {
///}
/// ```
pub trait AvroSchemaComponent {
+ /// Get the schema for this component
fn get_schema_in_ctxt(named_schemas: &mut Names, enclosing_namespace:
&Namespace) -> Schema;
+
+ /// Get the fields of this schema if it is a record.
+ ///
+ /// This returns `None` if the schema is not a record.
+ ///
+ /// The default implementation has to do a lot of extra work, so it is
strongly recommended to
+ /// implement this function when manually implementing this trait.
+ fn get_record_fields_in_ctxt(
+ named_schemas: &mut Names,
+ enclosing_namespace: &Namespace,
+ ) -> Option<Vec<RecordField>> {
+ get_record_fields_in_ctxt(named_schemas, enclosing_namespace,
Self::get_schema_in_ctxt)
+ }
+}
+
+/// Get the record fields from `schema_fn` without polluting `named_schemas`
or causing duplicate names
+///
+/// This is public so the derive macro can use it for `#[avro(with = ||)]` and
`#[avro(with = path)]`
+pub fn get_record_fields_in_ctxt(
+ named_schemas: &mut Names,
+ enclosing_namespace: &Namespace,
+ schema_fn: fn(named_schemas: &mut Names, enclosing_namespace: &Namespace)
-> Schema,
+) -> Option<Vec<RecordField>> {
+ let mut record = match schema_fn(named_schemas, enclosing_namespace) {
+ Schema::Record(record) => record,
+ Schema::Ref { name } => {
+ // This schema already exists in `named_schemas` so temporarily
remove it so we can
+ // get the actual schema.
+ let temp = named_schemas
+ .remove(&name)
+ .expect("Name should exist in `named_schemas` otherwise Ref is
invalid");
Review Comment:
Good!
--
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]