[UNSTABLE] Fix pattern matches in do_update_resource

pull/1200/head
Rick Carlino 2020-05-11 10:59:36 -05:00
parent 285fb1a491
commit f0ea202196
3 changed files with 23 additions and 14 deletions

View File

@ -2,7 +2,7 @@ defmodule FarmbotCeleryScript.Compiler.IdentifierSanitizer do
@moduledoc """
Responsible for ensuring variable names in Sequences are clean.
This is done because identifiers are `unquote`d and the user controls
the data inside them. To prevent things like
the data inside them. To prevent things like
`"System.cmd("rm -rf /*/**")"` being evaluated, all identifiers
are sanitized by prepending a token and hashing the value.
"""

View File

@ -1,5 +1,6 @@
defmodule FarmbotCeleryScript.Compiler.Sequence do
import FarmbotCeleryScript.Compiler.Utils
alias FarmbotCeleryScript.Compiler.IdentifierSanitizer
@iterables [:point_group, :every_point]
@ -113,19 +114,25 @@ defmodule FarmbotCeleryScript.Compiler.Sequence do
end
def create_better_params(body, env) do
Enum.reduce(body, %{}, fn ast, map ->
parameter_declarations = Enum.reduce(env, %{}, fn
{key, value}, map ->
encoded_label = "#{key}"
if String.starts_with?(encoded_label, "unsafe_") do
Map.put(map, IdentifierSanitizer.to_string(encoded_label), value)
else
map
end
end)
Enum.reduce(body, parameter_declarations, fn ast, map ->
args = Map.fetch!(ast, :args)
label = Map.fetch!(args, :label)
case ast do
%{kind: :variable_declaration} ->
Map.put(map, label, Map.fetch!(args, :data_value))
%{kind: :parameter_declaration} ->
IO.inspect(ast, label: "######### AST")
Map.put(map, label, compile_param_declaration(ast, env))
_ ->
raise "How do I compile this? #{inspect(Map.fetch!(ast, :args))}"
%{kind: :parameter_declaration} -> map
_ -> raise "How do I compile this? #{inspect(Map.fetch!(ast, :args))}"
end
end)
end

View File

@ -12,17 +12,19 @@ defmodule FarmbotCeleryScript.Compiler.UpdateResource do
args = Map.fetch!(variable, :args)
label = Map.fetch!(args, :label)
resource = Map.fetch!(better_params, label)
me.do_update(resource, update)
me.do_update(resource.args, update)
%AST{kind: :resource} ->
me.do_update(variable.args, update)
res ->
raise "Resource error. Please notfiy support: #{inspect(res)}"
%AST{kind: :point} -> me.do_update(variable.args, update)
%AST{kind: :resource} -> me.do_update(variable.args, update)
res -> raise "Resource error. Please notfiy support: #{inspect(res)}"
end
end
end
def do_update(%{pointer_id: id, pointer_type: kind}, update_params) do
FarmbotCeleryScript.SysCalls.update_resource(kind, id, update_params)
end
def do_update(%{resource_id: id, resource_type: kind}, update_params) do
FarmbotCeleryScript.SysCalls.update_resource(kind, id, update_params)
end