Skip to content

feat: support :time decode #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ecto/adapters/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ defmodule Ecto.Adapters.SQLite3 do
[&Codec.naive_datetime_decode/1, type]
end

@impl Ecto.Adapter
def loaders(:time, type) do
[&Codec.time_decode/1, type]
end

@impl Ecto.Adapter
def loaders(:utc_datetime_usec, type) do
[&Codec.utc_datetime_decode/1, type]
Expand Down
9 changes: 9 additions & 0 deletions lib/ecto/adapters/sqlite3/codec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ defmodule Ecto.Adapters.SQLite3.Codec do
end
end

def time_decode(nil), do: {:ok, nil}

def time_decode(value) do
case Time.from_iso8601(value) do
{:ok, _time} = result -> result
{:error, _} -> :error
end
end

def json_encode(value) do
Application.get_env(:ecto_sqlite3, :json_library, Jason).encode(value)
end
Expand Down
26 changes: 26 additions & 0 deletions test/ecto/adapters/sqlite3/codec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ defmodule Ecto.Adapters.SQLite3.CodecTest do
end
end

describe ".time_decode/1" do
test "nil" do
{:ok, nil} = Codec.time_decode(nil)
end

test "string" do
{:ok, time} = Time.from_iso8601("23:50:07")
assert {:ok, ^time} = Codec.time_decode("23:50:07")

{:ok, time} = Time.from_iso8601("23:50:07Z")
assert {:ok, ^time} = Codec.time_decode("23:50:07Z")

{:ok, time} = Time.from_iso8601("T23:50:07Z")
assert {:ok, ^time} = Codec.time_decode("T23:50:07Z")

{:ok, time} = Time.from_iso8601("23:50:07,0123456")
assert {:ok, ^time} = Codec.time_decode("23:50:07,0123456")

{:ok, time} = Time.from_iso8601("23:50:07.0123456")
assert {:ok, ^time} = Codec.time_decode("23:50:07.0123456")

{:ok, time} = Time.from_iso8601("23:50:07.123Z")
assert {:ok, ^time} = Codec.time_decode("23:50:07.123Z")
end
end

describe ".utc_datetime_decode/1" do
test "nil" do
assert {:ok, nil} = Codec.utc_datetime_decode(nil)
Expand Down