From b0ba68a323f166515494b97dabb00447e4b0cd44 Mon Sep 17 00:00:00 2001 From: RyanRConaway <79863335+RyanRConaway@users.noreply.github.com> Date: Fri, 14 Apr 2023 18:01:00 -0400 Subject: [PATCH] Fix arrays in nested transformations (#1661) * Respecting indexes in arrays --- apps/alchemist/mix.exs | 2 +- apps/transformers/lib/transform_actions.ex | 36 +++++++++++++++++++--- apps/transformers/mix.exs | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/apps/alchemist/mix.exs b/apps/alchemist/mix.exs index 5b8de84fa..c5d1a74a0 100644 --- a/apps/alchemist/mix.exs +++ b/apps/alchemist/mix.exs @@ -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", diff --git a/apps/transformers/lib/transform_actions.ex b/apps/transformers/lib/transform_actions.ex index b873d2faf..13548906a 100644 --- a/apps/transformers/lib/transform_actions.ex +++ b/apps/transformers/lib/transform_actions.ex @@ -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 -> @@ -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) @@ -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) diff --git a/apps/transformers/mix.exs b/apps/transformers/mix.exs index fdd118439..39b85645d 100644 --- a/apps/transformers/mix.exs +++ b/apps/transformers/mix.exs @@ -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",