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

Rename and document nested map conversion function #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 lib/system_registry/processor/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule SystemRegistry.Processor.State do

map ->
frag_reserved =
Transaction.scope(scope, map)
Transaction.to_nested_map(scope, map)
|> Node.leaf_nodes()
|> Enum.map(&%Node{node: &1})
|> permissions(t.pid)
Expand Down
17 changes: 13 additions & 4 deletions lib/system_registry/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule SystemRegistry.Transaction do
[leaf | internal_nodes]
|> Enum.reduce(t.update_nodes, &MapSet.put(&2, &1))

scope_map = scope(scope, value)
scope_map = to_nested_map(scope, value)
updates = deep_merge(t.updates, scope_map)
%{t | update_nodes: nodes, updates: updates}
end
Expand Down Expand Up @@ -161,7 +161,16 @@ defmodule SystemRegistry.Transaction do
end)
end

def scope(scope, value) do
@doc """
Convert the given scope and value to nested maps

```elixir
iex> SystemRegistry.Transaction.to_nested_map([:a, :b, :c], 1)
%{a: %{b: %{c: 1}}}
```
"""
@spec to_nested_map(SystemRegistry.scope(), any()) :: map()
def to_nested_map(scope, value) do
scope
|> Enum.reverse()
|> Enum.reduce(value, &Map.put(%{}, &1, &2))
Expand All @@ -179,7 +188,7 @@ defmodule SystemRegistry.Transaction do

map ->
scopes =
scope(scope, map)
to_nested_map(scope, map)
|> Node.leaf_nodes()

deletes ++ scopes
Expand All @@ -205,7 +214,7 @@ defmodule SystemRegistry.Transaction do
{delete_nodes, MapSet.put(deletes, node)}

true ->
scope = scope(node.node, %{})
scope = to_nested_map(node.node, %{})
reg = Registry.match(S, t.key, scope) |> strip()
reg_scope = get_in(reg, node.node)

Expand Down
4 changes: 4 additions & 0 deletions test/transaction_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule SystemRegistry.TransactionTest do
use SystemRegistryTest.Case
doctest SystemRegistry.Transaction
end