From 448944cb02d1a84a3cd0ffe7d202eeba55e26fca Mon Sep 17 00:00:00 2001 From: Parker Selbert Date: Fri, 1 Dec 2023 10:29:59 -0600 Subject: [PATCH] Correct appending choices with a . separator Clicking or tab completing a qualified term and a dot would incorrectly construct a compound term. Now, completing a partial filter like `workers:MyApp.Al` correctly returns `workers:MyApp.Alpha` instead of the broken `workers:MyApp:MyApp.Alpha`. Closes #83 --- lib/oban/web/query.ex | 27 ++++++++++++++++----------- test/oban/web/query_test.exs | 7 +++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/oban/web/query.ex b/lib/oban/web/query.ex index ce899d08..c5345ccd 100644 --- a/lib/oban/web/query.ex +++ b/lib/oban/web/query.ex @@ -342,17 +342,22 @@ defmodule Oban.Web.Query do def append(terms, choice) do choice = if String.match?(choice, ~r/[\s,]/), do: ~s("#{choice}"), else: choice - if MapSet.member?(@known_qualifiers, choice) do - choice - else - joiner = if String.contains?(terms, ":"), do: ":", else: "." - - terms - |> String.reverse() - |> String.split([":", "."], parts: 2) - |> List.last() - |> String.reverse() - |> Kernel.<>("#{joiner}#{choice}") + cond do + MapSet.member?(@known_qualifiers, choice) -> + choice + + String.contains?(terms, ":") -> + [qualifier, _] = String.split(terms, ":", parts: 2) + + "#{qualifier}:#{choice}" + + true -> + terms + |> String.reverse() + |> String.split(["."], parts: 2) + |> List.last() + |> String.reverse() + |> Kernel.<>(".#{choice}") end end diff --git a/test/oban/web/query_test.exs b/test/oban/web/query_test.exs index 54dc96b3..e542f77d 100644 --- a/test/oban/web/query_test.exs +++ b/test/oban/web/query_test.exs @@ -237,6 +237,13 @@ defmodule Oban.Web.QueryTest do assert "args.id:" == complete("args.i") assert "args.account_id:" == complete("args.accou") end + + test "completing a qualifier with a partial path" do + insert_job!(%{}, worker: "MyApp.Alpha") + + assert "workers:MyApp.Alpha" == complete("workers:My") + assert "workers:MyApp.Alpha" == complete("workers:MyApp.A") + end end describe "append/2" do