mark_as
Rick Carlino 2020-04-22 17:49:23 -05:00
parent bce9f4385f
commit 968179322a
3 changed files with 32 additions and 51 deletions

View File

@ -40,35 +40,4 @@ defmodule FarmbotCeleryScript.Compiler.DataControl do
)
end
end
def resource_update(
%{
args: %{
resource_type: kind,
resource_id: id,
label: label,
value: value
},
body: body
},
env
) do
initial = %{label => value}
# Technically now body isn't supported by this node.
extra =
Map.new(body, fn %{args: %{label: label, data_value: value}} ->
{label, value}
end)
# Make sure the initial stuff higher most priority
params = Map.merge(extra, initial)
quote do
FarmbotCeleryScript.SysCalls.resource_update(
unquote(Compiler.compile_ast(kind, env)),
unquote(Compiler.compile_ast(id, env)),
unquote(Macro.escape(params))
)
end
end
end

View File

@ -1,24 +1,13 @@
defmodule FarmbotCeleryScript.Compiler.UpdateResource do
# alias FarmbotCeleryScript.Compiler
alias FarmbotCeleryScript.AST
def coordinate(_ast, _env) do
raise "TODO: coordinate"
end
def named_pin(_ast, _env) do
raise "TODO: named_pin"
end
def point(_ast, _env) do
raise "TODO: point"
end
def tool(_ast, _env) do
raise "TODO: tool"
end
def update_resource(_ast, _env) do
raise "Its here!"
def update_resource(ast, _env) do
params = destructure_pairs(ast.body, %{})
{id, type} = destructure_resource(Map.fetch!(ast.args, :resource))
IO.inspect(params)
IO.inspect(type)
IO.inspect(id)
raise "TODO: Convert symbolic `resource` into concrete resource"
quote do
# FarmbotCeleryScript.SysCalls.update_resource(
@ -28,4 +17,26 @@ defmodule FarmbotCeleryScript.Compiler.UpdateResource do
# )
end
end
defp destructure_resource(%AST{
kind: :resource,
args: %{
resource_id: id,
resource_type: type
}
}) do
{type, id}
end
defp destructure_pairs([pair | rest], acc) do
IO.puts("TODO: Need to apply handlebars to `value`s.")
key = Map.fetch!(pair.args, :label)
val = Map.fetch!(pair.args, :value)
next_acc = Map.merge(acc, %{key => val})
destructure_pairs(rest, next_acc)
end
defp destructure_pairs([], acc) do
acc
end
end

View File

@ -356,7 +356,8 @@ defmodule FarmbotCeleryScript.CompilerTest do
end
test "`update_resource`: Multiple fields of `resource` type." do
compiled = "test/fixtures/update_resource_multi.json"
compiled =
"test/fixtures/update_resource_multi.json"
|> File.read!()
|> Jason.decode!()
|> AST.decode()