From 6f9a4f42f34aacacb69cd4ef75ee0f1703c4fe1e Mon Sep 17 00:00:00 2001 From: Moon Man Date: Fri, 6 Sep 2024 12:22:32 +0000 Subject: [PATCH] helper functions --- lib/vonbraun/util.ex | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/vonbraun/util.ex b/lib/vonbraun/util.ex index f40eccc..5763b41 100644 --- a/lib/vonbraun/util.ex +++ b/lib/vonbraun/util.ex @@ -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