Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arrays in nested transformations #1661

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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