Skip to content

Commit ec0e7d8

Browse files
author
José Valim
committed
Remove get_in/1
1 parent c2e912b commit ec0e7d8

File tree

2 files changed

+35
-88
lines changed

2 files changed

+35
-88
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,68 +1715,6 @@ defmodule Kernel do
17151715
Access.get_and_update(data, h, &get_and_update_in(&1, t, fun))
17161716
end
17171717

1718-
@doc """
1719-
Gets a value from a nested structure via the given `path`.
1720-
1721-
This is similar to `get_in/2`, except the path is extracted via
1722-
a macro rather than passing a list. For example:
1723-
1724-
get_in(opts[:foo][:bar])
1725-
1726-
Is equivalent to:
1727-
1728-
get_in(opts, [:foo, :bar])
1729-
1730-
Note that in order for this macro to work, the complete path must always
1731-
be visible by this macro.
1732-
1733-
## Examples
1734-
1735-
iex> users = %{"josé" => %{age: 27}, "eric" => %{age: 23}}
1736-
iex> get_in(users["josé"][:age])
1737-
27
1738-
1739-
iex> users = %{"josé" => %{age: 27}, "eric" => %{age: 23}}
1740-
iex> get_in(users["josé"].age)
1741-
27
1742-
1743-
## Paths
1744-
1745-
A path may start with a variable, local or remote call, and must be
1746-
followed by one or more:
1747-
1748-
* `foo[bar]` - access a field. In case an intermediate field is not
1749-
present or returns nil, an empty map is used;
1750-
1751-
* `foo.bar` - access a map/struct field. In case the field is not
1752-
present, an error is raised;
1753-
1754-
Here are some valid paths:
1755-
1756-
users["josé"][:age]
1757-
users["josé"].age
1758-
User.all["josé"].age
1759-
all_users()["josé"].age
1760-
1761-
Here are some invalid ones:
1762-
1763-
# Does a remote call after the initial value
1764-
users["josé"].do_something(arg1, arg2)
1765-
1766-
# Does not access any field
1767-
users
1768-
1769-
"""
1770-
defmacro get_in(path) do
1771-
[h|t] = unnest(path, [], "get_in/2")
1772-
Enum.reduce(t, h, fn
1773-
{:access, key}, acc ->
1774-
quote do: Access.get(unquote(acc), unquote(key))
1775-
{:map, key}, acc ->
1776-
quote do: Access.Map.get!(unquote(acc), unquote(key))
1777-
end)
1778-
end
1779-
17801718
@doc """
17811719
Puts a value in a nested structure via the given `path`.
17821720
@@ -1791,7 +1729,7 @@ defmodule Kernel do
17911729
17921730
Note that in order for this macro to work, the complete path must always
17931731
be visible by this macro. For more information about the supported path
1794-
expressions, please check `get_in/1` docs.
1732+
expressions, please check `get_and_update_in/2` docs.
17951733
17961734
## Examples
17971735
@@ -1824,7 +1762,7 @@ defmodule Kernel do
18241762
18251763
Note that in order for this macro to work, the complete path must always
18261764
be visible by this macro. For more information about the supported path
1827-
expressions, please check `get_in/1` docs.
1765+
expressions, please check `get_and_update_in/2` docs.
18281766
18291767
## Examples
18301768
@@ -1856,15 +1794,40 @@ defmodule Kernel do
18561794
get_and_update_in(opts, [:foo, :bar], &{&1, &1 + 1})
18571795
18581796
Note that in order for this macro to work, the complete path must always
1859-
be visible by this macro. For more information about the supported path
1860-
expressions, please check `get_in/1` docs.
1797+
be visible by this macro. See the Paths section below.
18611798
18621799
## Examples
18631800
18641801
iex> users = %{"josé" => %{age: 27}, "eric" => %{age: 23}}
18651802
iex> get_and_update_in(users["josé"][:age], &{&1, &1 + 1})
18661803
{27, %{"josé" => %{age: 28}, "eric" => %{age: 23}}}
18671804
1805+
## Paths
1806+
1807+
A path may start with a variable, local or remote call, and must be
1808+
followed by one or more:
1809+
1810+
* `foo[bar]` - access a field. In case an intermediate field is not
1811+
present or returns nil, an empty map is used;
1812+
1813+
* `foo.bar` - access a map/struct field. In case the field is not
1814+
present, an error is raised;
1815+
1816+
Here are some valid paths:
1817+
1818+
users["josé"][:age]
1819+
users["josé"].age
1820+
User.all["josé"].age
1821+
all_users()["josé"].age
1822+
1823+
Here are some invalid ones:
1824+
1825+
# Does a remote call after the initial value
1826+
users["josé"].do_something(arg1, arg2)
1827+
1828+
# Does not access any field
1829+
users
1830+
18681831
"""
18691832
defmacro get_and_update_in(path, fun) do
18701833
[h|t] = unnest(path, [], "get_and_update_in/2")

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,6 @@ defmodule KernelTest do
196196
end
197197
end
198198

199-
test "get_in/1" do
200-
users = %{"josé" => %{age: 27}, "eric" => %{age: 23}}
201-
assert get_in(users["josé"][:age]) == 27
202-
assert get_in(users["dave"][:age]) == nil
203-
204-
assert get_in(users["josé"].age) == 27
205-
206-
assert_raise ArgumentError, fn ->
207-
get_in(users["dave"].age)
208-
end
209-
210-
assert_raise KeyError, fn ->
211-
get_in(users["eric"].unknown)
212-
end
213-
end
214-
215199
test "put_in/3" do
216200
users = %{"josé" => %{age: 27}, "eric" => %{age: 23}}
217201

@@ -330,17 +314,17 @@ defmodule KernelTest do
330314
test "paths" do
331315
map = empty_map()
332316

333-
assert get_in(map[:foo]) == nil
334-
assert get_in(empty_map()[:foo]) == nil
335-
assert get_in(KernelTest.empty_map()[:foo]) == nil
336-
assert get_in(__MODULE__.empty_map()[:foo]) == nil
317+
assert put_in(map[:foo], "bar") == %{foo: "bar"}
318+
assert put_in(empty_map()[:foo], "bar") == %{foo: "bar"}
319+
assert put_in(KernelTest.empty_map()[:foo], "bar") == %{foo: "bar"}
320+
assert put_in(__MODULE__.empty_map()[:foo], "bar") == %{foo: "bar"}
337321

338322
assert_raise ArgumentError, ~r"access at least one field,", fn ->
339-
Code.eval_quoted(quote(do: get_in(map)), [])
323+
Code.eval_quoted(quote(do: put_in(map, "bar")), [])
340324
end
341325

342326
assert_raise ArgumentError, ~r"must start with a variable, local or remote call", fn ->
343-
Code.eval_quoted(quote(do: get_in(map.foo(1, 2)[:bar])), [])
327+
Code.eval_quoted(quote(do: put_in(map.foo(1, 2)[:bar], "baz")), [])
344328
end
345329
end
346330

0 commit comments

Comments
 (0)