Skip to content

Commit

Permalink
Check date value before calling to_iso8601 (#7769)
Browse files Browse the repository at this point in the history
* Check date value before calling to_iso8601

When deserializing a date value the value has to be a string when
calling to_iso8601. Otherwise it fails with a match error due to a
is_binary() guard.

* Fix: to_iso returns tuple with three values.
  • Loading branch information
garte authored and wing328 committed Mar 19, 2018
1 parent 9d85c82 commit f9b2839
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ defmodule {{moduleName}}.Deserializer do
|> Map.update!(field, &(Map.new(&1, fn {key, val} -> {key, Poison.Decode.decode(val, Keyword.merge(options, [as: struct(mod)]))} end)))
end
def deserialize(model, field, :date, _, _options) do
case DateTime.from_iso8601(Map.get(model, field)) do
{:ok, datetime} ->
Map.put(model, field, datetime)
_ ->
model
value = Map.get(model, field)
case is_binary(value) do
true -> case DateTime.from_iso8601(value) do
{:ok, datetime, _offset} ->
Map.put(model, field, datetime)
_ ->
model
end
false -> model
end
end
end

0 comments on commit f9b2839

Please sign in to comment.