Finish FarmbotCeleryScript.DotProps. TODO: Add `meta.*` attr support to `update_resource` RPC.
parent
e2696cd93b
commit
3b66b164a7
|
@ -1,13 +1,22 @@
|
|||
defmodule FarmbotCeleryScript.DotProps do
|
||||
def create(key, val) do
|
||||
if String.contains?(key, ".") do
|
||||
recurse(key, val, %{})
|
||||
else
|
||||
%{key => val}
|
||||
end
|
||||
end
|
||||
@dot "."
|
||||
@doc ~S"""
|
||||
Takes a "dotted" key and val.
|
||||
Returns deeply nested hash.
|
||||
|
||||
def recurse(_key, _val, acc) do
|
||||
acc
|
||||
## Examples
|
||||
|
||||
iex> create("foo.bar.baz", 321)
|
||||
%{"foo" => %{"bar" => %{"baz" => 321}}}
|
||||
|
||||
iex> create("foo", "bar")
|
||||
%{"foo" => "bar"}
|
||||
"""
|
||||
def create(dotted, val) do
|
||||
[key | list] = dotted |> String.split(@dot) |> Enum.reverse()
|
||||
|
||||
Enum.reduce(list, %{key => val}, fn next_key, acc ->
|
||||
%{next_key => acc}
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
defmodule FarmbotCeleryScript.DotPropsTest do
|
||||
use ExUnit.Case
|
||||
alias FarmbotCeleryScript.DotProps
|
||||
|
||||
test "converts dotted props to real nested maps" do
|
||||
assert %{"foo" => "bar"} == DotProps.create("foo", "bar")
|
||||
end
|
||||
doctest FarmbotCeleryScript.DotProps, import: true
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue