Skip to content

Commit

Permalink
Fix arrays in nested transformations (#1661)
Browse files Browse the repository at this point in the history
* Respecting indexes in arrays
  • Loading branch information
RyanRConaway authored Apr 14, 2023
1 parent 21c572a commit b0ba68a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/alchemist/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Alchemist.MixProject do
def project do
[
app: :alchemist,
version: "0.2.30",
version: "0.2.31",
elixir: "~> 1.10",
build_path: "../../_build",
config_path: "../../config/config.exs",
Expand Down
36 changes: 31 additions & 5 deletions apps/transformers/lib/transform_actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,19 @@ defmodule Transformers do
|> Enum.with_index()
|> Enum.reduce(acc, fn {value, index}, enum_acc ->
parent_key = "#{concat_key(key, parent_key)}[#{index}]"
child_payload = flatten_payload(value, parent_key)
Map.merge(enum_acc, child_payload)

case value do
innerListValue when is_list(innerListValue) ->
child_payload = flatten_payload(innerListValue, parent_key)
Map.merge(enum_acc, child_payload)

innerMapValue when is_map(innerMapValue) ->
child_payload = flatten_payload(innerMapValue, parent_key)
Map.merge(enum_acc, child_payload)

primitiveValue ->
Map.put(enum_acc, parent_key, primitiveValue)
end
end)

value ->
Expand All @@ -93,8 +104,24 @@ defmodule Transformers do
defp split_payload(payload) do
Enum.reduce(payload, %{}, fn {key, value}, acc ->
case String.split(key, ".") do
hierarchy when length(hierarchy) == 1 ->
Map.put(acc, hd(hierarchy), value)
[head | []] ->
if Regex.match?(~r/\[.\]/, head) do
base_parent_key = Regex.replace(~r/\[.\]/, head, "")

index =
Regex.scan(~r/\[.\]/, head)
|> hd()
|> hd()
|> String.replace("[", "")
|> String.replace("]", "")
|> String.to_integer()

current_acc_list = Map.get(acc, base_parent_key, [])

Map.put(acc, base_parent_key, current_acc_list ++ [value])
else
Map.put(acc, head, value)
end

hierarchy ->
{parent_key, child_hierarchy} = List.pop_at(hierarchy, 0)
Expand All @@ -115,7 +142,6 @@ defmodule Transformers do
updated_map = create_child_map(child_hierarchy, value)

updated_acc = List.insert_at(current_acc, index, updated_map)

Map.put(acc, base_parent_key, updated_acc)
else
map_child(parent_key, child_hierarchy, value, acc)
Expand Down
2 changes: 1 addition & 1 deletion apps/transformers/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Transformers.MixProject do
def project do
[
app: :transformers,
version: "1.0.25",
version: "1.0.26",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down

0 comments on commit b0ba68a

Please sign in to comment.