avro icon indicating copy to clipboard operation
avro copied to clipboard

Wrong decoding of nested map

Open samber opened this issue 2 years ago • 1 comments

I would like to encode a map having multiple values (primitive, date or empty map):

map[string]any{
	"a": "abcd",
	"b": 42,
	"c": false,
	"d": myDate,
	"e": map[string]any{},   //  <- always empty and not nil
	"f": nil,
},

I use the following schema:

...
{
    "name": "my_map",
    "type": {
        "type": "map",
        "values": [
            "string",
            "int",
            "long",
            "float",
            "double",
            "boolean",
            "null",
            {
                "type": "long",
                "logicalType": "timestamp-millis"
            },
            {
                "type": "map",
                "values": "null"  //  <- can be any type since the map will always be {}
            }
        ]
    }
}
...

After go->avro->go conversion, I get the following map:

{
  "a": {
    "string": "abcd"
  },
  "b": {
    "int": 42
  },
  "c": {
    "boolean": false
  },
  "d": {
    "long.timestamp-millis": "2024-02-06T10:38:03.033Z"
  },
  "e": null,
  "f": null
}

Does anybody know why the library inserts the type as key ?

When I remove the nested map in schema, the map is decoded properly, except for e being nil, instead of empty map.

samber avatar Feb 06 '24 10:02 samber

Your type is a map of union, and as stated in the README, and unions in the generic form decode to a map with the type as the only key. The any union type does not apply in this case because you are doing a generic decode. That is my assumption at any rate, as there is not enough here to actually tell, but it is what it looks like.

nrwiersma avatar Feb 06 '24 18:02 nrwiersma

Assuming the question has been answered. If there still an issue, please reopen.

nrwiersma avatar Jun 25 '24 12:06 nrwiersma