Skip to content

Instantly share code, notes, and snippets.

@nburkley
Forked from devonestes/struct_change.ex
Last active July 12, 2017 20:10
Show Gist options
  • Save nburkley/0a35dd7fe16c623c1e996351a409184b to your computer and use it in GitHub Desktop.
Save nburkley/0a35dd7fe16c623c1e996351a409184b to your computer and use it in GitHub Desktop.
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