Kriskras99 commented on code in PR #359:
URL: https://github.com/apache/avro-rs/pull/359#discussion_r2602116294


##########
avro_derive/src/lib.rs:
##########
@@ -142,26 +144,46 @@ fn get_data_struct_schema_def(
     let mut record_field_exprs = vec![];
     match s.fields {
         syn::Fields::Named(ref a) => {
-            let mut index: usize = 0;
             for field in a.named.iter() {
-                let mut name = field.ident.as_ref().unwrap().to_string(); // 
we know everything has a name
+                let mut name = field
+                    .ident
+                    .as_ref()
+                    .expect("Field must have a name")
+                    .to_string();
                 if let Some(raw_name) = name.strip_prefix("r#") {
                     name = raw_name.to_string();
                 }
                 let field_attrs =
-                    
FieldOptions::from_attributes(&field.attrs[..]).map_err(darling_to_syn)?;
+                    
FieldOptions::from_attributes(&field.attrs).map_err(darling_to_syn)?;
                 let doc =
                     preserve_optional(field_attrs.doc.or_else(|| 
extract_outer_doc(&field.attrs)));
                 match (field_attrs.rename, rename_all) {
                     (Some(rename), _) => {
                         name = rename;
                     }
-                    (None, rename_all) if !matches!(rename_all, 
RenameRule::None) => {
+                    (None, rename_all) if rename_all != RenameRule::None => {
                         name = rename_all.apply_to_field(&name);
                     }
                     _ => {}
                 }
-                if let Some(true) = field_attrs.skip {
+                if Some(true) == field_attrs.skip {
+                    continue;
+                } else if Some(true) == field_attrs.flatten {
+                    // Inline the fields of the child record at runtime, as we 
don't have access to
+                    // the schema here.
+                    let flatten_ty = &field.ty;
+                    record_field_exprs.push(quote! {
+                        if let 
::apache_avro::schema::Schema::Record(::apache_avro::schema::RecordSchema { 
fields, .. }) = #flatten_ty::get_schema() {
+                            for mut field in fields {
+                                field.position = schema_fields.len();
+                                schema_fields.push(field)
+                            }
+                        } else {
+                            panic!("Can only flatten RecordSchema")

Review Comment:
   This panic is happening at runtime (when the user/library calls 
`get_schema`). The function signature is `AvroSchema::get_schema() -> Schema` 
so we can't return an error either.
   We could add a test that just calls `get_schema()`, then the user will see 
it when they run `cargo test`. I've added it to #361 



-- 
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