lerouxrgd commented on PR #1900:
URL: https://github.com/apache/avro/pull/1900#issuecomment-2231224516

   I had a quick look at this, is there more to it than simply using 
`serde_bytes` on struct fields ?
   
   <details>
   <summary>
   Something like this patch
   </summary>
   
   ```diff
   diff --git a/lang/rust/Cargo.lock b/lang/rust/Cargo.lock
   index 8fce3d635..572e7bdc0 100644
   --- a/lang/rust/Cargo.lock
   +++ b/lang/rust/Cargo.lock
   @@ -92,6 +92,7 @@ dependencies = [
     "regex-lite",
     "rstest",
     "serde",
   + "serde_bytes",
     "serde_json",
     "serial_test",
     "sha2",
   @@ -1175,6 +1176,15 @@ dependencies = [
     "serde_derive",
    ]
    
   +[[package]]
   +name = "serde_bytes"
   +version = "0.11.15"
   +source = "registry+https://github.com/rust-lang/crates.io-index";
   +checksum = 
"387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a"
   +dependencies = [
   + "serde",
   +]
   +
    [[package]]
    name = "serde_derive"
    version = "1.0.204"
   diff --git a/lang/rust/Cargo.toml b/lang/rust/Cargo.toml
   index a6fd2ad48..e6b8b5767 100644
   --- a/lang/rust/Cargo.toml
   +++ b/lang/rust/Cargo.toml
   @@ -43,6 +43,7 @@ documentation = "https://docs.rs/apache-avro";
    [workspace.dependencies]
    log = { default-features = false, version = "0.4.22" }
    serde = { default-features = false, version = "1.0.204", features = 
["derive"] }
   +serde_bytes = { default-features = false, version = "0.11.15", features = 
["std"] }
    serde_json = { default-features = false, version = "1.0.120", features = 
["std"] }
    
    [profile.release.package.hello-wasm]
   diff --git a/lang/rust/avro/Cargo.toml b/lang/rust/avro/Cargo.toml
   index 27728bcf6..df5874d4e 100644
   --- a/lang/rust/avro/Cargo.toml
   +++ b/lang/rust/avro/Cargo.toml
   @@ -64,6 +64,7 @@ log = { workspace = true }
    num-bigint = { default-features = false, version = "0.4.6", features = 
["std", "serde"] }
    regex-lite = { default-features = false, version = "0.1.6", features = 
["std", "string"] }
    serde = { workspace = true }
   +serde_bytes = { workspace = true }
    serde_json = { workspace = true }
    snap = { default-features = false, version = "1.1.0", optional = true }
    strum = { default-features = false, version = "0.26.3" }
   diff --git a/lang/rust/avro/src/lib.rs b/lang/rust/avro/src/lib.rs
   index a68240600..3c31c1584 100644
   --- a/lang/rust/avro/src/lib.rs
   +++ b/lang/rust/avro/src/lib.rs
   @@ -872,6 +872,7 @@ pub use reader::{
    };
    pub use schema::{AvroSchema, Schema};
    pub use ser::to_value;
   +pub use serde_bytes;
    pub use util::{max_allocation_bytes, set_serde_human_readable};
    pub use uuid::Uuid;
    pub use writer::{
   diff --git a/lang/rust/avro/tests/avro-3631.rs 
b/lang/rust/avro/tests/avro-3631.rs
   new file mode 100644
   index 000000000..caa361eaf
   --- /dev/null
   +++ b/lang/rust/avro/tests/avro-3631.rs
   @@ -0,0 +1,108 @@
   +// Licensed to the Apache Software Foundation (ASF) under one
   +// or more contributor license agreements.  See the NOTICE file
   +// distributed with this work for additional information
   +// regarding copyright ownership.  The ASF licenses this file
   +// to you under the Apache License, Version 2.0 (the
   +// "License"); you may not use this file except in compliance
   +// with the License.  You may obtain a copy of the License at
   +//
   +//   http://www.apache.org/licenses/LICENSE-2.0
   +//
   +// Unless required by applicable law or agreed to in writing,
   +// software distributed under the License is distributed on an
   +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   +// KIND, either express or implied.  See the License for the
   +// specific language governing permissions and limitations
   +// under the License.
   +
   +use apache_avro::types::Value;
   +use apache_avro::{from_value, serde_bytes, to_value, Schema};
   +use serde::{Deserialize, Serialize};
   +
   +#[test]
   +fn avro_3631_test_serialize_fixed_fields() {
   +    #[derive(Debug, Serialize, Deserialize)]
   +    struct TestStructFixedField<'a> {
   +        #[serde(with = "serde_bytes")]
   +        bytes_field: &'a [u8],
   +
   +        #[serde(with = "serde_bytes")]
   +        vec_field: Vec<u8>,
   +
   +        #[serde(with = "serde_bytes")]
   +        fixed_field: [u8; 6],
   +    }
   +
   +    let test = TestStructFixedField {
   +        bytes_field: &[1, 2, 3],
   +        fixed_field: [1; 6],
   +        vec_field: vec![2, 3, 4],
   +    };
   +    let value: Value = to_value(test).unwrap();
   +    let schema = Schema::parse_str(
   +        r#"
   +            {
   +                "type": "record",
   +                "name": "TestStructFixedField",
   +                "fields": [
   +                    {
   +                        "name": "bytes_field",
   +                        "type": "bytes"
   +                    },
   +                    {
   +                        "name": "vec_field",
   +                        "type": "bytes"
   +                    },
   +                    {
   +                        "name": "fixed_field",
   +                        "type": {
   +                            "name": "fixed_field",
   +                            "type": "fixed",
   +                            "size": 6
   +                        }
   +                    }
   +                ]
   +            }
   +            "#,
   +    )
   +    .unwrap();
   +    assert!(value.validate(&schema));
   +}
   +
   +#[test]
   +fn avro_3631_deserialize_value_to_struct_with_byte_types() {
   +    #[derive(Debug, Deserialize, PartialEq)]
   +    struct TestStructFixedField {
   +        /// borrowed slices are not supported !
   +        // #[serde(with = "serde_bytes", borrow)]
   +        // slice_field: &'a [u8],
   +
   +        /// borrowed byte arrays are not supported !
   +        // #[serde(with = "serde_byte", borrow)]
   +        // borrowed_fixed_field: &'a [u8; 16],
   +
   +        #[serde(with = "serde_bytes")]
   +        vec_field: Vec<u8>,
   +
   +        #[serde(with = "serde_bytes")]
   +        fixed_field: [u8; 6],
   +    }
   +
   +    let expected = TestStructFixedField {
   +        // slice_field: &[1, 11, 111],
   +        // borrowed_fixed_field: &[2; 16],
   +        vec_field: vec![3, 33],
   +        fixed_field: [1; 6],
   +    };
   +
   +    let value = Value::Record(vec![
   +        // ("slice_field".to_owned(), ??? ) // needs something like 
Value::BytesSlice(&[u8])
   +        // ("borrowed_fixed_field".to_owned(), ??? ) // needs something 
like Value::FixedSlice(&[u8; 6])
   +        ("vec_field".to_owned(), Value::Bytes(vec![3, 33])),
   +        (
   +            "fixed_field".to_owned(),
   +            Value::Fixed(6, Vec::from(expected.fixed_field)),
   +        ),
   +    ]);
   +    assert_eq!(expected, from_value(&value).unwrap());
   +}
   
    ```
    
   </details>
   


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