Perform coercion for query strings in parser module

This commit is contained in:
Liam 2024-07-13 15:25:36 -04:00
parent 2f182e9068
commit 2dd2f43f37
10 changed files with 17 additions and 16 deletions

View file

@ -94,7 +94,6 @@ defmodule Philomena.Comments.Query do
def compile(query_string, opts \\ []) do def compile(query_string, opts \\ []) do
user = Keyword.get(opts, :user) user = Keyword.get(opts, :user)
query_string = query_string || ""
case user do case user do
nil -> nil ->

View file

@ -35,7 +35,6 @@ defmodule Philomena.Filters.Query do
def compile(query_string, opts \\ []) do def compile(query_string, opts \\ []) do
user = Keyword.get(opts, :user) user = Keyword.get(opts, :user)
query_string = query_string || ""
case user do case user do
nil -> nil ->

View file

@ -15,8 +15,6 @@ defmodule Philomena.Galleries.Query do
end end
def compile(query_string) do def compile(query_string) do
query_string = query_string || ""
fields() fields()
|> Parser.new() |> Parser.new()
|> Parser.parse(query_string) |> Parser.parse(query_string)

View file

@ -147,7 +147,6 @@ defmodule Philomena.Images.Query do
def compile(query_string, opts \\ []) do def compile(query_string, opts \\ []) do
user = Keyword.get(opts, :user) user = Keyword.get(opts, :user)
watch = Keyword.get(opts, :watch, false) watch = Keyword.get(opts, :watch, false)
query_string = query_string || ""
case user do case user do
nil -> nil ->

View file

@ -92,7 +92,6 @@ defmodule Philomena.Posts.Query do
def compile(query_string, opts \\ []) do def compile(query_string, opts \\ []) do
user = Keyword.get(opts, :user) user = Keyword.get(opts, :user)
query_string = query_string || ""
case user do case user do
nil -> nil ->

View file

@ -17,6 +17,6 @@ defmodule Philomena.Reports.Query do
def compile(query_string) do def compile(query_string) do
fields() fields()
|> Parser.new() |> Parser.new()
|> Parser.parse(query_string || "", %{}) |> Parser.parse(query_string, %{})
end end
end end

View file

@ -20,6 +20,6 @@ defmodule Philomena.Tags.Query do
def compile(query_string) do def compile(query_string) do
fields() fields()
|> Parser.new() |> Parser.new()
|> Parser.parse(query_string || "") |> Parser.parse(query_string)
end end
end end

View file

@ -184,18 +184,18 @@ defmodule PhilomenaQuery.Parse.Parser do
@spec parse(t(), String.t(), context()) :: {:ok, query()} | {:error, String.t()} @spec parse(t(), String.t(), context()) :: {:ok, query()} | {:error, String.t()}
def parse(parser, input, context \\ nil) def parse(parser, input, context \\ nil)
# Empty search should emit a match_none.
def parse(_parser, "", _context) do
{:ok, %{match_none: %{}}}
end
def parse(%Parser{} = parser, input, context) do def parse(%Parser{} = parser, input, context) do
parser = %{parser | __data__: context} parser = %{parser | __data__: context}
with {:ok, tokens, _1, _2, _3, _4} <- Lexer.lex(input), with {:ok, input} <- coerce_string(input),
{:ok, tokens, _1, _2, _3, _4} <- Lexer.lex(input),
{:ok, tokens} <- convert_empty_token_list(tokens),
{:ok, {tree, []}} <- search_top(parser, tokens) do {:ok, {tree, []}} <- search_top(parser, tokens) do
{:ok, tree} {:ok, tree}
else else
{:error, :empty_query} ->
{:ok, %{match_none: %{}}}
{:ok, {_tree, tokens}} -> {:ok, {_tree, tokens}} ->
{:error, "junk at end of expression: " <> debug_tokens(tokens)} {:error, "junk at end of expression: " <> debug_tokens(tokens)}
@ -211,6 +211,13 @@ defmodule PhilomenaQuery.Parse.Parser do
end end
end end
defp coerce_string(term) when is_binary(term), do: {:ok, term}
defp coerce_string(nil), do: {:ok, ""}
defp coerce_string(_), do: {:error, "search query is not a string"}
defp convert_empty_token_list([]), do: {:error, :empty_query}
defp convert_empty_token_list(tokens), do: {:ok, tokens}
defp debug_tokens(tokens) do defp debug_tokens(tokens) do
Enum.map_join(tokens, fn {_k, v} -> v end) Enum.map_join(tokens, fn {_k, v} -> v end)
end end

View file

@ -7,7 +7,7 @@ defmodule PhilomenaWeb.Api.Json.Search.GalleryController do
import Ecto.Query import Ecto.Query
def index(conn, params) do def index(conn, params) do
case Query.compile(params["q"] || "") do case Query.compile(params["q"]) do
{:ok, query} -> {:ok, query} ->
galleries = galleries =
Gallery Gallery

View file

@ -7,7 +7,7 @@ defmodule PhilomenaWeb.Api.Json.Search.TagController do
import Ecto.Query import Ecto.Query
def index(conn, params) do def index(conn, params) do
case Query.compile(params["q"] || "") do case Query.compile(params["q"]) do
{:ok, query} -> {:ok, query} ->
tags = tags =
Tag Tag