Skip to content

Commit

Permalink
[CAPCS-163]: Create separation between internal application domain ev…
Browse files Browse the repository at this point in the history
…ents and published events (#21)

* Fix decimal type to_avro_map

* Add missing clauses for decimal, date, and datetime
  • Loading branch information
Johnabell authored and cottinisimone committed Sep 5, 2024
1 parent 49be50f commit 0251462
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/avrogen/code_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1044,19 +1044,28 @@ defmodule Avrogen.CodeGenerator do
"Decimal.new(#{inner})"
]

def from_cond_conversion_incantation("decimal", inner),
do: from_cond_conversion_incantation("big_decimal", inner)

def from_cond_conversion_incantation("iso_date", inner),
do: [
"is_binary(#{inner}) and ( Date.from_iso8601(#{inner}) |> elem(0) == :ok)",
"Date.from_iso8601(#{inner}) |> elem(1)"
]

def from_cond_conversion_incantation("date", inner),
do: from_cond_conversion_incantation("iso_date", inner)

# TODO: what about the offset??
def from_cond_conversion_incantation("iso_datetime", inner),
do: [
"is_binary(#{inner}) and ( DateTime.from_iso8601(#{inner} |> elem(0) == :ok)",
"is_binary(#{inner}) and ( DateTime.from_iso8601(#{inner}) |> elem(0) == :ok)",
"DateTime.from_iso8601(#{inner}) |> elem(1)"
]

def from_cond_conversion_incantation("datetime", inner),
do: from_cond_conversion_incantation("iso_datetime", inner)

def conversion_from_incantation("big_decimal", inner), do: ~s'Decimal.new(#{inner})'
def conversion_from_incantation("decimal", inner), do: ~s'Decimal.new(#{inner})'
def conversion_from_incantation("iso_date", inner), do: ~s'Date.from_iso8601!(#{inner})'
Expand Down Expand Up @@ -1210,6 +1219,7 @@ defmodule Avrogen.CodeGenerator do
def match_conversion_incantation("iso_date"), do: ~s'%Date{} = d -> Date.to_iso8601(d)'
def match_conversion_incantation("date"), do: ~s'%Date{} = d -> Date.to_iso8601(d)'
def match_conversion_incantation("big_decimal"), do: ~s'%Decimal{} = d -> Decimal.to_string(d)'
def match_conversion_incantation("decimal"), do: ~s'%Decimal{} = d -> Decimal.to_string(d)'

def match_conversion_incantation("iso_datetime"),
do: ~s'%DateTime{} = d -> DateTime.to_iso8601(d)'
Expand Down
40 changes: 40 additions & 0 deletions test/code_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ defmodule Avrogen.CodeGenerator.Test do
assert ~s'"premium_breakdown" => Enum.into(r.premium_breakdown, %{}, fn {k, v} -> {k, Decimal.to_string(v)} end)' ==
CodeGenerator.to_avro_map_field(map_type, %{})
end

test "decimal types" do
decimal_type = %{
"name" => "price",
"type" => ["null", %{"type" => "string", "logicalType" => "decimal"}]
}

assert ~s'"price" => case r.price do\n %Decimal{} = d -> Decimal.to_string(d)\n_ -> r.price\nend' ==
CodeGenerator.to_avro_map_field(decimal_type, %{})
end
end

describe "from_avro_map_body_field" do
Expand All @@ -396,6 +406,36 @@ defmodule Avrogen.CodeGenerator.Test do
assert ~s'premium_breakdown: Enum.into(premium_breakdown, %{}, fn {k, v} -> {k, Decimal.new(v)} end)' ==
CodeGenerator.from_avro_map_body_field(map_type, %{})
end

test "decimal types" do
decimal_type = %{
"name" => "price",
"type" => ["null", %{"type" => "string", "logicalType" => "decimal"}]
}

assert ~s'price: if not is_nil(price) and (Decimal.parse(price) != :error) do\n Decimal.new(price)\nelse\n price\nend' ==
CodeGenerator.from_avro_map_body_field(decimal_type, %{})
end

test "date types" do
decimal_type = %{
"name" => "price",
"type" => ["null", %{"type" => "string", "logicalType" => "date"}]
}

assert ~s'price: if is_binary(price) and ( Date.from_iso8601(price) |> elem(0) == :ok) do\n Date.from_iso8601(price) |> elem(1)\nelse\n price\nend' ==
CodeGenerator.from_avro_map_body_field(decimal_type, %{})
end

test "datetime types" do
decimal_type = %{
"name" => "price",
"type" => ["null", %{"type" => "string", "logicalType" => "datetime"}]
}

assert ~s'price: if is_binary(price) and ( DateTime.from_iso8601(price) |> elem(0) == :ok) do\n DateTime.from_iso8601(price) |> elem(1)\nelse\n price\nend' ==
CodeGenerator.from_avro_map_body_field(decimal_type, %{})
end
end

describe "random_instance_field" do
Expand Down

0 comments on commit 0251462

Please sign in to comment.