Generate parse_type/parse_format from module attributes

This commit is contained in:
Claudio Ortolina
2026-04-14 16:56:04 +01:00
parent 2bb755b1c8
commit d75508531e
+11 -3
View File
@@ -322,7 +322,7 @@ defmodule MusicLibrary.Records.Record do
Calendar.strftime(dt, "%d/%m/%Y") Calendar.strftime(dt, "%d/%m/%Y")
end end
@spec parse_matching_record(map()) :: map() @spec parse_matching_record(map()) :: map() | no_return()
def parse_matching_record(%{ def parse_matching_record(%{
"id" => id, "id" => id,
"title" => title, "title" => title,
@@ -334,13 +334,21 @@ defmodule MusicLibrary.Records.Record do
%{ %{
id: id, id: id,
title: title, title: title,
format: String.to_existing_atom(format), format: parse_format(format),
type: String.to_existing_atom(type), type: parse_type(type),
purchased_at: parse_datetime(purchased_at), purchased_at: parse_datetime(purchased_at),
cover_hash: cover_hash cover_hash: cover_hash
} }
end end
for type <- @types do
defp parse_type(unquote(Atom.to_string(type))), do: unquote(type)
end
for format <- @formats do
defp parse_format(unquote(Atom.to_string(format))), do: unquote(format)
end
defp parse_datetime(nil), do: nil defp parse_datetime(nil), do: nil
defp parse_datetime(dt_string) do defp parse_datetime(dt_string) do