[ 
https://issues.apache.org/jira/browse/AVRO-3177?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Martin Tzvetanov Grigorov resolved AVRO-3177.
---------------------------------------------
    Resolution: Not A Problem

Hi,

 

I've just noticed this ticket while checking the backlog.

 

I've managed to accomplish what you need (I hope!) with:
{code:java}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum TestEnum {
    Long(i64),
    Double(f64),
    String(String),
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct TestName123 {
    pub test_field_name: HashMap<String, TestEnum>
}


let schema = r#"
{
    "type" : "record",
    "name" : "TestName123",
    "namespace" : "Microsoft.ServiceBus.Messaging",
    "fields" : [ {
        "name": "test_field_name",
        "type" : { "type": "map", "values": [ "long", "double", "string", 
"bytes" ] }
    }]
}
"#;

let mut map = HashMap::new();
map.insert(String::from("long"), TestEnum::Long(123_i64));
map.insert(String::from("double"), TestEnum::Double(234_f64));
map.insert(String::from("str"), TestEnum::String("string_a".to_owned()));
let tt = TestName123 {
    test_field_name: map
};

let value = crate::ser::to_value(&tt).unwrap();
// dbg!(value);

let res = crate::de::from_value::<TestName123>(&value);
dbg!(res.unwrap()); {code}
 

Please use the mailing lists for such kind of questions ([email protected]).

> Deserializing a HashMap with different possible value types
> -----------------------------------------------------------
>
>                 Key: AVRO-3177
>                 URL: https://issues.apache.org/jira/browse/AVRO-3177
>             Project: Apache Avro
>          Issue Type: Wish
>          Components: rust
>            Reporter: Theodore
>            Priority: Major
>
> Hi!
> I'm trying to convert a record that conforms to this schema:
> {code:java}
> const SCHEMA: &str = r#"
>  {
>  "type" : "record",
>  "name" : "TestName123",
>  "namespace" : "Microsoft.ServiceBus.Messaging",
>  "fields" : [ {
>  "name": "test_field_name",
>  "type" : { "type": "map", "values": [ "long", "double", "string", "bytes" ] }
>  }]
>  }
>  "#; {code}
> to a Rust type defined like this:
> {code:java}
> pub struct TestName123 {
>   pub test_field_name: HashMap<String, ???>
> }
> {code}
>  using this code:
> {code:java}
> // `record` is an Avro `Value` type, specifically a `Value::Record`.
> avro_rs::from_value::<TestName123>(record)
> {code}
> I can't seem to figure out the type for `test_field_name` map values. I just 
> wanted to confirm that doing this is at least possible, and if it is if 
> anyone would have any general pointers on the type.
> I can't find any similar examples in the documentation. I have tried things 
> like this:
>  
> {code:java}
> #[derive(Clone, Debug, Deserialize)]
> pub enum MapValue {
>   MapValueLong(u64),
>   MapValueDouble(f64),
>   MapValueString(String),
>   MapValueBytes(Bytes)
> }
> {code}
>  But `from_value` fails with an error message `"unsupported union"`.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to