From c9999387c1110e61967c449b234dc138191dbaca Mon Sep 17 00:00:00 2001 From: Martin Tzvetanov Grigorov Date: Mon, 3 Oct 2022 14:52:24 +0300 Subject: [PATCH] AVRO-3631: Add support for ser_de Value::Fixed It is based on https://github.com/serde-rs/bytes/pull/28 which is not yet merged. Signed-off-by: Martin Tzvetanov Grigorov --- lang/rust/avro/src/de.rs | 5 +++-- lang/rust/avro/src/ser.rs | 15 +++++++++------ lang/rust/avro/src/types.rs | 17 ++++++++++------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs index 33cd5af9c1f..b354d042b77 100644 --- a/lang/rust/avro/src/de.rs +++ b/lang/rust/avro/src/de.rs @@ -668,6 +668,8 @@ mod tests { use serde::{Deserialize, Serialize}; use serial_test::serial; use std::sync::atomic::Ordering; + use serde::Serialize; + use serde_bytes::ByteArray; use uuid::Uuid; use apache_avro_test_helper::TestResult; @@ -1554,7 +1556,7 @@ mod tests { fn test_avro_3631_struct_fixed_field() { #[derive(Debug, Serialize, Deserialize)] struct TestStructFixedField { - field: [u8; 6], + field: ByteArray<6>, } let value = Value::Record(vec![( @@ -1562,6 +1564,5 @@ mod tests { Value::Fixed(6, vec![0, 0, 0, 0, 0, 0]), )]); let _deserialized: TestStructFixedField = from_value(&value).unwrap(); - } } diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs index 3cd01fe304b..9f7bd9afcff 100644 --- a/lang/rust/avro/src/ser.rs +++ b/lang/rust/avro/src/ser.rs @@ -174,7 +174,7 @@ impl<'b> ser::Serializer for &'b mut Serializer { } fn serialize_bytes(self, v: &[u8]) -> Result { - Ok(Value::Bytes(v.to_owned())) + Ok(Value::Fixed(v.len(), v.to_owned())) } fn serialize_none(self) -> Result { @@ -494,6 +494,7 @@ mod tests { use serde::{Deserialize, Serialize}; use serial_test::serial; use std::sync::atomic::Ordering; + use serde_bytes::ByteArray; #[derive(Debug, Deserialize, Serialize, Clone)] struct Test { @@ -686,7 +687,7 @@ mod tests { #[derive(Debug, Serialize, Deserialize)] struct TestStructFixedField { - field: [u8; 6], + field: ByteArray<6>, } #[test] @@ -1046,15 +1047,17 @@ mod tests { } #[test] - fn test_to_value_fixed_field_avro_3631() { - let test = TestStructFixedField { field: [1; 6] }; + fn avro_3631_test_to_value_fixed_field() { + let test = TestStructFixedField { + field: ByteArray::new([1; 6]), + }; let expected = Value::Record(vec![( "field".to_owned(), - Value::Fixed(6, Vec::from(test.field.clone())), + Value::Fixed(6, Vec::from(test.field.clone().into_array())), )]); assert_eq!( - to_value(test).unwrap(), expected, + to_value(test).unwrap(), "error serializing fixed array" ); } diff --git a/lang/rust/avro/src/types.rs b/lang/rust/avro/src/types.rs index eda9fe53416..84cbd76f516 100644 --- a/lang/rust/avro/src/types.rs +++ b/lang/rust/avro/src/types.rs @@ -1163,6 +1163,7 @@ mod tests { use pretty_assertions::assert_eq; use serde::{Deserialize, Serialize}; use serde_json::json; + use serde_bytes::ByteArray; use uuid::Uuid; #[test] @@ -3114,14 +3115,16 @@ Field with name '"b"' is not a member of the map items"#, ); } - #[derive(Debug, Serialize, Deserialize)] - struct TestStructFixedField { - field: [u8; 6], - } - #[test] - fn test_avro_3631_serialize_fixed_fields() { - let test = TestStructFixedField { field: [1; 6] }; + fn avro_3631_test_serialize_fixed_fields() { + #[derive(Debug, Serialize, Deserialize)] + struct TestStructFixedField { + field: ByteArray<6>, + } + + let test = TestStructFixedField { + field: ByteArray::new([1; 6]), + }; let value: Value = to_value(test).unwrap(); let schema = Schema::parse_str( r#"