martin-g commented on code in PR #2583: URL: https://github.com/apache/avro/pull/2583#discussion_r1391063952
########## lang/rust/avro/tests/union_schema.rs: ########## @@ -0,0 +1,307 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use serde::{Deserialize, Serialize}; +use serde::de::DeserializeOwned; +use apache_avro::{from_value, Schema, Writer, Reader, Codec}; + + +static SCHEMA_A_STR: &str = r#"{ + "name": "A", + "type": "record", + "fields": [ + {"name": "field_a", "type": "float"} + ] + }"#; + +static SCHEMA_B_STR: &str = r#"{ + "name": "B", + "type": "record", + "fields": [ + {"name": "field_b", "type": "long"} + ] + }"#; + +static SCHEMA_C_STR: &str = r#"{ + "name": "C", + "type": "record", + "fields": [ + {"name": "field_union", "type": ["A", "B"]}, + {"name": "field_c", "type": "string"} + ] + }"#; + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct A { + field_a: f32, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct B { + field_b: i64, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +#[serde(untagged)] +enum UnionAB { + A(A), + B(B), +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct C { + field_union: UnionAB, + field_c: String +} + +fn encode_decode<T> (input: &T,schema: &Schema,schemata: &Vec<Schema>) -> T Review Comment: It would be better to return a `AvroResult<T>` here and replace as many as possible `.unwrap()` calls with `?` ########## lang/rust/avro/tests/union_schema.rs: ########## @@ -0,0 +1,307 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use serde::{Deserialize, Serialize}; +use serde::de::DeserializeOwned; +use apache_avro::{from_value, Schema, Writer, Reader, Codec}; + + +static SCHEMA_A_STR: &str = r#"{ + "name": "A", + "type": "record", + "fields": [ + {"name": "field_a", "type": "float"} + ] + }"#; + +static SCHEMA_B_STR: &str = r#"{ + "name": "B", + "type": "record", + "fields": [ + {"name": "field_b", "type": "long"} + ] + }"#; + +static SCHEMA_C_STR: &str = r#"{ + "name": "C", + "type": "record", + "fields": [ + {"name": "field_union", "type": ["A", "B"]}, + {"name": "field_c", "type": "string"} + ] + }"#; + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct A { + field_a: f32, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct B { + field_b: i64, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +#[serde(untagged)] +enum UnionAB { + A(A), + B(B), +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct C { + field_union: UnionAB, + field_c: String +} + +fn encode_decode<T> (input: &T,schema: &Schema,schemata: &Vec<Schema>) -> T + where T: DeserializeOwned + Serialize { + let mut encoded: Vec<u8> = Vec::new(); + let mut writer = Writer::with_schemata(&schema, schemata.iter().collect(), &mut encoded, Codec::Null); + writer.append_ser(input).unwrap(); + writer.flush().unwrap(); + + let mut reader = Reader::with_schemata(schema, schemata.iter().collect(), encoded.as_slice()).unwrap(); + from_value::<T>(&reader.next().unwrap().unwrap()).unwrap() +} + + +#[test] +fn union_schema_round_trip_no_null() { Review Comment: ```suggestion fn test_avro3901_union_schema_round_trip_no_null() { ``` ########## lang/rust/avro/src/encode.rs: ########## @@ -242,11 +252,26 @@ pub(crate) fn encode_internal<S: Borrow<Schema>>( )); } } + } else if let Schema::Union(UnionSchema{ schemas, .. }) = schema { + let original_size = buffer.len(); + for (index,s) in schemas.iter().enumerate() { + encode_long(index as i64, buffer); + match encode_internal(value, s, names, enclosing_namespace, buffer) { + Ok(_) => return Ok(()), + Err(_) => { + buffer.truncate(original_size); //undo any partial encoding Review Comment: Should we exit/break the loop in this case ? ########## lang/rust/avro/tests/union_schema.rs: ########## @@ -0,0 +1,307 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use serde::{Deserialize, Serialize}; +use serde::de::DeserializeOwned; +use apache_avro::{from_value, Schema, Writer, Reader, Codec}; + + +static SCHEMA_A_STR: &str = r#"{ + "name": "A", + "type": "record", + "fields": [ + {"name": "field_a", "type": "float"} + ] + }"#; + +static SCHEMA_B_STR: &str = r#"{ + "name": "B", + "type": "record", + "fields": [ + {"name": "field_b", "type": "long"} + ] + }"#; + +static SCHEMA_C_STR: &str = r#"{ + "name": "C", + "type": "record", + "fields": [ + {"name": "field_union", "type": ["A", "B"]}, + {"name": "field_c", "type": "string"} + ] + }"#; + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct A { + field_a: f32, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct B { + field_b: i64, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +#[serde(untagged)] +enum UnionAB { + A(A), + B(B), +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct C { + field_union: UnionAB, + field_c: String +} + +fn encode_decode<T> (input: &T,schema: &Schema,schemata: &Vec<Schema>) -> T + where T: DeserializeOwned + Serialize { + let mut encoded: Vec<u8> = Vec::new(); + let mut writer = Writer::with_schemata(&schema, schemata.iter().collect(), &mut encoded, Codec::Null); + writer.append_ser(input).unwrap(); + writer.flush().unwrap(); + + let mut reader = Reader::with_schemata(schema, schemata.iter().collect(), encoded.as_slice()).unwrap(); + from_value::<T>(&reader.next().unwrap().unwrap()).unwrap() +} + + +#[test] +fn union_schema_round_trip_no_null() { + let schemata: Vec<Schema> = Schema::parse_list(&[SCHEMA_A_STR, SCHEMA_B_STR, SCHEMA_C_STR]).expect("parsing schemata"); + + { + let input = C { field_union: (UnionAB::A(A { field_a: 45.5 })), field_c: "foo".to_string() }; + let output = encode_decode(&input,&schemata[2],&schemata); + assert_eq!(input,output); + } + { + let input = C { field_union: (UnionAB::B(B { field_b: 73 })), field_c: "bar".to_string() }; + let output = encode_decode(&input,&schemata[2],&schemata); + assert_eq!(input,output); + } +} + +static SCHEMA_D_STR: &str = r#"{ + "name": "D", + "type": "record", + "fields": [ + {"name": "field_union", "type": ["null", "A", "B"]}, + {"name": "field_d", "type": "string"} + ] + }"#; + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +#[serde(untagged)] +enum UnionNoneAB { + None, + A(A), + B(B), +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct D { + field_union: UnionNoneAB, + field_d: String +} + +#[test] +fn union_schema_round_trip_null_at_start() { Review Comment: ```suggestion fn test_avro3901_union_schema_round_trip_null_at_start() { ``` ########## lang/rust/avro/tests/union_schema.rs: ########## @@ -0,0 +1,307 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use serde::{Deserialize, Serialize}; +use serde::de::DeserializeOwned; +use apache_avro::{from_value, Schema, Writer, Reader, Codec}; + + +static SCHEMA_A_STR: &str = r#"{ + "name": "A", + "type": "record", + "fields": [ + {"name": "field_a", "type": "float"} + ] + }"#; + +static SCHEMA_B_STR: &str = r#"{ + "name": "B", + "type": "record", + "fields": [ + {"name": "field_b", "type": "long"} + ] + }"#; + +static SCHEMA_C_STR: &str = r#"{ + "name": "C", + "type": "record", + "fields": [ + {"name": "field_union", "type": ["A", "B"]}, + {"name": "field_c", "type": "string"} + ] + }"#; + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct A { + field_a: f32, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct B { + field_b: i64, +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +#[serde(untagged)] +enum UnionAB { + A(A), + B(B), +} + +#[derive(Serialize,Deserialize,Clone, PartialEq, Debug)] +struct C { + field_union: UnionAB, + field_c: String +} + +fn encode_decode<T> (input: &T,schema: &Schema,schemata: &Vec<Schema>) -> T + where T: DeserializeOwned + Serialize { + let mut encoded: Vec<u8> = Vec::new(); + let mut writer = Writer::with_schemata(&schema, schemata.iter().collect(), &mut encoded, Codec::Null); + writer.append_ser(input).unwrap(); + writer.flush().unwrap(); + + let mut reader = Reader::with_schemata(schema, schemata.iter().collect(), encoded.as_slice()).unwrap(); + from_value::<T>(&reader.next().unwrap().unwrap()).unwrap() +} + + +#[test] +fn union_schema_round_trip_no_null() { + let schemata: Vec<Schema> = Schema::parse_list(&[SCHEMA_A_STR, SCHEMA_B_STR, SCHEMA_C_STR]).expect("parsing schemata"); + + { Review Comment: No need of the scoping. Rust is fine with shadowing. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
