Skip to content

Commit

Permalink
TxtBlock: fix handling of improper lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ProducerMatt committed Feb 27, 2024
1 parent 5fda1cd commit 89f51dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/stampede.ex
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,19 @@ defmodule Stampede do
def reload_service(cfg) do
Service.apply_service_function(cfg, :reload_configs, [])
end

@spec! foldr_improper(
maybe_improper_list(),
any(),
(elem :: any(), acc :: any() -> acc :: any())
) :: any()
def foldr_improper(ls, acc, f), do: do_foldr_improper(ls, acc, f)
defp do_foldr_improper([], acc, _f), do: acc
defp do_foldr_improper([h | []], acc, f), do: f.(h, acc)

defp do_foldr_improper([h | t], acc, f),
do: f.(h, do_foldr_improper(t, acc, f))

defp do_foldr_improper(other, acc, f) when not is_list(other),
do: f.(other, acc)
end
10 changes: 10 additions & 0 deletions lib/stampede/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ defmodule Stampede.Response do
struct!(
unquote(__MODULE__),
Keyword.put_new(unquote(keys), :origin_plug, __MODULE__)
|> Keyword.update!(:text, fn
str when is_binary(str) ->
str

nil ->
nil

iodata when is_list(iodata) ->
iodata |> IO.iodata_to_binary()
end)
)
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/txt_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ defmodule TxtBlock do
end

def to_iolist(blueprint, service_name) when is_list(blueprint) do
List.foldr(blueprint, [], fn
S.foldr_improper(blueprint, [], fn
[], acc ->
acc
|> IO.inspect(pretty: true)

item, acc ->
to_iolist(item, service_name)
Expand All @@ -58,7 +57,6 @@ defmodule TxtBlock do
other ->
[other | acc]
end
|> IO.inspect(pretty: true)
end)
|> case do
[] ->
Expand Down

0 comments on commit 89f51dc

Please sign in to comment.