expanding regex sigils to use modifiers
This commit is contained in:
parent
511d93fa54
commit
a1f2dfb10a
3 changed files with 46 additions and 5 deletions
|
@ -90,6 +90,8 @@ defp do_convert(entity) when is_list(entity) do
|
||||||
for v <- entity, into: [], do: do_convert(v)
|
for v <- entity, into: [], do: do_convert(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_convert(%Regex{} = entity), do: inspect(entity)
|
||||||
|
|
||||||
defp do_convert(entity) when is_map(entity) do
|
defp do_convert(entity) when is_map(entity) do
|
||||||
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
|
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
|
||||||
end
|
end
|
||||||
|
@ -122,7 +124,7 @@ def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity
|
||||||
|
|
||||||
def transform(entity), do: :erlang.term_to_binary(entity)
|
def transform(entity), do: :erlang.term_to_binary(entity)
|
||||||
|
|
||||||
defp do_transform(%Regex{} = entity) when is_map(entity), do: entity
|
defp do_transform(%Regex{} = entity), do: entity
|
||||||
|
|
||||||
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
|
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
|
||||||
{dispatch_settings, []} = do_eval(entity)
|
{dispatch_settings, []} = do_eval(entity)
|
||||||
|
@ -154,8 +156,15 @@ defp do_transform(entity) when is_binary(entity) do
|
||||||
defp do_transform(entity), do: entity
|
defp do_transform(entity), do: entity
|
||||||
|
|
||||||
defp do_transform_string("~r/" <> pattern) do
|
defp do_transform_string("~r/" <> pattern) do
|
||||||
pattern = String.trim_trailing(pattern, "/")
|
modificator = String.split(pattern, "/") |> List.last()
|
||||||
~r/#{pattern}/
|
pattern = String.trim_trailing(pattern, "/" <> modificator)
|
||||||
|
|
||||||
|
case modificator do
|
||||||
|
"" -> ~r/#{pattern}/
|
||||||
|
"i" -> ~r/#{pattern}/i
|
||||||
|
"u" -> ~r/#{pattern}/u
|
||||||
|
"s" -> ~r/#{pattern}/s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_transform_string(":" <> atom), do: String.to_atom(atom)
|
defp do_transform_string(":" <> atom), do: String.to_atom(atom)
|
||||||
|
|
|
@ -1779,7 +1779,11 @@ test "common config example", %{conn: conn} do
|
||||||
%{"tuple" => [":seconds_valid", 60]},
|
%{"tuple" => [":seconds_valid", 60]},
|
||||||
%{"tuple" => [":path", ""]},
|
%{"tuple" => [":path", ""]},
|
||||||
%{"tuple" => [":key1", nil]},
|
%{"tuple" => [":key1", nil]},
|
||||||
%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}
|
%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
|
||||||
|
%{"tuple" => [":regex1", "~r/https:\/\/example.com/"]},
|
||||||
|
%{"tuple" => [":regex2", "~r/https:\/\/example.com/u"]},
|
||||||
|
%{"tuple" => [":regex3", "~r/https:\/\/example.com/i"]},
|
||||||
|
%{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -1796,7 +1800,11 @@ test "common config example", %{conn: conn} do
|
||||||
%{"tuple" => [":seconds_valid", 60]},
|
%{"tuple" => [":seconds_valid", 60]},
|
||||||
%{"tuple" => [":path", ""]},
|
%{"tuple" => [":path", ""]},
|
||||||
%{"tuple" => [":key1", nil]},
|
%{"tuple" => [":key1", nil]},
|
||||||
%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}
|
%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
|
||||||
|
%{"tuple" => [":regex1", "~r/https:\\/\\/example.com/"]},
|
||||||
|
%{"tuple" => [":regex2", "~r/https:\\/\\/example.com/u"]},
|
||||||
|
%{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]},
|
||||||
|
%{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -103,6 +103,30 @@ test "sigil" do
|
||||||
assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
|
assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "link sigil" do
|
||||||
|
binary = Config.transform("~r/https:\/\/example.com/")
|
||||||
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
|
||||||
|
assert Config.from_binary(binary) == ~r/https:\/\/example.com/
|
||||||
|
end
|
||||||
|
|
||||||
|
test "link sigil with u modifier" do
|
||||||
|
binary = Config.transform("~r/https:\/\/example.com/u")
|
||||||
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/u)
|
||||||
|
assert Config.from_binary(binary) == ~r/https:\/\/example.com/u
|
||||||
|
end
|
||||||
|
|
||||||
|
test "link sigil with i modifier" do
|
||||||
|
binary = Config.transform("~r/https:\/\/example.com/i")
|
||||||
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
|
||||||
|
assert Config.from_binary(binary) == ~r/https:\/\/example.com/i
|
||||||
|
end
|
||||||
|
|
||||||
|
test "link sigil with s modifier" do
|
||||||
|
binary = Config.transform("~r/https:\/\/example.com/s")
|
||||||
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
|
||||||
|
assert Config.from_binary(binary) == ~r/https:\/\/example.com/s
|
||||||
|
end
|
||||||
|
|
||||||
test "2 child tuple" do
|
test "2 child tuple" do
|
||||||
binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
|
binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
|
||||||
assert binary == :erlang.term_to_binary({"v1", :v2})
|
assert binary == :erlang.term_to_binary({"v1", :v2})
|
||||||
|
|
Loading…
Reference in a new issue