Skip to content
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

Handle DateTime fields through Repo #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/dlex/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ defmodule Dlex.Node do
float: "float",
string: "string",
geo: "geo",
datetime: "datetime",
utc_datetime: "datetime",
uid: "[uid]"
]

Expand Down
9 changes: 9 additions & 0 deletions lib/dlex/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ defmodule Dlex.Repo do
end
end

def encode(%DateTime{} = data), do: data
def encode(%{__struct__: struct} = data) do
data
|> Map.from_struct()
Expand All @@ -148,6 +149,7 @@ defmodule Dlex.Repo do
def encode(data) when is_list(data), do: encode_list(data, [])
def encode(data), do: data


defp encode_kv([], map, _struct), do: map
defp encode_kv([{_key, nil} | kv], map, struct), do: encode_kv(kv, map, struct)

Expand Down Expand Up @@ -227,6 +229,8 @@ defmodule Dlex.Repo do
with %{} = map <- do_decode(map, lookup, strict?), do: {:ok, map}
end



defp do_decode(map, lookup, strict?) when is_map(map) and is_map(lookup) do
with %{"dgraph.type" => [type_string]} <- map,
type when type != nil <- Map.get(lookup, type_string) do
Expand Down Expand Up @@ -264,6 +268,11 @@ defmodule Dlex.Repo do
end)
end

defp do_decode_field(struct, {field_name, :utc_datetime}, value, lookup, strict?) when not is_struct(value) do
{:ok, new_value} = Calendar.DateTime.Parse.rfc3339_utc(value)
do_decode_field(struct, {field_name, :utc_datetime}, new_value, lookup, strict?)
end

defp do_decode_field(struct, {field_name, field_type}, value, lookup, strict?) do
case Ecto.Type.load(field_type, value) do
{:ok, loaded_value} ->
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Dlex.MixProject do
[
{:db_connection, "~> 2.1"},
{:grpc, "~> 0.3.1"},
{:calendar, "~> 1.0.0"},
{:jason, "~> 1.0", optional: true},
{:mint, "~> 1.0", optional: true},
{:castore, "~> 0.1.4", optional: true},
Expand Down
6 changes: 4 additions & 2 deletions test/dlex/node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Dlex.NodeTest do
assert "user" == User.__schema__(:source)
assert :string == User.__schema__(:type, :name)
assert :integer == User.__schema__(:type, :age)
assert [:name, :age, :friends, :location] == User.__schema__(:fields)
assert [:name, :age, :friends, :location, :modified] == User.__schema__(:fields)
end

test "alter" do
Expand All @@ -22,11 +22,13 @@ defmodule Dlex.NodeTest do
},
%{"predicate" => "user.age", "type" => "int"},
%{"predicate" => "user.friends", "type" => "[uid]"},
%{"predicate" => "user.location", "type" => "geo"}
%{"predicate" => "user.location", "type" => "geo"},
%{"predicate" => "user.modified", "type" => "datetime"}
],
"types" => [
%{
"fields" => [
%{"name" => "user.modified", "type" => "datetime"},
%{"name" => "user.location", "type" => "geo"},
%{"name" => "user.friends", "type" => "[uid]"},
%{"name" => "user.age", "type" => "int"},
Expand Down
8 changes: 8 additions & 0 deletions test/dlex/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ defmodule Dlex.RepoTest do
assert {:ok, %{uid: _uid}} = TestRepo.set(valid_changeset)
end

test "setting datetime field in Changeset" do
now = DateTime.utc_now()
changeset = Ecto.Changeset.cast(%User{}, %{name: "TimeTraveler", age: 20, modified: now}, [:name, :age, :modified])
assert {:ok, %{uid: uid}} = TestRepo.set(changeset)

assert {:ok, %User{name: "TimeTraveler", age: 20, modified: now}} = TestRepo.get(uid)
end

test "using custom types" do
changes = %{name: "John", age: 30, location: %{lat: 15.5, lon: 10.2}}
changeset = Changeset.cast(%User{}, changes, [:name, :age, :location])
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule Dlex.User do
field :age, :integer
field :friends, :uid
field :location, Dlex.Geo
field :modified, :utc_datetime
field :cache, :any, virtual: true
end
end
Expand Down