Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: lenra_common dependancy #40

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions lib/errors/business_error.ex

This file was deleted.

38 changes: 31 additions & 7 deletions lib/exec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ defmodule QueryParser.Exec do

require Logger

alias LenraCommon.JsonHelper

@all_operator ["$nin", "$not", "$nor"]

defp is_bson_type(value, type) do
Expand Down Expand Up @@ -38,10 +36,8 @@ defmodule QueryParser.Exec do
exec?(ast, elem, %{})
end

@doc """
exec? will take a bool expression and execute it on the element.
It returns true if the elem matches the expression.
"""
# exec? will take a bool expression and execute it on the element.
# It returns true if the elem matches the expression.
# Case of a "expression" with a list of clauses. All clauses must match.
defp exec?(%{"pos" => "expression", "clauses" => clauses}, elem, ctx) do
Enum.all?(clauses, &exec?(&1, elem, ctx))
Expand Down Expand Up @@ -139,7 +135,7 @@ defmodule QueryParser.Exec do

defp exec?(%{"pos" => "leaf-clause", "key" => key, "value" => value}, elem, ctx) do
key_list = String.split(key, ".")
elem_value = JsonHelper.get_in_json(elem, key_list)
elem_value = get_in_json(elem, key_list)

# If the next element is a leaf-value, this is a "short equal"
if Map.get(value, "pos") == "leaf-value" do
Expand Down Expand Up @@ -234,4 +230,32 @@ defmodule QueryParser.Exec do
Logger.error("operator does not exist #{operator}")
false
end

######################
## Helper functions ##
######################

defp get_in_json(nil, [_]), do: {:error, :nil_json}
defp get_in_json(nil, [_ | t]), do: get_in_json(nil, t)

defp get_in_json(data, [h]) when is_bitstring(h) and is_list(data),
do: get_in_list(data, h)

defp get_in_json(data, [h]) when is_bitstring(h), do: data[h]

defp get_in_json(data, [h | t]) when is_bitstring(h) and is_list(data),
do: get_in_json(get_in_list(data, h), t)

defp get_in_json(data, [h | t]) when is_bitstring(h),
do: get_in_json(data[h], t)

defp get_in_list(data, h) do
case Integer.parse(h) do
:error ->
{:error, :invalid_index}

{number, _} ->
Enum.at(data, number)
end
end
end
7 changes: 3 additions & 4 deletions lib/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule QueryParser.Parser do
@moduledoc """
This Parser module will use the grammar to parse the query string into an AST.
"""
alias QueryParser.Errors.BusinessError
alias QueryParser.Parser.Grammar

# Sadly, the warning in the grammar file do propagate with these function.
Expand All @@ -13,14 +12,14 @@ defmodule QueryParser.Parser do
@dialyzer {:nowarn_function, parse!: 2}
@dialyzer {:nowarn_function, replace: 2}

@spec parse(String.t(), map()) :: {:ok, any()} | {:error, LenraCommon.Errors.BusinessError.t()}
@spec parse(String.t(), map()) :: {:ok, any()} | {:error, :invalid_query}
def parse(query_str, params \\ %{}) do
case Grammar.parse(query_str) do
{:error, _term} ->
BusinessError.invalid_query_tuple()
{:error, :invalid_query}

:mismatch ->
BusinessError.invalid_query_tuple()
{:error, :invalid_query}

