PookieBuns commented on code in PR #337:
URL: https://github.com/apache/avro-rs/pull/337#discussion_r2547943321
##########
avro/src/ser_schema.rs:
##########
@@ -2770,4 +2745,161 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn test_serialize_union_record_variant() -> TestResult {
+ let schema = Schema::parse_str(
+ r#"{
+ "type": "record",
+ "name": "TestRecord",
+ "fields": [{
+ "name": "innerUnion", "type": [
+ {"type": "record", "name": "innerRecordFoo", "fields": [
+ {"name": "foo", "type": "string"}
+ ]},
+ {"type": "record", "name": "innerRecordBar", "fields": [
+ {"name": "bar", "type": "string"}
+ ]},
+ {"name": "intField", "type": "int"},
+ {"name": "stringField", "type": "string"}
+ ]
+ }]
+ }"#,
+ )?;
+
+ #[derive(Serialize)]
+ #[serde(rename_all = "camelCase")]
+ struct TestRecord {
+ inner_union: InnerUnion,
+ }
+
+ #[derive(Serialize)]
+ #[serde(untagged)]
+ enum InnerUnion {
+ InnerVariantFoo(InnerRecordFoo),
+ InnerVariantBar(InnerRecordBar),
+ IntField(i32),
+ StringField(String),
+ }
+
+ #[derive(Serialize)]
+ #[serde(rename = "innerRecordFoo")]
+ struct InnerRecordFoo {
+ foo: String,
+ }
+
+ #[derive(Serialize)]
+ #[serde(rename = "innerRecordBar")]
+ struct InnerRecordBar {
+ bar: String,
+ }
+
+ assert!(!crate::util::is_human_readable());
+ let mut buffer: Vec<u8> = Vec::new();
+ let rs = ResolvedSchema::try_from(&schema)?;
+ let mut serializer =
+ SchemaAwareWriteSerializer::new(&mut buffer, &schema,
rs.get_names(), None);
+
+ let foo_record = TestRecord {
+ inner_union: InnerUnion::InnerVariantFoo(InnerRecordFoo {
+ foo: String::from("foo"),
+ }),
+ };
+ foo_record.serialize(&mut serializer)?;
+ let bar_record = TestRecord {
+ inner_union: InnerUnion::InnerVariantBar(InnerRecordBar {
+ bar: String::from("bar"),
+ }),
+ };
+ bar_record.serialize(&mut serializer)?;
+ let int_record = TestRecord {
+ inner_union: InnerUnion::IntField(1),
+ };
+ int_record.serialize(&mut serializer)?;
+ let string_record = TestRecord {
+ inner_union: InnerUnion::StringField(String::from("string")),
+ };
+ string_record.serialize(&mut serializer)?;
+ Ok(())
+ }
+
+ #[test]
+ fn test_serialize_option_union_record_variant() -> TestResult {
Review Comment:
Updated!
##########
avro/src/ser_schema.rs:
##########
@@ -2770,4 +2745,161 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn test_serialize_union_record_variant() -> TestResult {
+ let schema = Schema::parse_str(
+ r#"{
+ "type": "record",
+ "name": "TestRecord",
+ "fields": [{
+ "name": "innerUnion", "type": [
+ {"type": "record", "name": "innerRecordFoo", "fields": [
+ {"name": "foo", "type": "string"}
+ ]},
+ {"type": "record", "name": "innerRecordBar", "fields": [
+ {"name": "bar", "type": "string"}
+ ]},
+ {"name": "intField", "type": "int"},
+ {"name": "stringField", "type": "string"}
+ ]
+ }]
+ }"#,
+ )?;
+
+ #[derive(Serialize)]
+ #[serde(rename_all = "camelCase")]
+ struct TestRecord {
+ inner_union: InnerUnion,
+ }
+
+ #[derive(Serialize)]
+ #[serde(untagged)]
+ enum InnerUnion {
+ InnerVariantFoo(InnerRecordFoo),
+ InnerVariantBar(InnerRecordBar),
+ IntField(i32),
+ StringField(String),
+ }
+
+ #[derive(Serialize)]
+ #[serde(rename = "innerRecordFoo")]
+ struct InnerRecordFoo {
+ foo: String,
+ }
+
+ #[derive(Serialize)]
+ #[serde(rename = "innerRecordBar")]
+ struct InnerRecordBar {
+ bar: String,
+ }
+
+ assert!(!crate::util::is_human_readable());
+ let mut buffer: Vec<u8> = Vec::new();
+ let rs = ResolvedSchema::try_from(&schema)?;
+ let mut serializer =
+ SchemaAwareWriteSerializer::new(&mut buffer, &schema,
rs.get_names(), None);
+
+ let foo_record = TestRecord {
+ inner_union: InnerUnion::InnerVariantFoo(InnerRecordFoo {
+ foo: String::from("foo"),
+ }),
+ };
+ foo_record.serialize(&mut serializer)?;
+ let bar_record = TestRecord {
+ inner_union: InnerUnion::InnerVariantBar(InnerRecordBar {
+ bar: String::from("bar"),
+ }),
+ };
+ bar_record.serialize(&mut serializer)?;
+ let int_record = TestRecord {
+ inner_union: InnerUnion::IntField(1),
+ };
+ int_record.serialize(&mut serializer)?;
+ let string_record = TestRecord {
+ inner_union: InnerUnion::StringField(String::from("string")),
+ };
+ string_record.serialize(&mut serializer)?;
+ Ok(())
+ }
+
+ #[test]
+ fn test_serialize_option_union_record_variant() -> TestResult {
+ let schema = Schema::parse_str(
+ r#"{
+ "type": "record",
+ "name": "TestRecord",
+ "fields": [{
+ "name": "innerUnion", "type": [
+ "null",
+ {"type": "record", "name": "innerRecordFoo", "fields": [
+ {"name": "foo", "type": "string"}
+ ]},
+ {"type": "record", "name": "innerRecordBar", "fields": [
+ {"name": "bar", "type": "string"}
+ ]},
+ {"name": "intField", "type": "int"},
+ {"name": "stringField", "type": "string"}
+ ]
+ }]
+ }"#,
+ )?;
+
+ #[derive(Serialize)]
+ #[serde(rename_all = "camelCase")]
+ struct TestRecord {
+ inner_union: Option<InnerUnion>,
+ }
+
+ #[derive(Serialize)]
+ #[serde(untagged)]
+ enum InnerUnion {
+ InnerVariantFoo(InnerRecordFoo),
+ InnerVariantBar(InnerRecordBar),
+ IntField(i32),
+ StringField(String),
+ }
+
+ #[derive(Serialize)]
+ #[serde(rename = "innerRecordFoo")]
+ struct InnerRecordFoo {
+ foo: String,
+ }
+
+ #[derive(Serialize)]
+ #[serde(rename = "innerRecordBar")]
+ struct InnerRecordBar {
+ bar: String,
+ }
+
+ assert!(!crate::util::is_human_readable());
Review Comment:
Similar to above, removed
--
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]