Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Simplify Logic #12

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading