Skip to content

Commit

Permalink
Fix: Simplify Logic (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleVsteger authored May 28, 2024
1 parent bcbd9ee commit 2f35548
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions lib/channel_spec/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,21 @@ defmodule Serializer do
# Helper function to ensure the order of the output json
@spec to_ordered_structs(map()) :: Jason.OrderedObject.t()
defp to_ordered_structs(map) when is_map(map) do
reverse_ordered =
map
|> Enum.to_list()
|> Enum.sort_by(fn {key, _val} -> key end, :asc)
|> Enum.reduce(%Jason.OrderedObject{}, fn
{key, value}, acc when is_map(value) ->
%{acc | values: [{key, to_ordered_structs(value)} | acc.values]}
map
|> Enum.to_list()
|> Enum.sort_by(fn {key, _val} -> key end, :desc)
|> Enum.reduce(%Jason.OrderedObject{}, fn
{key, value}, acc when is_map(value) ->
%{acc | values: [{key, to_ordered_structs(value)} | acc.values]}

{key, value}, acc when is_list(value) ->
%{acc | values: [{key, to_ordered_structs(value)} | acc.values]}
{key, value}, acc when is_list(value) ->
%{acc | values: [{key, to_ordered_structs(value)} | acc.values]}

{key, value}, acc ->
%{acc | values: [{key, value} | acc.values]}
end)

%{reverse_ordered | values: Enum.reverse(reverse_ordered.values)}
end

defp to_ordered_structs(list) when is_list(list) do
Enum.map(list, &to_ordered_structs/1)
{key, value}, acc ->
%{acc | values: [{key, value} | acc.values]}
end)
end

defp to_ordered_structs(stuff), do: stuff
defp to_ordered_structs(list) when is_list(list), do: Enum.map(list, &to_ordered_structs/1)
defp to_ordered_structs(other), do: other
end

0 comments on commit 2f35548

Please sign in to comment.