这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
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
14 changes: 10 additions & 4 deletions lib/maybe.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Maybe do
@moduledoc """
Access nested maps and structs, protected from `nil`.
Access nested maps and structs, protected from `nil`.

See `maybe/1` or `maybe/2` for more details.

Expand All @@ -16,7 +16,7 @@ defmodule Maybe do
"""

@doc """
Get a value out of a nested map or struct, or return nil.
Get a value out of a nested map or struct, or return nil.

Compiles down to `maybe/2`. In other words, this:

Expand All @@ -35,6 +35,10 @@ defmodule Maybe do
iex> map = %{city: nil}
...> maybe(map.city.name)
nil

iex> map = %{city: :unknown}
...> maybe(map.city.name)
nil
"""
defmacro maybe(ast) do
[variable | keys] = extract_keys(ast)
Expand All @@ -45,7 +49,7 @@ defmodule Maybe do
end

@doc """
Get a value out of a nested map or struct, or return nil.
Get a value out of a nested map or struct, or return nil.

For prettier syntax, see the `maybe/1` macro.

Expand All @@ -63,10 +67,12 @@ defmodule Maybe do
def maybe(nil, _keys), do: nil
def maybe(val, []), do: val

def maybe(map, [h | t]) do
def maybe(map, [h | t]) when is_map(map) do
maybe(Map.get(map, h), t)
end

def maybe(_, _), do: nil

defp extract_keys(ast, keys \\ [])
defp extract_keys([], keys), do: keys

Expand Down