@@ -1715,68 +1715,6 @@ defmodule Kernel do
1715
1715
Access . get_and_update ( data , h , & get_and_update_in ( & 1 , t , fun ) )
1716
1716
end
1717
1717
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
-
1780
1718
@ doc """
1781
1719
Puts a value in a nested structure via the given `path`.
1782
1720
@@ -1791,7 +1729,7 @@ defmodule Kernel do
1791
1729
1792
1730
Note that in order for this macro to work, the complete path must always
1793
1731
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.
1795
1733
1796
1734
## Examples
1797
1735
@@ -1824,7 +1762,7 @@ defmodule Kernel do
1824
1762
1825
1763
Note that in order for this macro to work, the complete path must always
1826
1764
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.
1828
1766
1829
1767
## Examples
1830
1768
@@ -1856,15 +1794,40 @@ defmodule Kernel do
1856
1794
get_and_update_in(opts, [:foo, :bar], &{&1, &1 + 1})
1857
1795
1858
1796
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.
1861
1798
1862
1799
## Examples
1863
1800
1864
1801
iex> users = %{"josé" => %{age: 27}, "eric" => %{age: 23}}
1865
1802
iex> get_and_update_in(users["josé"][:age], &{&1, &1 + 1})
1866
1803
{27, %{"josé" => %{age: 28}, "eric" => %{age: 23}}}
1867
1804
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
+
1868
1831
"""
1869
1832
defmacro get_and_update_in ( path , fun ) do
1870
1833
[ h | t ] = unnest ( path , [ ] , "get_and_update_in/2" )
0 commit comments