klarna / erlavro

Avro support for Erlang/Elixir (http://avro.apache.org/)
Apache License 2.0
131 stars 39 forks source link

OCF decoder hook #103

Closed Strech closed 4 years ago

Strech commented 4 years ago

Hey guys, I want to say thanks for an amazing library πŸ‘πŸΌ

Issue

I've been using it to provide an Elixir library Avrora and I'm missing a small piece of functionality – a decoder hook for OCF similar to the binary decoder hook.

Motivation

The need comes from handing null values of the primitive fields with a value of NULL. I would like to transform them into nil values of Elixir. I can do it with a binary decoder via hook, but I can't with OCF.

Proposal

I'm not sure about the interface (should it be the props list or just a hook) for OCF, but Idea is that the test might look like this

decoder_hook_test() ->
  InteropOcfFile = test_data("interop.ocf"),
  Hook = fun(Type, __SubNameOrId__, Data, DecodeFun) ->
    case avro:get_type_name(Type) of
      null -> {<<"modifiedNullValue">>, Data}
      _ -> DecodeFun(Data)
    end
  end,
  {_, _, [Object]} = avro_ocf:decode_file(InteropOcfFile, Hook),
  ?assertEqual(<<"modifiedNullValue">>,
               proplists:get_value(<<"nullField">>, Object)).

WDYT?

zmstone commented 4 years ago

Hi. The avro_binary_decoder:decode_stream/4 API can be updated to support a proplist for the last argument (in a backward compatible way), see: https://github.com/klarna/erlavro/blob/master/src/avro_binary_decoder.erl#L82-L84 And then expose this proplist as the 2nd arg fro avro_ocf:decode_file/2. With such API, the hook can be passed in as [{hook, Fun}].

Strech commented 4 years ago

@zmstone Exactly πŸ‘ŒπŸΌ I will try to attach PR to this issue (but I'm sorry for my Erlang skills in advance)