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

Change is_decodable/1 to decodable?/1 #138

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions lib/avrora/codec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ defmodule Avrora.Codec do

## Examples

...> Avrora.Codec.Plain.is_decodable(<<1, 2, 3>>)
...> Avrora.Codec.Plain.decodable?(<<1, 2, 3>>)
true
...> Avrora.Codec.SchemaRegistry.is_decodable(<<1, 2, 3>>)
...> Avrora.Codec.SchemaRegistry.decodable?(<<1, 2, 3>>)
false
"""
@callback decodable?(payload :: binary()) :: boolean

@doc """
Checks whether a given binary Avro message is decodable with the Plain codec.

This function is deprecated and will be removed in future release.
Please use `Avrora.Codec.Plain.decodable?/1` instead.
"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you delete the documentation @doc false and use @deprecated "Use ... instead"

@callback is_decodable(payload :: binary()) :: boolean

@doc """
Expand Down
12 changes: 12 additions & 0 deletions lib/avrora/codec/object_container_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ defmodule Avrora.Codec.ObjectContainerFile do
alias Avrora.Resolver
alias Avrora.Schema.Encoder, as: SchemaEncoder

@impl true
def decodable?(payload) when is_binary(payload) do
case payload do
<<@magic_bytes, _::binary>> -> true
_ -> false
end
end

@impl true
def is_decodable(payload) when is_binary(payload) do
Logger.warning(
"`Avrora.Codec.ObjectContainerFile.is_decodable/1` is deprecated, use `Avrora.Codec.ObjectContainerFile.decodable?/1` instead"
)

case payload do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please replace the code with a call of decodable? instead of duplication

<<@magic_bytes, _::binary>> -> true
_ -> false
Expand Down
10 changes: 9 additions & 1 deletion lib/avrora/codec/plain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ defmodule Avrora.Codec.Plain do

@behaviour Avrora.Codec

require Logger
alias Avrora.AvroDecoderOptions
alias Avrora.Resolver

@impl true
def is_decodable(payload) when is_binary(payload), do: true
def decodable?(payload) when is_binary(payload), do: true

@impl true
def is_decodable(payload) when is_binary(payload) do
Logger.warning("`Avrora.Codec.Plain.is_decodable/1` is deprecated, use `Avrora.Codec.Plain.decodable?/1` instead")

true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please re-use new function instead?

end

@impl true
def extract_schema(_payload), do: {:error, :schema_not_found}
Expand Down
12 changes: 12 additions & 0 deletions lib/avrora/codec/schema_registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ defmodule Avrora.Codec.SchemaRegistry do
alias Avrora.Resolver
alias Avrora.Schema

@impl true
def decodable?(payload) when is_binary(payload) do
case payload do
<<@magic_bytes, _::binary>> -> true
_ -> false
end
end

@impl true
def is_decodable(payload) when is_binary(payload) do
Logger.warning(
"`Avrora.Codec.SchemaRegistry.is_decodable/1` is deprecated, use `Avrora.Codec.SchemaRegistry.decodable?/1` instead"
)

case payload do
<<@magic_bytes, _::binary>> -> true
_ -> false
Expand Down
6 changes: 3 additions & 3 deletions lib/avrora/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Avrora.Encoder do
def extract_schema(payload) when is_binary(payload) do
codec =
[Codec.SchemaRegistry, Codec.ObjectContainerFile, Codec.Plain]
|> Enum.find(& &1.is_decodable(payload))
|> Enum.find(& &1.decodable?(payload))

codec.extract_schema(payload)
end
Expand All @@ -46,7 +46,7 @@ defmodule Avrora.Encoder do
def decode(payload) when is_binary(payload) do
codec =
[Codec.SchemaRegistry, Codec.ObjectContainerFile, Codec.Plain]
|> Enum.find(& &1.is_decodable(payload))
|> Enum.find(& &1.decodable?(payload))

codec.decode(payload)
end
Expand All @@ -73,7 +73,7 @@ defmodule Avrora.Encoder do

codec =
[Codec.SchemaRegistry, Codec.ObjectContainerFile, Codec.Plain]
|> Enum.find(& &1.is_decodable(payload))
|> Enum.find(& &1.decodable?(payload))

if codec == Codec.Plain do
Logger.warning(
Expand Down
6 changes: 3 additions & 3 deletions test/avrora/codec/object_container_file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ defmodule Avrora.Codec.ObjectContainerFileTest do
setup :verify_on_exit!
setup :support_config

describe "is_decodable/1" do
describe "decodable?/1" do
test "when payload is a valid binary" do
assert Codec.ObjectContainerFile.is_decodable(payment_message())
assert Codec.ObjectContainerFile.decodable?(payment_message())
end

test "when payload is not a valid binary" do
refute Codec.ObjectContainerFile.is_decodable(<<0, 1, 2>>)
refute Codec.ObjectContainerFile.decodable?(<<0, 1, 2>>)
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/avrora/codec/plain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ defmodule Avrora.Codec.PlainTest do
setup :verify_on_exit!
setup :support_config

describe "is_decodable/1" do
describe "decodable?/1" do
test "when payload is a valid binary" do
assert Codec.Plain.is_decodable(payment_message())
assert Codec.Plain.decodable?(payment_message())
end

test "when payload is not a valid binary" do
assert Codec.Plain.is_decodable(<<0, 1, 2>>)
assert Codec.Plain.decodable?(<<0, 1, 2>>)
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/avrora/codec/schema_registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ defmodule Avrora.Codec.SchemaRegistryTest do
setup :verify_on_exit!
setup :support_config

describe "is_decodable/1" do
describe "decodable?/1" do
test "when payload is a valid binary" do
assert Codec.SchemaRegistry.is_decodable(payment_message())
assert Codec.SchemaRegistry.decodable?(payment_message())
end

test "when payload is not a valid binary" do
assert Codec.SchemaRegistry.is_decodable(<<0, 1, 2>>)
assert Codec.SchemaRegistry.decodable?(<<0, 1, 2>>)
end
end

Expand Down
Loading