martin-g edited a comment on pull request #1368:
URL: https://github.com/apache/avro/pull/1368#issuecomment-943329047
Here is a test case that properly constructs a POJO:
```rust
#[test]
fn test_from_value_with_union() -> TestResult<()> {
#[derive(Debug, Deserialize, PartialEq)]
struct StructWithMapInner {
map: Option<HashMap<String, i64>>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct StructWithMap {
inner: StructWithMapInner,
}
let raw_map: HashMap<String, i64> = [
("one".to_string(), 1i64),
("three".to_string(), 3i64),
("two".to_string(), 2i64)
].iter().cloned().collect();
let map = raw_map.iter()
.map(|(k, v)| (k.clone(), Value::Long(*v)))
.collect();
let value = Value::Record(vec![(
"inner".to_owned(),
Value::Record(vec![
("map".to_string(), Value::Union(Box::new(Value::Map(map)))),
// ("map".to_string(), Value::Map(map)),
]),
)]);
let result: StructWithMap = crate::from_value(&value)?;
assert_eq!(result, StructWithMap{ inner: StructWithMapInner{ map:
Some(raw_map) }});
Ok(())
}
```
The key is that you need to use an `Option` for the `Union`.
Everything works without your changes.
Just put this test at the bottom of `de.rs` and run `cargo test
test_from_value_with_union`
--
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]