Dynamic Orb module
Mix.install([:orb, :orb_wasmtime])
Section
defmodule CurrentUser do
def get_id() do
Process.get(:user_id) || raise "User ID must be set."
end
def get_type() do
Process.get(:user_type, :viewer)
end
def get() do
%{
type: get_type(),
id: get_id()
}
end
end
defmodule DynamicExample do
use Orb
defw can_edit?(post_id: I32, author_id: I32), I32 do
inline do
case CurrentUser.get() do
%{type: :viewer, id: user_id} ->
wasm do
author_id === user_id
end
%{type: :admin} ->
1
end
end
end
end
Process.put(:user_type, :viewer)
Process.put(:user_id, 1234)
wat = Orb.to_wat(DynamicExample)
IO.puts(wat)
OrbWasmtime.Wasm.call(wat, :can_edit?, 42, 1234)
|> IO.inspect(label: "can edit post by user 1234")
OrbWasmtime.Wasm.call(wat, :can_edit?, 42, 1235)
|> IO.inspect(label: "can edit post by user 1235")
OrbWasmtime.Wasm.call(wat, :can_edit?, 42, 876)
|> IO.inspect(label: "can edit post by user 876")
:ok
Process.put(:user_type, :admin)
Process.put(:user_id, 1)
wat = Orb.to_wat(DynamicExample)
IO.puts(wat)
OrbWasmtime.Wasm.call(wat, :can_edit?, 42, 1234)
|> IO.inspect(label: "can edit post by user 1234")
OrbWasmtime.Wasm.call(wat, :can_edit?, 42, 1235)
|> IO.inspect(label: "can edit post by user 1235")
OrbWasmtime.Wasm.call(wat, :can_edit?, 42, 876)
|> IO.inspect(label: "can edit post by user 876")
:ok