-
-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion(Seq): Map to Vector of structs #565
Comments
The #[derive(serde::Serialize, serde::Deserialize)]
struct Custom<K, V> {
custom_key: K,
v: V,
}
impl<K, KAs, V, VAs> SerializeAs<(K, V)> for Custom<KAs, VAs>
where
KAs: SerializeAs<K>,
VAs: SerializeAs<V>,
{
fn serialize_as<S>((k, v): &(K, V), serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
(Custom {
custom_key: SerializeAsWrap::<K, KAs>::new(k),
v: SerializeAsWrap::<V, VAs>::new(v),
})
.serialize(serializer)
}
}
impl<'de, K, KAs, V, VAs> DeserializeAs<'de, (K, V)> for Custom<KAs, VAs>
where
KAs: DeserializeAs<'de, K>,
VAs: DeserializeAs<'de, V>,
{
fn deserialize_as<D>(deserializer: D) -> Result<(K, V), D::Error>
where
D: serde::Deserializer<'de>,
{
let c = <Custom<DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>>>::deserialize(
deserializer,
)?;
Ok((c.custom_key.into_inner(), c.v.into_inner()))
}
}
#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SM(#[serde_as(as = "Seq<Custom<_, _>>")] BTreeMap<u32, IpAddr>);
let map: BTreeMap<_, _> = vec![(1, ip), (10, ip), (200, ip2)].into_iter().collect();
is_equal(
SM(map),
expect![[r#"
[
{
"custom_key": 1,
"v": "1.2.3.4"
},
{
"custom_key": 10,
"v": "1.2.3.4"
},
{
"custom_key": 200,
"v": "255.255.255.255"
}
]"#]],
); Is that a good enough solution? |
Sorry this doesn't seem works, I got error:
Looks like --- code --- #[derive(Clone, Debug, Serialize, Deserialize)]
/// helper struct to deserialize a custom named struct into K-V pairs in Maps
struct KvProxy<KAs, VAs> {
/// proxy typed key
key: KAs,
/// proxy typed value
value: VAs,
}
impl<K, V, KAs, VAs> SerializeAs<(K, V)> for KvProxy<KAs, VAs>
where
KAs: SerializeAs<K>,
VAs: SerializeAs<V>,
{
fn serialize_as<S>((k, v): &(K, V), serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let proxy = KvProxy {
key: SerializeAsWrap::<K, KAs>::new(k),
value: SerializeAsWrap::<V, VAs>::new(v),
};
proxy.serialize(serializer)
}
}
impl<'de, K, V, KAs, VAs> DeserializeAs<'de, (K, V)> for KvProxy<KAs, VAs>
where
KAs: DeserializeAs<'de, K>,
VAs: DeserializeAs<'de, V>,
{
fn deserialize_as<D>(deserializer: D) -> Result<(K, V), D::Error>
where
D: Deserializer<'de>,
{
let proxy = KvProxy::<DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>>::deserialize(
deserializer,
)?;
Ok((proxy.key.into_inner(), proxy.value.into_inner()))
}
}
//...
struct DataFile {
// other fields
#[serde(default)]
#[serde_as(as = "DefaultOnNull<Seq<KvProxy<_,_>>>")]
column_sizes: BTreeMap<i32, i64>,
} |
This still requires changes to
|
Sorry, my mistake. |
566: Generalize the trait bounds of `Seq` r=jonasbb a=jonasbb Generalize the trait bounds of `Seq` to allow for more flexible. Instead of forcing the inner type to be a tuple, take anything convertible to/from a tuple. This can be expressed using the Closes #565 Co-authored-by: Jonas Bushart <[email protected]>
This doesn't actually work. You get this error message for this code snippet. Unfortunately, it wasn't caught with the tests. #[serde_as(as = "Seq<(_, Vec<_>)>")]
BTreeMap<usize, Vec<usize>>,
The problem is that during serialization you never get the key or value directly, you only get references to them. That is ok for the Ultimately, this probably means it cannot be implemented properly. As mentioned before, |
573: Add regression test for problems with #565/#566 r=jonasbb a=jonasbb #572 #565 (comment) bors r+ Co-authored-by: Jonas Bushart <[email protected]>
For structs like:
And implemented
From<(K,V)>
andInto<(K,V)>
, allow them to be used inner type ofSeq
.Expected behaviour
Can be serialized into (or deserialized from):
Motivation
I've been trying to implement reading from
Iceberg
'sAVRO
files, and usingapache-avro
Serde API to readIceberg
encoded Maps will produce an array of small, key-value with naming tuples.The text was updated successfully, but these errors were encountered: