Yeap, you beat me to it. I think that if I was writing this I would probably do:
@spec reshape_map_int_fields(map) :: map
def reshape_map_int_fields(map) when is_map(map),
do: Enum.reduce(map, %{}, fn {k, v}, acc ->
Map.put(acc, k, maybe_reshape(k, v))
end)
@spec maybe_reshape(atom | String.t, term) :: integer | nil | term
def maybe_reshape(k, v) when is_atom(k) and is_binary(v),
do: maybe_reshape(Atom.to_string(k), v)
def maybe_reshape(<<"int_", _>>, v) when is_binary(v) do
case Integer.parse(v) do
{new_val, _} -> new_val
_ -> nil
end
end
# perhaps add a special clause for when it's still `"int_"` but not binary, not nil
# and not integer to error out, or nillify but that would depend
def maybe_reshape(_, v), do: v
This would have the benefit of also converting binary keys besides atom ones.






















