-
-
Save nburkley/0a35dd7fe16c623c1e996351a409184b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule Tester do | |
defstruct [:first, :second] | |
def foo_to_bar(struct = %{__struct__: struct_module}) do | |
updated_list = | |
struct | |
|> Map.from_struct | |
|> Enum.map(&transform_value/1) | |
struct(struct_module, updated_list) | |
end | |
defp transform_value({key, :foo}), do: {key, :bar} | |
defp transform_value({key, value}), do: {key, value} | |
end | |
iex(2)> s = %Tester{first: :foo, second: :baz} | |
%Tester{first: :foo, second: :baz} | |
iex(3)> Tester.foo_to_bar(s) | |
%Tester{first: :bar, second: :baz} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment