mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
Perform coercion for query strings in parser module
This commit is contained in:
parent
2f182e9068
commit
2dd2f43f37
10 changed files with 17 additions and 16 deletions
|
@ -94,7 +94,6 @@ defmodule Philomena.Comments.Query do
|
|||
|
||||
def compile(query_string, opts \\ []) do
|
||||
user = Keyword.get(opts, :user)
|
||||
query_string = query_string || ""
|
||||
|
||||
case user do
|
||||
nil ->
|
||||
|
|
|
@ -35,7 +35,6 @@ defmodule Philomena.Filters.Query do
|
|||
|
||||
def compile(query_string, opts \\ []) do
|
||||
user = Keyword.get(opts, :user)
|
||||
query_string = query_string || ""
|
||||
|
||||
case user do
|
||||
nil ->
|
||||
|
|
|
@ -15,8 +15,6 @@ defmodule Philomena.Galleries.Query do
|
|||
end
|
||||
|
||||
def compile(query_string) do
|
||||
query_string = query_string || ""
|
||||
|
||||
fields()
|
||||
|> Parser.new()
|
||||
|> Parser.parse(query_string)
|
||||
|
|
|
@ -147,7 +147,6 @@ defmodule Philomena.Images.Query do
|
|||
def compile(query_string, opts \\ []) do
|
||||
user = Keyword.get(opts, :user)
|
||||
watch = Keyword.get(opts, :watch, false)
|
||||
query_string = query_string || ""
|
||||
|
||||
case user do
|
||||
nil ->
|
||||
|
|
|
@ -92,7 +92,6 @@ defmodule Philomena.Posts.Query do
|
|||
|
||||
def compile(query_string, opts \\ []) do
|
||||
user = Keyword.get(opts, :user)
|
||||
query_string = query_string || ""
|
||||
|
||||
case user do
|
||||
nil ->
|
||||
|
|
|
@ -17,6 +17,6 @@ defmodule Philomena.Reports.Query do
|
|||
def compile(query_string) do
|
||||
fields()
|
||||
|> Parser.new()
|
||||
|> Parser.parse(query_string || "", %{})
|
||||
|> Parser.parse(query_string, %{})
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,6 +20,6 @@ defmodule Philomena.Tags.Query do
|
|||
def compile(query_string) do
|
||||
fields()
|
||||
|> Parser.new()
|
||||
|> Parser.parse(query_string || "")
|
||||
|> Parser.parse(query_string)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -184,18 +184,18 @@ defmodule PhilomenaQuery.Parse.Parser do
|
|||
@spec parse(t(), String.t(), context()) :: {:ok, query()} | {:error, String.t()}
|
||||
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
|
||||
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}
|
||||
else
|
||||
{:error, :empty_query} ->
|
||||
{:ok, %{match_none: %{}}}
|
||||
|
||||
{:ok, {_tree, tokens}} ->
|
||||
{:error, "junk at end of expression: " <> debug_tokens(tokens)}
|
||||
|
||||
|
@ -211,6 +211,13 @@ defmodule PhilomenaQuery.Parse.Parser do
|
|||
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
|
||||
Enum.map_join(tokens, fn {_k, v} -> v end)
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ defmodule PhilomenaWeb.Api.Json.Search.GalleryController do
|
|||
import Ecto.Query
|
||||
|
||||
def index(conn, params) do
|
||||
case Query.compile(params["q"] || "") do
|
||||
case Query.compile(params["q"]) do
|
||||
{:ok, query} ->
|
||||
galleries =
|
||||
Gallery
|
||||
|
|
|
@ -7,7 +7,7 @@ defmodule PhilomenaWeb.Api.Json.Search.TagController do
|
|||
import Ecto.Query
|
||||
|
||||
def index(conn, params) do
|
||||
case Query.compile(params["q"] || "") do
|
||||
case Query.compile(params["q"]) do
|
||||
{:ok, query} ->
|
||||
tags =
|
||||
Tag
|
||||
|
|
Loading…
Reference in a new issue