This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro-rs.git
The following commit(s) were added to refs/heads/main by this push:
new d82d820 feat: Expose the capability to do headerless avro encoding
with serde (#193)
d82d820 is described below
commit d82d820cb99ad12dadaeb0f91b30c733350c4113
Author: Allen Benz <[email protected]>
AuthorDate: Mon May 5 21:41:59 2025 -0700
feat: Expose the capability to do headerless avro encoding with serde (#193)
* Expose the capability to do headerless avro encoding with serde
* Added a unit test
formatted code
fixed up a couple comments
* Minor cleanups
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
* make write_avro_datum_ref public
---------
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Co-authored-by: Martin Tzvetanov Grigorov <[email protected]>
---
avro/src/lib.rs | 4 ++--
avro/src/writer.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 50 insertions(+), 6 deletions(-)
diff --git a/avro/src/lib.rs b/avro/src/lib.rs
index 930541a..be2a14a 100644
--- a/avro/src/lib.rs
+++ b/avro/src/lib.rs
@@ -906,8 +906,8 @@ pub use ser::to_value;
pub use util::{max_allocation_bytes, set_serde_human_readable};
pub use uuid::Uuid;
pub use writer::{
- to_avro_datum, to_avro_datum_schemata, GenericSingleObjectWriter,
SpecificSingleObjectWriter,
- Writer,
+ to_avro_datum, to_avro_datum_schemata, write_avro_datum_ref,
GenericSingleObjectWriter,
+ SpecificSingleObjectWriter, Writer,
};
#[cfg(feature = "derive")]
diff --git a/avro/src/writer.rs b/avro/src/writer.rs
index 7406e48..c7d3141 100644
--- a/avro/src/writer.rs
+++ b/avro/src/writer.rs
@@ -571,7 +571,7 @@ impl<T> SpecificSingleObjectWriter<T>
where
T: AvroSchema + Serialize,
{
- /// Write the referenced `Serialize` object to the provided Write object.
Returns a result with
+ /// Write the referenced `Serialize` object to the provided `Write`
object. Returns a result with
/// the number of bytes written including the header
pub fn write_ref<W: Write>(&mut self, data: &T, writer: &mut W) ->
AvroResult<usize> {
let mut bytes_written: usize = 0;
@@ -583,9 +583,7 @@ where
self.header_written = true;
}
- let names: HashMap<Name, &Schema> = HashMap::new();
- let mut serializer = SchemaAwareWriteSerializer::new(writer,
&self.schema, &names, None);
- bytes_written += data.serialize(&mut serializer)?;
+ bytes_written += write_avro_datum_ref(&self.schema, data, writer)?;
Ok(bytes_written)
}
@@ -658,6 +656,23 @@ pub fn to_avro_datum<T: Into<Value>>(schema: &Schema,
value: T) -> AvroResult<Ve
Ok(buffer)
}
+/// Write the referenced [Serialize]able object to the provided [Write] object.
+/// Returns a result with the number of bytes written.
+///
+/// **NOTE** This function has a quite small niche of usage and does **NOT**
generate headers and sync
+/// markers; use [`append_ser`](struct.Writer.html#method.append_ser) to be
fully Avro-compatible
+/// if you don't know what you are doing, instead.
+pub fn write_avro_datum_ref<T: Serialize, W: Write>(
+ schema: &Schema,
+ data: &T,
+ writer: &mut W,
+) -> AvroResult<usize> {
+ let names: HashMap<Name, &Schema> = HashMap::new();
+ let mut serializer = SchemaAwareWriteSerializer::new(writer, schema,
&names, None);
+ let bytes_written = data.serialize(&mut serializer)?;
+ Ok(bytes_written)
+}
+
/// Encode a compatible value (implementing the `ToAvro` trait) into Avro
format, also
/// performing schema validation.
/// If the provided `schema` is incomplete then its dependencies must be
@@ -734,6 +749,7 @@ mod tests {
]
}
"#;
+
const UNION_SCHEMA: &str = r#"["null", "long"]"#;
#[test]
@@ -753,6 +769,34 @@ mod tests {
Ok(())
}
+ #[test]
+ fn avro_rs_193_write_avro_datum_ref() -> TestResult {
+ #[derive(Serialize)]
+ struct TestStruct {
+ a: i64,
+ b: String,
+ }
+
+ let schema = Schema::parse_str(SCHEMA)?;
+ let mut writer: Vec<u8> = Vec::new();
+ let data = TestStruct {
+ a: 27,
+ b: "foo".to_string(),
+ };
+
+ let mut expected = Vec::new();
+ zig_i64(27, &mut expected)?;
+ zig_i64(3, &mut expected)?;
+ expected.extend([b'f', b'o', b'o']);
+
+ let bytes = write_avro_datum_ref(&schema, &data, &mut writer)?;
+
+ assert_eq!(bytes, expected.len());
+ assert_eq!(writer, expected);
+
+ Ok(())
+ }
+
#[test]
fn test_union_not_null() -> TestResult {
let schema = Schema::parse_str(UNION_SCHEMA)?;