Permalink
Browse files

Merge pull request #3 from alexpeachey/add-optional-ecto-dep

Allow optional support for Ecto
  • Loading branch information...
mrdougwright committed Oct 2, 2018
2 parents 6909f60 + 5db6b43 commit 7aa7cfb353a4b427da8fd788cf4fd4e73c3e1c78
Showing with 16 additions and 2 deletions.
  1. +5 −2 lib/unpack.ex
  2. +1 −0 mix.exs
  3. +3 −0 mix.lock
  4. +7 −0 test/unpack_test.exs
@@ -15,8 +15,11 @@ defmodule Unpack do
iex> Unpack.get(struct, [:player, :game, :id])
nil
"""
def get(data, [key | tail]) when is_map(data), do:
get(Map.get(data, key), tail)
if Code.ensure_loaded?(Ecto) do
def get(%Ecto.Association.NotLoaded{}, _), do: nil
end
def get(data, [key | tail]) when is_map(data), do: get(Map.get(data, key), tail)
def get(data, []) when is_map(data) do
case map_size(data) do
@@ -31,6 +31,7 @@ defmodule Unpack.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ecto, "~> 2.2", optional: true},
{:ex_doc, "~> 0.18.0", only: :dev, runtime: false}
]
end
@@ -1,4 +1,7 @@
%{
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "2.2.10", "e7366dc82f48f8dd78fcbf3ab50985ceeb11cb3dc93435147c6e13f2cda0992e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.18.4", "4406b8891cecf1352f49975c6d554e62e4341ceb41b9338949077b0d4a97b949", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
}
@@ -46,5 +46,12 @@ defmodule UnpackTest do
struct = %{game: %Game{developer: %Developer{name: "Nascar"}}}
assert Unpack.get(struct, [:game, :developer, :name]) == "Nascar"
end
if Code.ensure_loaded?(Ecto) do
test "returns nil if value is an unloaded Ecto relation" do
struct = %{game: %Game{developer: %Ecto.Association.NotLoaded{}}}
assert Unpack.get(struct, [:game, :developer]) == nil
end
end
end
end

0 comments on commit 7aa7cfb

Please sign in to comment.