View Source Exrpc.MFALookup (exrpc v0.3.6)

Function allowlist and lookup table.

Unlike :rpc, we don't transmit module and function names over the wire. Each function is assigned a unique integer ID, and the client sends the ID based on this lookup table.

# Instantiate a new lookup table: iex> Exrpc.MFALookup.create([])

iex> {:ok, lookup} = Exrpc.MFALookup.create([{Greeter, :hello, 1}, {Greeter, :goodbye, 1}, {Adder, :add, 2}])

# Route to ID and vice-versa: iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :hello, 1}) 0 iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :goodbye, 1}) 1 iex> Exrpc.MFALookup.mfa_to_id(lookup, {Adder, :add, 2}) 2 iex> Exrpc.MFALookup.mfa_to_id(lookup, {Greeter, :howdy, 1}) nil iex> Exrpc.MFALookup.id_to_mfa(lookup, 0) {Greeter, :hello, 1} iex> Exrpc.MFALookup.id_to_mfa(lookup, 1) {Greeter, :goodbye, 1} iex> Exrpc.MFALookup.id_to_mfa(lookup, 2) {Adder, :add, 2} iex> Exrpc.MFALookup.id_to_mfa(lookup, 3) nil # Convert lookup table to list (for transmitting to clients): iex> Exrpc.MFALookup.to_list(lookup) [{Greeter, :hello, 1}, {Greeter, :goodbye, 1}, {Adder, :add, 2}]

Summary

Types

@type function_name() :: atom()
@type id() :: integer()
@type t() :: {atom() | :ets.tid(), atom() | :ets.tid()}

Functions

@spec create([mfa()]) :: {:ok, t()} | {:error, atom()}
@spec id_to_mfa(t(), id()) :: {module(), function_name(), arity()} | nil
@spec mfa_to_id(t(), mfa()) :: id() | nil
@spec to_list(t()) :: [mfa()]