Packages

An elixir library for MAVLink, an application that enables communication with other systems using the MAVLink protocol over serial, UDP and TCP connections, and utility modules for performing common MAVLink commands and tasks with one or more remote vehicles.

Current section

Files

Jump to
xmavlink lib mavlink_util tables.ex
Raw

lib/mavlink_util/tables.ex

defmodule XMAVLink.Util.Tables do
@moduledoc """
Utility ETS table name helpers.
Most applications should use `XMAVLink.Util.CacheManager` query functions
instead of reading ETS tables directly. When an integration needs table names
for supervision or migration code, use this module rather than hard-coding
global names such as `:messages`, `:systems`, `:params`, and `:sessions`.
"""
@kinds [:messages, :systems, :params, :sessions]
@type kind :: :messages | :systems | :params | :sessions
@type prefix :: atom | String.t() | nil
@type opts_or_prefix ::
keyword | %{optional(:table_prefix) => prefix} | XMAVLink.Util.Context.t() | prefix
@doc """
Return all utility table names for a context, prefix, or options.
"""
@spec names(opts_or_prefix) :: %{required(kind) => atom}
def names(opts_or_prefix \\ []) do
prefix = prefix(opts_or_prefix)
Map.new(@kinds, fn kind ->
{kind, table_name(kind, prefix)}
end)
end
@doc """
Return one utility table name for a context, prefix, or options.
"""
@spec name(kind, opts_or_prefix) :: atom
def name(kind, opts_or_prefix \\ []) when kind in @kinds do
table_name(kind, prefix(opts_or_prefix))
end
defp table_name(kind, nil), do: kind
defp table_name(kind, prefix), do: :"#{prefix}_#{kind}"
defp prefix(opts) when is_list(opts) do
case Keyword.fetch(opts, :context) do
{:ok, %{table_prefix: prefix}} -> Keyword.get(opts, :table_prefix, prefix)
:error -> Keyword.get(opts, :table_prefix)
end
end
defp prefix(%{table_prefix: prefix}), do: prefix
defp prefix(nil), do: nil
defp prefix(prefix) when is_atom(prefix) or is_binary(prefix), do: prefix
end