beam-community / avro_ex

An Avro Library that emphasizes testability and ease of use.
https://hexdocs.pm/avro_ex/AvroEx.html
67 stars 27 forks source link

Doesn't support empty map #3

Closed martinmaillard closed 6 years ago

martinmaillard commented 6 years ago

UPDATE: I just realized this issue was recently fixed for arrays. It's still happening for maps but I might be able to fix it.

Hi,

I'm having an issue when trying to decode a previously encoded empty array or map.

Using the following schema:

{
    "type": "record",
    "name": "Minimal",
    "fields": [{
        "name": "my_list",
        "type": {"type": "array", "items": "string"}
    }]
}

When I try to encode an emtpy list, and then decode it again, the following happens:

iex> {:ok, encoded} = AvroEx.encode(schema, %{"my_list" => []})
{:ok, <<0>>}
iex> {:ok, decoded} = AvroEx.decode(schema, encoded)
** (FunctionClauseError) no function clause matching in AvroEx.Decode.variable_integer_decode/3

    The following arguments were given to AvroEx.Decode.variable_integer_decode/3:

        # 1
        ""

        # 2
        ""

        # 3
        %AvroEx.Schema.Primitive{metadata: %{}, type: :long}

    Attempted function clauses (showing 3 out of 3):

        def variable_integer_decode(<<0::integer()-size(1), n::integer()-size(7), rest::bitstring()>>, acc, %AvroEx.Schema.Primitive{type: :integer}) when is_bitstring(acc) and is_bitstring(acc)
        def variable_integer_decode(<<0::integer()-size(1), n::integer()-size(7), rest::bitstring()>>, acc, %AvroEx.Schema.Primitive{type: :long}) when is_bitstring(acc) and is_bitstring(acc)
        def variable_integer_decode(<<1::integer()-size(1), n::integer()-size(7), rest::bitstring()>>, acc, type) when is_bitstring(acc) and is_bitstring(acc)

    (avro_ex) lib/avro_ex/decode.ex:180: AvroEx.Decode.variable_integer_decode/3
    (avro_ex) lib/avro_ex/decode.ex:77: AvroEx.Decode.do_decode/3
    (avro_ex) lib/avro_ex/decode.ex:92: AvroEx.Decode.do_decode/3
    (avro_ex) lib/avro_ex/decode.ex:101: AvroEx.Decode.do_decode/3
    (avro_ex) lib/avro_ex/decode.ex:144: anonymous fn/4 in AvroEx.Decode.do_decode/3
    (elixir) lib/enum.ex:2949: Enum.reduce_range_dec/4
    (avro_ex) lib/avro_ex/decode.ex:143: AvroEx.Decode.do_decode/3
    (avro_ex) lib/avro_ex/decode.ex:112: anonymous fn/3 in AvroEx.Decode.do_decode/3

If I switch the array for a map in the schema and try again with an empty map, the same behavior occurs.