From 8a11da83e835819ee338cf2e26ea2f1553c5805e Mon Sep 17 00:00:00 2001 From: Ashlynne Mitchell Date: Fri, 27 Jun 2025 18:48:47 +1000 Subject: [PATCH] feat: add `skip_cache` option for identity resolver --- .gitignore | 3 ++- .vscode/settings.json | 3 +++ lib/atex/identity_resolver.ex | 11 +++++++++-- lib/atex/identity_resolver/did_document.ex | 2 -- 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 9a651fe..70c8ba2 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ atex-*.tar /tmp/ .envrc -.direnv \ No newline at end of file +.direnv +.vscode/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..07eabfd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "git.enabled": false +} diff --git a/lib/atex/identity_resolver.ex b/lib/atex/identity_resolver.ex index f084f56..4141780 100644 --- a/lib/atex/identity_resolver.ex +++ b/lib/atex/identity_resolver.ex @@ -2,12 +2,19 @@ defmodule Atex.IdentityResolver do alias Atex.IdentityResolver.{Cache, DID, DIDDocument, Handle, Identity} @handle_strategy Application.compile_env(:atex, :handle_resolver_strategy, :dns_first) + @type options() :: {:skip_cache, boolean()} # TODO: simplify errors - def resolve(identifier) do + @spec resolve(String.t(), list(options())) :: {:ok, Identity.t()} | {:error, any()} + def resolve(identifier, opts \\ []) do + opts = Keyword.validate!(opts, skip_cache: false) + skip_cache = Keyword.get(opts, :skip_cache) + + cache_result = if skip_cache, do: {:error, :not_found}, else: Cache.get(identifier) + # If cache fetch succeeds, then the ok tuple will be retuned by the default `with` behaviour - with {:error, :not_found} <- Cache.get(identifier), + with {:error, :not_found} <- cache_result, {:ok, identity} <- do_resolve(identifier), identity <- Cache.insert(identity) do {:ok, identity} diff --git a/lib/atex/identity_resolver/did_document.ex b/lib/atex/identity_resolver/did_document.ex index 7b6a343..9a1301b 100644 --- a/lib/atex/identity_resolver/did_document.ex +++ b/lib/atex/identity_resolver/did_document.ex @@ -68,8 +68,6 @@ defmodule Atex.IdentityResolver.DIDDocument do @spec from_json(map()) :: {:ok, t()} | {:error, Peri.Error.t()} def from_json(%{} = map) do map - # TODO: `atomize_keys` instead? Peri doesn't convert nested schemas to atoms but does for the base schema. - # Smells like a PR if I've ever smelt one... |> Recase.Enumerable.convert_keys(&Recase.to_snake/1) |> schema() |> case do -- 2.51.2