Check date value before calling to_iso8601 (#7769)

* 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.
This commit is contained in:
garte 2018-03-19 08:15:38 +01:00 committed by William Cheng
parent 9d85c82cdd
commit f9b2839a30

View File

@ -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