Skip to content

Commit

Permalink
Correct appending choices with a . separator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sorentwo committed Dec 1, 2023
1 parent 41b4e86 commit 448944c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/oban/web/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions test/oban/web/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 448944c

Please sign in to comment.