{:ok, res} ->
{:ok, replace(res, params)}
Expand Down
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ defmodule QueryParser.MixProject do
{:dialyxir, "~> 1.2", only: [:dev, :test], runtime: false},
{:execjs, "~> 2.0", only: [:test], runtime: false},
{:neotomex, "~> 0.1.7"},
{:poison, "~> 5.0", override: true},
{:lenra_common, "~> 2.8"}
{:poison, "~> 5.0", override: true}
]
end
end
14 changes: 2 additions & 12 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
%{
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"castore": {:hex, :castore, "0.1.22", "4127549e411bedd012ca3a308dede574f43819fe9394254ca55ab4895abfa1a2", [:mix], [], "hexpm", "c17576df47eb5aa1ee40cc4134316a99f5cad3e215d5c77b8dd3cfef12a22cac"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
"dialyxir": {:hex, :dialyxir, "1.4.0", "6b698401c16de79e8596b73dca63762255e70e4bbe26423530e173917220d5fc", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "c7ecaa1da27debae488ab09d9827ec58a0161c7821972b6d2cb26c1614648849"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"execjs": {:hex, :execjs, "2.0.0", "9002fd5cc98750f8465994f44a09fb7ca74b709bfaf171518c7f68f334624805", [:mix], [{:poison, "~> 4.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "68441697283a9ddcf41ed572199aba2b3b3481cc145d992e563e4865fc39751a"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"lenra_common": {:hex, :lenra_common, "2.8.0", "ff35f3acb3af1002ced050a60252961d2d6c3bb3d0689cf145ab574133891136", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.6.15", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "06d4bb116a10e581dd45c40703f266e2290de815296165daaf20969bc0bba075"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"neotomex": {:hex, :neotomex, "0.1.7", "64f76513653aa87ea7abdde0fd600e56955d838020a13d88f2bf334c88ac3e7a", [:mix], [], "hexpm", "4b87b8f614d1cd89dc8ba80ba0e559bedb3ebf6f6d74cd774fcfdd215e861445"},
"phoenix": {:hex, :phoenix, "1.6.15", "0a1d96bbc10747fd83525370d691953cdb6f3ccbac61aa01b4acb012474b047d", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0 or ~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d70ab9fbf6b394755ea88b644d34d79d8b146e490973151f248cacd122d20672"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.1", "ba04e489ef03763bf28a17eb2eaddc2c20c6d217e2150a61e3298b0f4c2012b5", [:mix], [], "hexpm", "81367c6d1eea5878ad726be80808eb5a787a23dee699f96e72b1109c57cdd8d9"},
"phoenix_template": {:hex, :phoenix_template, "1.0.0", "c57bc5044f25f007dc86ab21895688c098a9f846a8dda6bc40e2d0ddc146e38f", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "1b066f99a26fd22064c12b2600a9a6e56700f591bf7b20b418054ea38b4d4357"},
"phoenix_view": {:hex, :phoenix_view, "2.0.2", "6bd4d2fd595ef80d33b439ede6a19326b78f0f1d8d62b9a318e3d9c1af351098", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}], "hexpm", "a929e7230ea5c7ee0e149ffcf44ce7cf7f4b6d2bfe1752dd7c084cdff152d36f"},
"plug": {:hex, :plug, "1.14.0", "ba4f558468f69cbd9f6b356d25443d0b796fbdc887e03fa89001384a9cac638f", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bf020432c7d4feb7b3af16a0c2701455cbbbb95e5b6866132cb09eb0c29adc14"},
"plug_crypto": {:hex, :plug_crypto, "1.2.3", "8f77d13aeb32bfd9e654cb68f0af517b371fb34c56c9f2b58fe3df1235c1251a", [:mix], [], "hexpm", "b5672099c6ad5c202c45f5a403f21a3411247f164e4a8fab056e5cd8a290f4a2"},
"poison": {:hex, :poison, "5.0.0", "d2b54589ab4157bbb82ec2050757779bfed724463a544b6e20d79855a9e43b24", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "11dc6117c501b80c62a7594f941d043982a1bd05a1184280c0d9166eb4d8d3fc"},
"telemetry": {:hex, :telemetry, "1.2.0", "a8ce551485a9a3dac8d523542de130eafd12e40bbf76cf0ecd2528f24e812a44", [:rebar3], [], "hexpm", "1427e73667b9a2002cf1f26694c422d5c905df889023903c4518921d53e3e883"},
}
2 changes: 0 additions & 2 deletions test/exec_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule QueryParser.ExecTest do
use ExUnit.Case

alias LenraCommon.Errors.BusinessError

def parse_and_exec(data, query, params \\ %{}) do
ast =
query
Expand Down
3 changes: 1 addition & 2 deletions test/support/js_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Using Execjs, we load the js parser and exec it to parse the mongo query.
this way, we can compare the result of the js parser with our elixir parser in unit test.
"""
alias QueryParser.Errors.BusinessError

def parse!(json) do
parser = File.read!("./parser.js") |> Execjs.compile()
Expand All @@ -15,7 +14,7 @@
res = parse!(json)
{:ok, res}
rescue
e ->

Check warning on line 17 in test/support/js_parser.ex

View workflow job for this annotation

GitHub Actions / Checks

variable "e" is unused (if the variable is not meant to be used, prefix it with an underscore)
BusinessError.invalid_query_tuple()
{:error, :invalid_query}
end
end
Loading