Skip to content

Instantly share code, notes, and snippets.

@gvaughn
Created March 1, 2016 00:58
Show Gist options
  • Save gvaughn/60e20c6bb6ab50421485 to your computer and use it in GitHub Desktop.
Save gvaughn/60e20c6bb6ab50421485 to your computer and use it in GitHub Desktop.
representation mapping
defmodule Mapper do
@x_mapping [bar: [:baar], constant: [:constant], foo: [:fooo], stuffiness: [:stuff, :stuffiness], thing_other: [:thing, :other], thing_sub: [:thing, :sub]]
def for_x(source_data) do
mapper_for(@x_mapping).(source_data)
end
defp mapper_for(mapping) do
&Enum.map(&1, fn input -> Map.new(mapping, fn {dest, src} -> {dest, get_in(input, src)} end) end)
end
end
data = [
%{
constant: "just stay there",
fooo: "foovalue",
baar: "barvalue",
thing: %{sub: "thing subfield", other: "thing anotherfield", discard: "nobodycares"},
stuff: %{stuff: true, stuffiness: "extreme"}
},
%{
constant: "just stay there as well",
fooo: "foovalue2",
baar: "barvalue2",
thing: %{sub: "thing subfield2", other: "thing anotherfield2", discard: "nobodycares"},
stuff: %{stuff: true, stuffiness: "moderate"}
},
]
Mapper.for_x(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment