Skip to content

Commit

Permalink
AVRO-3631: Add support for ser_de Value::Fixed
Browse files Browse the repository at this point in the history
It is based on serde-rs/bytes#28 which is not
yet merged.

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
martin-g committed Apr 9, 2024
1 parent 0102043 commit c999938
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lang/rust/avro/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1554,14 +1556,13 @@ 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![(
"field".to_string(),
Value::Fixed(6, vec![0, 0, 0, 0, 0, 0]),
)]);
let _deserialized: TestStructFixedField = from_value(&value).unwrap();

}
}
15 changes: 9 additions & 6 deletions lang/rust/avro/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'b> ser::Serializer for &'b mut Serializer {
}

fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
Ok(Value::Bytes(v.to_owned()))
Ok(Value::Fixed(v.len(), v.to_owned()))
}

fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -686,7 +687,7 @@ mod tests {

#[derive(Debug, Serialize, Deserialize)]
struct TestStructFixedField {
field: [u8; 6],
field: ByteArray<6>,
}

#[test]
Expand Down Expand Up @@ -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"
);
}
Expand Down
17 changes: 10 additions & 7 deletions lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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#"
Expand Down

0 comments on commit c999938

Please sign in to comment.