helper functions

This commit is contained in:
Moon Man 2024-09-06 12:22:32 +00:00
parent 97cedc73b6
commit 6f9a4f42f3
1 changed files with 32 additions and 0 deletions

View File

@ -10,4 +10,36 @@ defmodule Vonbraun.Util do
def my_key_id() do
"#{my_id()}#main-key"
end
@doc """
For the passed thing, get only the ID. If binary, assumed to be the ID. There
should only be one item in list if it is a list, otherwise return an error.
"""
def get_only_id(id) when is_binary(id), do: {:ok, id}
def get_only_id(%{"@id" => id}) when is_binary(id), do: {:ok, id}
def get_only_id([thing]) when is_binary(thing) or is_map(thing), do: get_only_id(thing)
def get_only_id(_), do: :error
@doc """
For the passed property value, assume there should only be one value. If a
list, return the item if length of list is one, else error. Any other type,
return it as-is.
"""
def get_only_property([property]), do: {:ok, property}
def get_only_property(property) when is_list(property), do: {:error}
def get_only_property(property), do: {:ok, property}
@doc """
For a passed value, if it is a bare id
"""
def is_only_id?(object = %{"@id" => id}) when map_size(object) == 1 and is_binary(id), do: true
def is_only_id?([value]) when not is_list(value), do: is_only_id?(value)
def is_only_id?(_), do: false
end