Created
March 31, 2016 23:32
-
-
Save moxley/ef318feec9652e8ce9eef1150dafbb49 to your computer and use it in GitHub Desktop.
Strip an Ecto record of attributes that cannot be encoded to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
record = Enum.reduce(Map.keys(record), %{}, fn (key, map) -> | |
value = Map.get(record, key) | |
key_to_add = cond do | |
is_map(value) && Map.get(value, :__struct__, nil) == Elixir.Ecto.Association.NotLoaded -> | |
nil | |
key == :__meta__ -> | |
nil | |
key == :__struct__ -> | |
nil | |
true -> | |
key | |
end | |
if key_to_add do | |
map |> Map.merge(%{key_to_add => value}) | |
else | |
map | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment