Jefffrey commented on code in PR #11638:
URL: https://github.com/apache/arrow-rs/pull/11638#discussion_r4301780408


##########
arrow-schema/src/fields.rs:
##########
@@ -1032,4 +1046,36 @@ mod tests {
         ];
         UnionFields::from_iter(fields);
     }
+
+    #[cfg(feature = "serde")]
+    #[test]
+    fn test_union_fields_serde_round_trip() {
+        let fields = UnionFields::try_new(
+            vec![1, 3],
+            vec![
+                Field::new("a", DataType::Int32, true),
+                Field::new("b", DataType::Utf8, true),
+            ],
+        )
+        .unwrap();
+        let serialized = postcard::to_stdvec(&fields).unwrap();
+        let deserialized: UnionFields = 
postcard::from_bytes(&serialized).unwrap();
+        assert_eq!(fields, deserialized);
+    }
+
+    #[cfg(feature = "serde")]
+    #[test]
+    fn test_union_fields_deserialize_should_validate() {
+        let field = Arc::new(Field::new("a", DataType::Int32, true));
+
+        let duplicate: Vec<(i8, FieldRef)> = vec![(1, Arc::clone(&field)), (1, 
Arc::clone(&field))];
+        let serialized = postcard::to_stdvec(&duplicate).unwrap();
+        let err = 
postcard::from_bytes::<UnionFields>(&serialized).unwrap_err();
+        assert!(matches!(err, postcard::Error::SerdeDeCustom), "{err:?}");
+
+        let negative: Vec<(i8, FieldRef)> = vec![(0, Arc::clone(&field)), (-1, 
field)];
+        let serialized = postcard::to_stdvec(&negative).unwrap();
+        let err = 
postcard::from_bytes::<UnionFields>(&serialized).unwrap_err();
+        assert!(matches!(err, postcard::Error::SerdeDeCustom), "{err:?}");

Review Comment:
   is it possible to assert some of the error message too?



##########
arrow-schema/src/fields.rs:
##########
@@ -316,10 +316,24 @@ impl<'a> IntoIterator for &'a Fields {
 /// - All type ids are unique
 /// - All type ids are non-negative
 #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
+#[cfg_attr(feature = "serde", derive(serde::Serialize))]
 #[cfg_attr(feature = "serde", serde(transparent))]
 pub struct UnionFields(Arc<[(i8, FieldRef)]>);
 
+/// Deserializes the same representation as `Serialize`, but goes through
+/// [`UnionFields::try_new`] so the invariants above are enforced.

Review Comment:
   ```suggestion
   // Manual implementation to enforce invariants per docstring above
   ```



-- 
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]

Reply via email to