-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from muhifauzan/struct-support
Add destructure structs feature
- Loading branch information
Showing
4 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,10 @@ defmodule Destructure do | |
""" | ||
|
||
@doc """ | ||
Easy destructuring of maps and keyword lists, with atom keys only. String keys | ||
are not supported because Elixir raises a `SyntaxError` on syntax like | ||
`%{"name"}`. | ||
Easy destructuring of maps, structs, and keyword lists, with atom keys only. | ||
String keys are not supported because Elixir raises a `SyntaxError` on syntax | ||
like `%{"name"}`. Optional key also need to be placed at the last for the same | ||
reason with the string key. | ||
## Examples | ||
|
@@ -20,8 +21,7 @@ defmodule Destructure do | |
Or in case/for/with statements. | ||
iex> case %{name: "Mike"} do | ||
...> d%{name} -> | ||
...> name | ||
...> d%{name} -> name | ||
...> end | ||
"Mike" | ||
|
@@ -43,6 +43,24 @@ defmodule Destructure do | |
...> {first, last, mail} | ||
{"Daniel", "Berkompas", "[email protected]"} | ||
For structs: | ||
iex> d(%Person{name}) = %Person{name: "Daniel Berkompas"} | ||
...> name | ||
"Daniel Berkompas" | ||
With multiple keys: | ||
iex> d(%Person{name, email}) = %Person{name: "Daniel Berkompas", email: "[email protected]"} | ||
...> {name, email} | ||
{"Daniel Berkompas", "[email protected]"} | ||
With multiple keys and custom variable naming: | ||
iex> d(%Person{name, email: mail}) = %Person{name: "Daniel Berkompas", email: "[email protected]"} | ||
...> {name, mail} | ||
{"Daniel Berkompas", "[email protected]"} | ||
For keyword lists: | ||
iex> d({name}) = [name: "Daniel"] | ||
|
@@ -78,8 +96,16 @@ defmodule Destructure do | |
{:%{}, context, Enum.map(args, &pattern/1)} | ||
end | ||
|
||
# Handle structs, including ones with multiple keys | ||
# {:%, [], | ||
# [{:__aliases__, [alias: false], [:Namespace]}, | ||
# {:%{}, [], [{:first, [], Elixir}, {:second, [], Elixir}]}]} | ||
defmacro d({:%, _, [{:__aliases__, _, _}, {:%{}, context, args}]}) do | ||
{:%{}, context, Enum.map(args, &pattern/1)} | ||
end | ||
|
||
# Handle 1 and 3+ element tuples | ||
# {:{}, [], [{:variable_name, [], Elixir}]} | ||
# {:{}, [], [{:first, [], Elixir}]} | ||
defmacro d({:{}, _, args}) do | ||
Enum.map(args, &pattern/1) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters