| 1 |
|
defmodule Tools.Sys.Set do |
| 2 |
|
import Log, only: [log: 5, agent: 5] |
| 3 |
|
import Map, only: [put: 3] |
| 4 |
|
import Mem, only: [depth: 0, table: 1] |
| 5 |
|
|
| 6 |
|
@icon "✏️" |
| 7 |
|
|
| 8 |
|
def spec(name, _state) do |
| 9 |
72 |
spec(name) |
| 10 |
|
end |
| 11 |
|
|
| 12 |
|
defp spec(name) do |
| 13 |
72 |
%{name: name, description: description(), parameters: parameters()} |
| 14 |
|
end |
| 15 |
|
|
| 16 |
72 |
defp description do |
| 17 |
|
"Store data with a key" |
| 18 |
|
end |
| 19 |
|
|
| 20 |
:-( |
def icon, do: @icon |
| 21 |
|
|
| 22 |
25 |
def exec(_tool, %{"value" => value, "key" => key}, %{name: name} = state) do |
| 23 |
25 |
log(@icon, key, " = ", value, :blue) |
| 24 |
25 |
agent(@icon, key, " = ", value, %{name: name}) |
| 25 |
25 |
store(name, key, value) |
| 26 |
|
{"stored", state} |
| 27 |
|
end |
| 28 |
|
|
| 29 |
|
def exec(tool, %{"value" => value} = args, state) do |
| 30 |
:-( |
exec(tool, put(args, "key", value), state) |
| 31 |
|
end |
| 32 |
|
|
| 33 |
:-( |
def exec(_, _args, state) do |
| 34 |
|
{"set needs key and value", state} |
| 35 |
|
end |
| 36 |
|
|
| 37 |
|
defp parameters do |
| 38 |
72 |
%{type: "object", properties: props(), required: ["key", "value"]} |
| 39 |
|
end |
| 40 |
|
|
| 41 |
|
defp props do |
| 42 |
72 |
%{ |
| 43 |
|
key: %{type: "string", description: "The key to store data under"}, |
| 44 |
|
value: %{type: "string", description: "The value to store"} |
| 45 |
|
} |
| 46 |
|
end |
| 47 |
|
|
| 48 |
|
defp store(name, key, value) do |
| 49 |
25 |
key |> pick(name) |> :ets.insert({key, value}) |
| 50 |
|
end |
| 51 |
|
|
| 52 |
:-( |
defp pick("depth_" <> _, _name), do: depth() |
| 53 |
:-( |
defp pick("tree_" <> _, _name), do: depth() |
| 54 |
25 |
defp pick(_key, name), do: table(name) |
| 55 |
|
end |