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 2023/05/31 12:58:59 UTC

[avro] branch master updated: AVRO-3764: [Rust]: Add resolve method with schemata for an automatic Schema::Ref resolving (#2262)

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

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new cdfd66fed AVRO-3764: [Rust]: Add resolve method with schemata for an automatic Schema::Ref resolving (#2262)
cdfd66fed is described below

commit cdfd66fed1cb366400a41aa7dcbec19d1fad8a09
Author: theo <43...@users.noreply.github.com>
AuthorDate: Wed May 31 15:58:53 2023 +0300

    AVRO-3764: [Rust]: Add resolve method with schemata for an automatic Schema::Ref resolving (#2262)
    
    * feat: add draft method to resolve with schemata
    
    * feat: add test for resolving with schemata
    
    * AVRO-3764: Minor improvements in the test
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    
    ---------
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    Co-authored-by: Fedor Telnov <f....@picodata.io>
    Co-authored-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/avro/src/types.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/lang/rust/avro/src/types.rs b/lang/rust/avro/src/types.rs
index d44826be1..93e81e98e 100644
--- a/lang/rust/avro/src/types.rs
+++ b/lang/rust/avro/src/types.rs
@@ -561,6 +561,18 @@ impl Value {
         self.resolve_internal(schema, rs.get_names(), &enclosing_namespace)
     }
 
+    /// Attempt to perform schema resolution on the value, with the given
+    /// [Schema](../schema/enum.Schema.html) and set of schemas to use for Refs resolution.
+    ///
+    /// See [Schema Resolution](https://avro.apache.org/docs/current/spec.html#Schema+Resolution)
+    /// in the Avro specification for the full set of rules of schema
+    /// resolution.
+    pub fn resolve_schemata(self, schema: &Schema, schemata: Vec<&Schema>) -> AvroResult<Self> {
+        let enclosing_namespace = schema.namespace();
+        let rs = ResolvedSchema::try_from(schemata)?;
+        self.resolve_internal(schema, rs.get_names(), &enclosing_namespace)
+    }
+
     fn resolve_internal(
         mut self,
         schema: &Schema,
@@ -2638,4 +2650,44 @@ Field with name '"b"' is not a member of the map items"#,
     fn test_avro_3688_field_b_set() {
         avro_3688_schema_resolution_panic(true);
     }
+
+    #[test]
+    fn test_avro_3764_use_resolve_schemata() {
+        let referenced_schema =
+            r#"{"name": "enumForReference", "type": "enum", "symbols": ["A", "B"]}"#;
+        let main_schema = r#"{"name": "recordWithReference", "type": "record", "fields": [{"name": "reference", "type": "enumForReference"}]}"#;
+
+        let value: serde_json::Value = serde_json::from_str(
+            r#"
+            {
+                "reference": "A"
+            }
+        "#,
+        )
+        .unwrap();
+
+        let avro_value = Value::from(value);
+
+        let schemas = Schema::parse_list(&[main_schema, referenced_schema]).unwrap();
+
+        let main_schema = schemas.get(0).unwrap();
+        let schemata: Vec<_> = schemas.iter().skip(1).collect();
+
+        let resolve_result = avro_value
+            .clone()
+            .resolve_schemata(main_schema, schemata.clone());
+
+        assert!(
+            resolve_result.is_ok(),
+            "result of resolving with schemata should be ok, got: {:?}",
+            resolve_result
+        );
+
+        let resolve_result = avro_value.resolve(main_schema);
+        assert!(
+            resolve_result.is_err(),
+            "result of resolving without schemata should be err, got: {:?}",
+            resolve_result
+        );
+    }
 }