Skip to content

Commit

Permalink
Merge pull request #89 from zenlist/issue-87
Browse files Browse the repository at this point in the history
Support deserializing structs from lists
  • Loading branch information
bryanburgers authored Nov 13, 2023
2 parents 5a529db + 18f2082 commit e934f2f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/de/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,11 @@ impl<'de> de::Deserializer<'de> for Deserializer {
where
V: Visitor<'de>,
{
self.deserialize_map(visitor)
if let AttributeValue::L(_) = self.input {
self.deserialize_seq(visitor)
} else {
self.deserialize_map(visitor)
}
}

fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand Down
91 changes: 91 additions & 0 deletions src/de/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,94 @@ fn issue_27() {

assert_identical_json!(Subject, attribute_value.clone());
}

mod issue_87 {
use super::*;

#[test]
fn deserialize_struct_from_list() {
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
struct Subject {
first_value: String,
second_value: String,
}

let attribute_value = AttributeValue::L(vec![
AttributeValue::S(String::from("Value1")),
AttributeValue::S(String::from("Value2")),
]);

let s: Subject = from_attribute_value(attribute_value.clone()).unwrap();
assert_eq!(
s,
Subject {
first_value: String::from("Value1"),
second_value: String::from("Value2"),
}
);
assert_identical_json!(Subject, attribute_value.clone());
}

#[test]
fn deserialize_enum_newtype_from_list() {
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(tag = "0", content = "1")]
enum Subject {
Newtype(u8),
}

let attribute_value = AttributeValue::L(vec![
AttributeValue::S(String::from("Newtype")),
AttributeValue::N(String::from("1")),
]);

let s: Subject = from_attribute_value(attribute_value.clone()).unwrap();
assert_eq!(s, Subject::Newtype(1));

assert_identical_json!(Subject, attribute_value.clone())
}

#[test]
fn deserialize_enum_tuple_from_list() {
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(tag = "0", content = "1")]
enum Subject {
Tuple(u8, u8),
}

let attribute_value = AttributeValue::L(vec![
AttributeValue::S(String::from("Tuple")),
AttributeValue::L(vec![
AttributeValue::N(String::from("1")),
AttributeValue::N(String::from("2")),
]),
]);

let s: Subject = from_attribute_value(attribute_value.clone()).unwrap();
assert_eq!(s, Subject::Tuple(1, 2));

assert_identical_json!(Subject, attribute_value.clone())
}

#[test]
fn deserialize_enum_struct_variant_from_list() {
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(tag = "0", content = "1")]
enum Subject {
Structy { one: u8, two: u8 },
}

let attribute_value = AttributeValue::L(vec![
AttributeValue::S(String::from("Structy")),
AttributeValue::M(HashMap::from([
(String::from("one"), AttributeValue::N(String::from("1"))),
(String::from("two"), AttributeValue::N(String::from("2"))),
])),
]);

let s: Subject = from_attribute_value(attribute_value.clone()).unwrap();
assert_eq!(s, Subject::Structy { one: 1, two: 2 });

assert_identical_json!(Subject, attribute_value.clone())
}
}

0 comments on commit e934f2f

Please sign in to comment.