-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
[PROTOTYPE] Add unnamed types support #115
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,15 +38,19 @@ defmodule Avrora.Storage.File do | |
""" | ||
@impl true | ||
def get(key) when is_binary(key) do | ||
with {:ok, body} <- read_schema_file_by_name(key), | ||
do: SchemaEncoder.from_json(body, &read_schema_file_by_name/1) | ||
with {:ok, schema_name} <- Name.parse(key), | ||
{:ok, body} <- read_schema_file_by_name(key) do | ||
SchemaEncoder.from_json(body, name: schema_name.name, reference_lookup_fun: &read_schema_file_by_name/1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a refactoring here, nested calls generate too much lookup table. Considering unnamed types, we need to overhaul how we store lookup tables and how we work with them. |
||
end | ||
end | ||
|
||
@impl true | ||
def get(key) when is_integer(key), do: {:error, :unsupported} | ||
|
||
@impl true | ||
def put(_key, _value), do: {:error, :unsupported} | ||
|
||
# TODO: Move `Name.parse` outside of the method | ||
defp read_schema_file_by_name(name) do | ||
with {:ok, schema_name} <- Name.parse(name), | ||
filepath <- name_to_filepath(schema_name.name) do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,6 +259,42 @@ defmodule Avrora.Codec.PlainTest do | |
|
||
assert encoded == "59B02128" | ||
end | ||
|
||
test "when payload is matching the Union schema and schema is resolvable" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Add decoding test! |
||
union_schema = union_schema() | ||
|
||
Avrora.Storage.MemoryMock | ||
|> expect(:get, fn key -> | ||
assert key == "io.acme.Union" | ||
|
||
{:ok, nil} | ||
end) | ||
|> expect(:put, fn key, value -> | ||
assert key == "io.acme.Union" | ||
assert value == union_schema | ||
|
||
{:ok, value} | ||
end) | ||
|
||
Avrora.Storage.RegistryMock | ||
|> expect(:put, fn key, value -> | ||
assert key == "io.acme.Union" | ||
assert value == union_json() | ||
|
||
{:error, :unconfigured_registry_url} | ||
end) | ||
|
||
Avrora.Storage.FileMock | ||
|> expect(:get, fn key -> | ||
assert key == "io.acme.Union" | ||
|
||
{:ok, union_schema} | ||
end) | ||
|
||
{:ok, encoded} = Codec.Plain.encode(123, schema: %Schema{full_name: "io.acme.Union"}) | ||
|
||
assert encoded == <<0, 246, 1>> | ||
end | ||
end | ||
|
||
defp missing_field_error do | ||
|
@@ -306,6 +342,11 @@ defmodule Avrora.Codec.PlainTest do | |
%{schema | id: nil, version: nil} | ||
end | ||
|
||
defp union_schema do | ||
{:ok, schema} = Schema.Encoder.from_json(union_json(), name: "io.acme.Union") | ||
%{schema | id: nil, version: nil} | ||
end | ||
|
||
defp payment_json do | ||
~s({"namespace":"io.acme","name":"Payment","type":"record","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]}) | ||
end | ||
|
@@ -329,4 +370,6 @@ defmodule Avrora.Codec.PlainTest do | |
defp fixed_json do | ||
~s({"namespace":"io.acme","name":"CRC32","type":"fixed","size":8}) | ||
end | ||
|
||
defp union_json, do: ~s(["int","string"]) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not much value