Finish FarmbotCeleryScript.DotProps. TODO: Add `meta.*` attr support to `update_resource` RPC.

mark_as
Rick Carlino 2020-04-30 21:44:26 -05:00
parent e2696cd93b
commit 3b66b164a7
2 changed files with 19 additions and 14 deletions

View File

@ -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

View File

@ -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