Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop unnecessary configuration #105

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ defmodule MyClient do
registry_user_agent: "Avrora/0.25.0 Elixir",
schemas_path: "./priv/schemas",
registry_schemas_autoreg: false,
convert_null_values: false,
convert_map_to_proplist: false,
names_cache_ttl: :timer.minutes(5),
decoder_hook: &MyClient.decoder_hook/4
end
Expand All @@ -110,8 +108,6 @@ config :avrora,
registry_user_agent: "Avrora/0.24.2 Elixir", # optional: if you want to return previous behaviour, set it to `nil`
schemas_path: "./priv/schemas",
registry_schemas_autoreg: false, # optional: if you want manually register schemas
convert_null_values: false, # optional: if you want to keep decoded `:null` values as is
convert_map_to_proplist: false, # optional: if you want to restore the old behavior for decoding map-type
names_cache_ttl: :timer.minutes(5), # optional: if you want periodic disk reads
decoder_hook: &MyClient.decoder_hook/4 # optional: if you want to amend the data/result
```
Expand All @@ -122,8 +118,6 @@ config :avrora,
- `registry_user_agent`<sup>[v0.25]</sup> - HTTP `User-Agent` header for Schema Registry requests, default `Avrora/<version> Elixir`
- `schemas_path` - Base path for locally stored schema files, default `./priv/schemas`
- `registry_schemas_autoreg`<sup>[v0.13]</sup> - Flag for automatic schemas registration in the Schema Registry, default `true`
- `convert_null_values`<sup>[v0.14]</sup> - Flag for automatic conversion of decoded `:null` values into `nil`, default `true`
- `convert_map_to_proplist`<sup>[v0.15]</sup> restore old behaviour and confiugre decoding map-type to proplist, default `false`
- `names_cache_ttl`<sup>[v0.10]</sup> - Time in ms to cache schemas by name in memory, default `:infinity`
- `decoder_hook`<sup>[v0.24]</sup> - Function with arity 4 to amend data or result, default `fn _, _, data, fun -> fun.(data) end`

Expand Down
1 change: 0 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ config :avrora,
registry_auth: nil,
registry_user_agent: nil,
registry_schemas_autoreg: true,
convert_null_values: true,
names_cache_ttl: :infinity

config :logger, :console, format: "$time $metadata[$level] $levelpad$message\n"
20 changes: 8 additions & 12 deletions lib/avrora/avro_decoder_options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,32 @@ defmodule Avrora.AvroDecoderOptions do

alias Avrora.Config

@options %{
encoding: :avro_binary,
hook: &__MODULE__.__hook__/4,
is_wrapped: true,
map_type: :map,
record_type: :map
}
@null_type_name "null"

@doc """
A unified erlavro decoder options compatible for both binary and OCF decoders.
"""
def options do
if convert_map_to_proplist(), do: %{@options | map_type: :proplist}, else: @options
%{
encoding: :avro_binary,
hook: &__MODULE__.__hook__/4,
is_wrapped: true,
map_type: :map,
record_type: :map
}
end

# NOTE: This is internal module function and should never be used directly
@doc false
def __hook__(type, sub_name_or_idx, data, decode_fun) do
convert = convert_null_values()
decoder_hook = decoder_hook()

result = decoder_hook.(type, sub_name_or_idx, data, decode_fun)

if convert == true && :avro.get_type_name(type) == @null_type_name,
if :avro.get_type_name(type) == @null_type_name,
do: {nil, data},
else: result
end

defp convert_null_values, do: Config.self().convert_null_values()
defp convert_map_to_proplist, do: Config.self().convert_map_to_proplist()
defp decoder_hook, do: Config.self().decoder_hook()
end
2 changes: 0 additions & 2 deletions lib/avrora/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ defmodule Avrora.Client do
def registry_auth, do: get(@opts, :registry_auth, nil)
def registry_user_agent, do: get(@opts, :registry_user_agent, "Avrora/#{version()} Elixir")
def registry_schemas_autoreg, do: get(@opts, :registry_schemas_autoreg, true)
def convert_null_values, do: get(@opts, :convert_null_values, true)
def convert_map_to_proplist, do: get(@opts, :convert_map_to_proplist, false)
def names_cache_ttl, do: get(@opts, :names_cache_ttl, :infinity)
def decoder_hook, do: get(@opts, :decoder_hook, fn _, _, data, fun -> fun.(data) end)
def file_storage, do: unquote(:"Elixir.#{module}.Storage.File")
Expand Down
10 changes: 0 additions & 10 deletions lib/avrora/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ defmodule Avrora.Config do
* `registry_auth` authentication settings for Schema Registry, default `nil`
* `registry_user_agent` HTTP `User-Agent` header for Schema Registry requests, default `Avrora/<version> Elixir`
* `registry_schemas_autoreg` automatically register schemas in Schema Registry, default `true`
* `convert_null_values` convert `:null` values in the decoded message into `nil`, default `true`
* `convert_map_to_proplist` bring back old behavior and configure decoding AVRO map-type as proplist, default `false`
* `names_cache_ttl` duration to cache global schema names millisecods, default `:infinity`
* `decoder_hook` function to amend decoded payload, default `fn _, _, data, fun -> fun.(data) end`

Expand All @@ -29,8 +27,6 @@ defmodule Avrora.Config do
@callback registry_auth :: tuple() | nil
@callback registry_user_agent :: String.t() | nil
@callback registry_schemas_autoreg :: boolean()
@callback convert_null_values :: boolean()
@callback convert_map_to_proplist :: boolean()
@callback names_cache_ttl :: integer() | atom()
@callback decoder_hook :: (any(), any(), any(), any() -> any())
@callback file_storage :: module()
Expand Down Expand Up @@ -59,12 +55,6 @@ defmodule Avrora.Config do
@doc false
def registry_schemas_autoreg, do: get_env(:registry_schemas_autoreg, true)

@doc false
def convert_null_values, do: get_env(:convert_null_values, true)

@doc false
def convert_map_to_proplist, do: get_env(:convert_map_to_proplist, false)

@doc false
def names_cache_ttl, do: get_env(:names_cache_ttl, :infinity)

Expand Down
12 changes: 1 addition & 11 deletions test/avrora/codec/object_container_file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ defmodule Avrora.Codec.ObjectContainerFileTest do
end

test "when payload is a valid binary and null values must be as is" do
stub(Avrora.ConfigMock, :convert_null_values, fn -> false end)

{:ok, decoded} = Codec.ObjectContainerFile.decode(null_value_message(), schema: null_value_schema())

assert decoded == [%{"key" => "user-1", "value" => :null}]
assert decoded == [%{"key" => "user-1", "value" => nil}]
end

test "when payload is a valid binary and null values must be converted" do
Expand All @@ -90,14 +88,6 @@ defmodule Avrora.Codec.ObjectContainerFileTest do
assert decoded == [%{"key" => "user-1", "value" => nil}]
end

test "when payload is a valid binary and map type must be decoded as proplist" do
stub(Avrora.ConfigMock, :convert_map_to_proplist, fn -> true end)

{:ok, decoded} = Codec.ObjectContainerFile.decode(map_message())

assert decoded == [%{"map_field" => [{"key", "value"}]}]
end

test "when payload is a valid binary and map type must be decoded as map" do
{:ok, decoded} = Codec.ObjectContainerFile.decode(map_message())

Expand Down
12 changes: 1 addition & 11 deletions test/avrora/codec/plain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ defmodule Avrora.Codec.PlainTest do
end

test "when payload is a valid binary and null values must be as is" do
stub(Avrora.ConfigMock, :convert_null_values, fn -> false end)

{:ok, decoded} = Codec.Plain.decode(null_value_message(), schema: null_value_schema())

assert decoded == %{"key" => "user-1", "value" => :null}
assert decoded == %{"key" => "user-1", "value" => nil}
end

test "when payload is a valid binary and null values must be converted" do
Expand All @@ -91,14 +89,6 @@ defmodule Avrora.Codec.PlainTest do
assert decoded == %{"key" => "user-1", "value" => nil}
end

test "when payload is a valid binary and map type must be decoded as proplist" do
stub(Avrora.ConfigMock, :convert_map_to_proplist, fn -> true end)

{:ok, decoded} = Codec.Plain.decode(map_message(), schema: map_schema())

assert decoded == %{"map_field" => [{"key", "value"}]}
end

test "when payload is a valid binary and map type must be decoded as map" do
{:ok, decoded} = Codec.Plain.decode(map_message(), schema: map_schema())

Expand Down
4 changes: 0 additions & 4 deletions test/support/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ defmodule Support.Config do
@impl true
def registry_schemas_autoreg, do: true
@impl true
def convert_null_values, do: true
@impl true
def names_cache_ttl, do: :infinity
@impl true
def decoder_hook, do: fn _, _, data, fun -> fun.(data) end
@impl true
def convert_map_to_proplist, do: false
@impl true
def file_storage, do: Avrora.Storage.FileMock
@impl true
def memory_storage, do: Avrora.Storage.MemoryMock
Expand Down