Skip to content

Commit

Permalink
fix converting string map to atom, after halt error
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Nov 4, 2023
1 parent 815001f commit a1f3c68
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog for MishkaDeveloperTools 0.1.2

---
- [ ] Solving the problem of creating extra `atom` in case of a mistake or an attack on the system. It could be a `security` issue, please update.
- [x] Solving the problem of creating extra `atom` in case of a mistake or an attack on the system. It could be a `security` issue, please update.
---

- [x] Add allowed parent domain core key `Enum` derive style
Expand Down
26 changes: 14 additions & 12 deletions lib/macros/guarded_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,9 @@ defmodule GuardedStruct do
|> auto_core_key(core_keys, type)
|> domain_core_key()
|> on_core_key(attrs)
|> from_core_key(attrs)
|> conditional_fields_validating(conditionals, type, key, attrs)
|> sub_fields_validating(fields, module, external, attrs, key, type)
|> from_core_key()
|> conditional_fields_validating(conditionals, type, key)
|> sub_fields_validating(fields, module, external, key, type)
|> fields_validating(validator, module)
|> main_validating(found_main_validator, main_validator, module)
|> replace_condition_fields_derives(derives)
Expand Down Expand Up @@ -1508,16 +1508,17 @@ defmodule GuardedStruct do
defp on_core_key({:error, _, _, :halt} = error, _full_attrs), do: error

defp on_core_key({:ok, attrs, core_keys}, full_attrs) do
full_attrs = Parser.convert_to_atom_map(full_attrs)
dependent_keys_errors = check_dependent_keys(attrs, core_keys, full_attrs)

if length(dependent_keys_errors) == 0,
do: {:ok, attrs, core_keys},
do: {:ok, attrs, core_keys, full_attrs},
else: {:error, :dependent_keys, dependent_keys_errors, :halt}
end

defp from_core_key({:error, _, _, :halt} = error, _full_attrs), do: error
defp from_core_key({:error, _, _, :halt} = error), do: error

defp from_core_key({:ok, attrs, core_keys}, full_attrs) do
defp from_core_key({:ok, attrs, core_keys, full_attrs}) do
reduce_attrs =
Enum.filter(core_keys, fn {_key, %{type: type, values: _}} -> type == :from end)
|> Enum.reduce(attrs, fn {key, %{type: :from, values: pattern}}, acc ->
Expand All @@ -1531,12 +1532,12 @@ defmodule GuardedStruct do
end
end)

{:ok, reduce_attrs}
{:ok, reduce_attrs, full_attrs}
end

defp conditional_fields_validating({:error, _, _, :halt} = error, _, _, _, _), do: error
defp conditional_fields_validating({:error, _, _, :halt} = error, _, _, _), do: error

defp conditional_fields_validating({:ok, attrs}, conditionals, type, key, full_attrs) do
defp conditional_fields_validating({:ok, attrs, full_attrs}, conditionals, type, key) do
{cond_fields, uncond_fields} =
Enum.reduce(attrs, {%{}, %{}}, fn {key, val}, {cond_acc, uncond_acc} ->
if Keyword.has_key?(conditionals, key),
Expand Down Expand Up @@ -1587,13 +1588,13 @@ defmodule GuardedStruct do
end)

cond_data = conditionals_fields_data_divider(cond_builders)
{:ok, uncond_fields, cond_data}
{:ok, uncond_fields, cond_data, full_attrs}
end

@doc false
def sub_fields_validating({:error, _, _, :halt} = error, _, _, _, _, _, _), do: error
def sub_fields_validating({:error, _, _, :halt} = error, _, _, _, _, _), do: error

def sub_fields_validating({:ok, attrs, conds}, fields, module, external, full_attrs, key, type) do
def sub_fields_validating({:ok, attrs, conds, full_attrs}, fields, module, external, key, type) do
allowed_fields = Map.take(attrs, fields) |> Map.keys()
sub_modules = get_fields_sub_module(module, allowed_fields, external)

Expand Down Expand Up @@ -1974,6 +1975,7 @@ defmodule GuardedStruct do

errors || {:ok, Enum.map(builders_output, &elem(&1, 1))}
else
IO.inspect(attrs)
{:error, :bad_parameters, "Your input must be a list of items"}
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/guarded_struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,17 @@ defmodule MishkaDeveloperToolsTest.GuardedStructTest do
})
end

test "nested string map to atom with derive and validation" do
{:ok, _nested_struct} =
assert TestUserAuthStruct.builder(%{
"name" => "mishka",
"auth_path" => [
%{"action" => "*:admin", "path" => %{"role" => "1"}},
%{"action" => "*:user", "path" => %{"role" => "3"}}
]
})
end

############## (▰˘◡˘▰) GuardedStructTest Tests helper functions (▰˘◡˘▰) ##############
# Extracts the first type from a module.
defp types(bytecode) do
Expand Down

0 comments on commit a1f3c68

Please sign in to comment.