-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ex
134 lines (111 loc) · 3.24 KB
/
parser.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
defmodule QueryParser.AST.Parser do
@moduledoc """
This module parse Json query into an AST tree.
It takes care of simplifications and organize a tree that is easy to navigate into.
"""
alias QueryParser.AST.{
And,
ArrayValue,
BooleanValue,
Contains,
DataKey,
Eq,
Find,
In,
MeRef,
NumberValue,
Or,
Query,
Select,
StringValue
}
def from_json(nil), do: nil
def from_json(q) do
parse_query(q, %{})
end
defp parse_query(%{"$find" => find}, ctx) do
%Query{
find: parse_find(find, ctx),
select: %Select{clause: nil}
}
end
defp parse_find(find, ctx) do
%Find{clause: parse_expr(find, ctx)}
end
# Map is equivalent to a "$and" clause
defp parse_expr(clauses, ctx) when is_map(clauses) do
parse_expr({"$and", Map.to_list(clauses)}, ctx)
end
# A key that starts with $ is a function
defp parse_expr({"$" <> _ = k, val}, ctx) do
parse_fun({k, val}, ctx)
end
# A simple k => v clause
defp parse_expr({k, v}, ctx) do
ctx = Map.merge(ctx, %{left: from_k(k, ctx)})
# ctx =
# case left do
# nil ->
# Map.merge(ctx, %{left: from_k(k, ctx)})
# left ->
# Map.merge(ctx, %{
# left: %DataKey{
# key_path: Enum.concat(left.key_path, [k])
# }
# })
# end
parse_expr(v, ctx)
end
# If there is a left in context, and is not a function, this is a simplified $eq function
defp parse_expr(value, %{left: _} = ctx) do
parse_expr({"$eq", value}, ctx)
end
# List with eq_value ctx is an ArrayValue
defp parse_expr(clauses, ctx) when is_list(clauses) do
%ArrayValue{values: Enum.map(clauses, &parse_expr(&1, ctx))}
end
defp parse_expr("@me", _ctx) do
%MeRef{}
end
defp parse_expr(value, _ctx) when is_bitstring(value) do
%StringValue{value: value}
end
defp parse_expr(value, _ctx) when is_number(value) do
%NumberValue{value: value}
end
defp parse_expr(value, _ctx) when is_boolean(value) do
%BooleanValue{value: value}
end
# Simplification of an "$and" function with only one clause
defp parse_fun({"$and", [clause]}, ctx) do
parse_expr(clause, ctx)
end
defp parse_fun({"$and", clauses}, ctx) when is_list(clauses) do
%And{clauses: Enum.map(clauses, &parse_expr(&1, ctx))}
end
# Or function
defp parse_fun({"$or", clauses}, ctx) when is_list(clauses) do
%Or{clauses: Enum.map(clauses, &parse_expr(&1, ctx))}
end
# Eq function
defp parse_fun({"$eq", val}, %{left: _} = ctx) do
{left, ctx} = Map.pop(ctx, :left)
%Eq{left: left, right: parse_expr(val, ctx)}
end
# Eq function
defp parse_fun({"$contains", val}, %{left: _} = ctx) do
{left, ctx} = Map.pop(ctx, :left)
%Contains{field: left, value: parse_expr(val, ctx)}
end
# in function
defp parse_fun({"$in", clauses}, %{left: _} = ctx) when is_list(clauses) do
{left, ctx} = Map.pop(ctx, :left)
%In{field: left, values: Enum.map(clauses, &parse_expr(&1, ctx))}
end
defp parse_fun({name, _value}, _ctx) do
raise "Could not parse function #{name}. Validator should not accept this function."
end
defp from_k(key, _ctx) when is_bitstring(key) do
%DataKey{key_path: String.split(key, ".")}
end
end