Skip to content

Commit

Permalink
fix: remove more String.to_atom calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Timmer authored and maennchen committed Nov 14, 2022
1 parent b0e6bfb commit 8fc13c5
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/crontab/cron_expression/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ defmodule Crontab.CronExpression.Parser do
error_message = "Can't parse #{value} as month."

result =
case {Map.fetch(@month_values, String.to_atom(String.upcase(value))),
Integer.parse(value, 10)} do
case {fetch_month_value(String.upcase(value)), Integer.parse(value, 10)} do
# No valid month string or integer
{:error, :error} -> {:error, error_message}
# Month specified as string
Expand Down Expand Up @@ -356,8 +355,7 @@ defmodule Crontab.CronExpression.Parser do
defp parse_week_day(value) do
error_message = "Can't parse #{value} as day of week."

case {Map.fetch(@weekday_values, String.to_atom(String.upcase(value))),
Integer.parse(value, 10)} do
case {fetch_weekday_value(String.upcase(value)), Integer.parse(value, 10)} do
{:error, :error} -> {:error, error_message}
{{:ok, number}, :error} -> {:ok, number}
{:error, {number, ""}} -> {:ok, number}
Expand Down Expand Up @@ -400,6 +398,24 @@ defmodule Crontab.CronExpression.Parser do
end
end

@spec fetch_weekday_value(binary) :: {:ok, integer} | :error
Enum.map(@weekday_values, fn {weekday, weekday_number} ->
defp fetch_weekday_value(unquote(to_string(weekday))) do
{:ok, unquote(weekday_number)}
end
end)

defp fetch_weekday_value(_), do: :error

@spec fetch_month_value(binary) :: {:ok, integer} | :error
Enum.map(@month_values, fn {month, month_number} ->
defp fetch_month_value(unquote(to_string(month))) do
{:ok, unquote(month_number)}
end
end)

defp fetch_month_value(_), do: :error

@spec special(binary) :: result
Enum.map(@specials, fn {special, special_value} ->
defp special(unquote(to_string(special))) do
Expand Down

0 comments on commit 8fc13c5

Please sign in to comment.