From aec0c4597ab790816e2689aebc44b8128a959d9e Mon Sep 17 00:00:00 2001 From: Mayel de Borniol Date: Wed, 8 Jan 2025 17:21:14 +0000 Subject: [PATCH] https://github.com/bonfire-networks/bonfire-app/issues/900 --- lib/enums.ex | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/enums.ex b/lib/enums.ex index 1b75572..d303c51 100644 --- a/lib/enums.ex +++ b/lib/enums.ex @@ -820,6 +820,14 @@ defmodule Bonfire.Common.Enums do def nested_structs_to_maps(v), do: v + def merge_to_struct(module \\ nil, first, precedence) + + def merge_to_struct(module, first, precedence) when not is_struct(first), + do: merge_to_struct(nil, struct(module, first), precedence) + + def merge_to_struct(_, first, precedence) when is_struct(first), + do: struct(first, struct_to_map(precedence)) + def maybe_merge_to_struct(first, precedence) when is_struct(first), do: struct(first, struct_to_map(precedence)) @@ -867,8 +875,13 @@ defmodule Bonfire.Common.Enums do @doc "Recursively filters nil values from a map" def map_filter_empty(data) when is_map(data) and not is_struct(data) do - Enum.map(data, &map_filter_empty/1) - |> Enum.reject(fn {_, v} -> is_nil(v) end) + data + # |> Enum.map(&map_filter_empty/1) + |> Enum.reject(fn + {_, nil} -> true + {_, %Ecto.Association.NotLoaded{}} -> true + _ -> false + end) |> Map.new() end