defmodule CoreUtils.StructUtil do @exceptions [NaiveDateTime, DateTime] @bloat [ :__meta__, :__struct__, :__cardinality__, :__field__, :__owner__, Ecto.Association.NotLoaded ] def nested_parse(list) when is_list(list) do Enum.map(list, &nested_parse/1) end def nested_parse(%{__struct__: Core.Models.Task} = struct) do struct = if struct.geom do {lon, lat} = struct.geom.coordinates %{struct | lon: lon, lat: lat} else struct end nested_parse(Map.from_struct(struct)) end def nested_parse(schema) when is_map(schema) do schema |> Map.take(Map.keys(schema) -- @bloat) |> Enum.filter(fn {_, %Ecto.Association.NotLoaded{}} -> false _ -> true end) |> Enum.map(&nested_parse/1) |> Enum.into(%{}) end def nested_parse({key, %{__struct__: struct} = val}) when struct in @exceptions, do: {key, val} def nested_parse({key, val}) when is_map(val) or is_list(val) do {key, nested_parse(val)} end def nested_parse(data), do: data end