You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/12/14 23:28:27 UTC

[avro] 02/02: AVRO-3683: WIP compiles and all tests pass

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch avro-3683-multiple-schemas
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 4cbafabb17cc9ac5b45c7808198471a1fab72048
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Thu Dec 15 01:27:51 2022 +0200

    AVRO-3683: WIP compiles and all tests pass
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/avro/src/encode.rs | 16 ----------------
 lang/rust/avro/src/schema.rs |  1 +
 lang/rust/avro/src/types.rs  |  1 +
 lang/rust/avro/src/writer.rs | 21 ++++++++++++++-------
 4 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/lang/rust/avro/src/encode.rs b/lang/rust/avro/src/encode.rs
index 3cdb067fe..2ae48f91c 100644
--- a/lang/rust/avro/src/encode.rs
+++ b/lang/rust/avro/src/encode.rs
@@ -37,22 +37,6 @@ pub fn encode(value: &Value, schema: &Schema, buffer: &mut Vec<u8>) -> AvroResul
     encode_internal(value, schema, rs.get_names(), &None, buffer)
 }
 
-pub fn encode_schemata(
-    value: &Value,
-    schemata: &[&Schema],
-    buffer: &mut Vec<u8>,
-) -> AvroResult<()> {
-    let rs = ResolvedSchema::try_from(schemata)?;
-    for schema in schemata {
-        if value.validate(schema) {
-            encode_internal(value, schema, rs.get_names(), &None, buffer)?;
-            break;
-        }
-    }
-
-    todo!("Err(None of the provided schemata matched the value)")
-}
-
 fn encode_bytes<B: AsRef<[u8]> + ?Sized>(s: &B, buffer: &mut Vec<u8>) {
     let bytes = s.as_ref();
     encode_long(bytes.len() as i64, buffer);
diff --git a/lang/rust/avro/src/schema.rs b/lang/rust/avro/src/schema.rs
index 1ffb9e6ea..48b284eef 100644
--- a/lang/rust/avro/src/schema.rs
+++ b/lang/rust/avro/src/schema.rs
@@ -399,6 +399,7 @@ impl Serialize for Alias {
     }
 }
 
+#[derive(Debug)]
 pub(crate) struct ResolvedSchema<'s> {
     names_ref: NamesRef<'s>,
     schemata: Vec<&'s Schema>,
diff --git a/lang/rust/avro/src/types.rs b/lang/rust/avro/src/types.rs
index ba18f7714..0755ad6e6 100644
--- a/lang/rust/avro/src/types.rs
+++ b/lang/rust/avro/src/types.rs
@@ -380,6 +380,7 @@ impl Value {
                 let name = name.fully_qualified_name(enclosing_namespace);
                 names.get(&name).map_or_else(
                     || {
+                        eprintln!("Schema not found: {:?}", &name);
                         Some(format!(
                             "Unresolved schema reference: '{:?}'. Parsed names: {:?}",
                             name,
diff --git a/lang/rust/avro/src/writer.rs b/lang/rust/avro/src/writer.rs
index 6264e07de..2dba23e33 100644
--- a/lang/rust/avro/src/writer.rs
+++ b/lang/rust/avro/src/writer.rs
@@ -16,7 +16,6 @@
 // under the License.
 
 //! Logic handling writing in Avro format at user level.
-use crate::encode::encode_schemata;
 use crate::{
     encode::{encode, encode_internal, encode_to_vec},
     rabin::Rabin,
@@ -377,17 +376,24 @@ fn write_avro_datum<T: Into<Value>>(
     Ok(())
 }
 
+// TODO: document and add tests
 fn write_avro_datum_schemata<T: Into<Value>>(
     schemata: &[&Schema],
     value: T,
     buffer: &mut Vec<u8>,
-) -> Result<(), Error> {
+) -> AvroResult<()> {
     let avro = value.into();
-    if !avro.validate_schemata(schemata) {
-        return Err(Error::Validation);
+    let rs = ResolvedSchema::try_from(schemata)?;
+    for schema in schemata {
+        if avro
+            .validate_internal(schema, rs.get_names(), &schema.namespace())
+            .is_none()
+        {
+            encode_internal(&avro, schema, rs.get_names(), &schema.namespace(), buffer)?;
+            return Ok(());
+        }
     }
-    encode_schemata(&avro, schemata, buffer)?;
-    Ok(())
+    return Err(Error::Validation);
 }
 
 /// Writer that encodes messages according to the single object encoding v1 spec
@@ -556,6 +562,7 @@ pub fn to_avro_datum<T: Into<Value>>(schema: &Schema, value: T) -> AvroResult<Ve
     Ok(buffer)
 }
 
+// TODO: document and add tests
 pub fn to_avro_datum_schemata<T: Into<Value>>(
     schemata: &[&Schema],
     value: T,
@@ -1292,7 +1299,7 @@ mod tests {
             Value::Record(vec![("field_a".into(), Value::Float(1.0))]),
         )]);
 
-        let expected: Vec<u8> = Vec::new();
+        let expected: Vec<u8> = vec![0, 0, 128, 63];
         assert_eq!(
             to_avro_datum_schemata(&schemata.as_slice(), record).unwrap(),
             expected