Implement safer format parsing
This commit is contained in:
@@ -34,7 +34,7 @@ defmodule MusicLibrary.Records.SearchParser do
|
||||
Ecto.Enum.dump_values(MusicLibrary.Records.Record, :format)
|
||||
|> Enum.map(&string/1)
|
||||
|> choice()
|
||||
|> map({String, :to_existing_atom, []})
|
||||
|> map({__MODULE__, :resolve_format, []})
|
||||
|
||||
format = concat(format_filter, formats) |> tag(:format)
|
||||
|
||||
@@ -66,6 +66,11 @@ defmodule MusicLibrary.Records.SearchParser do
|
||||
{:ok, normalize(result)}
|
||||
end
|
||||
|
||||
def resolve_format(format) do
|
||||
Ecto.Enum.mappings(MusicLibrary.Records.Record, :format)
|
||||
|> Enum.find_value(fn {key, value} -> if value == format, do: key end)
|
||||
end
|
||||
|
||||
defp normalize(result) do
|
||||
Enum.reduce(result, %{}, fn
|
||||
{:artist, [{:query, [value]}]}, acc ->
|
||||
|
||||
Reference in New Issue
Block a user