Skip to content

Commit 9128f36

Browse files
committed
Add guards and extend trees functionality
1 parent 19aa078 commit 9128f36

4 files changed

Lines changed: 78 additions & 24 deletions

File tree

examples/user_pipeline.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def some_other_filters
6464
order: { 'courses.id': 'asc' },
6565
params: {
6666
limit: 10,
67-
offset: 0,
6867
not_supported: 'omitted'
6968
}
7069
}

lib/filterly/tree.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ def self.initialize_with_filters
2727
end
2828

2929
def extend_ast(node_attr_name, new_node, stmt_type)
30+
self.class.ensure_not_nil_node!(new_node)
31+
3032
@root_node = TreeTraverser
3133
.new(@root_node)
3234
.extend_ast(node_attr_name, new_node, stmt_type)
3335
end
3436

3537
def prepend_ast(new_node, stmt_type)
38+
self.class.ensure_not_nil_node!(new_node)
39+
3640
@root_node = TreeTraverser.new(@root_node).prepend_ast(new_node, stmt_type)
3741
end
3842

@@ -44,7 +48,12 @@ def to_s
4448
root_node.to_s
4549
end
4650

47-
# @apir private
51+
# @api private
52+
def self.ensure_not_nil_node!(ast_node)
53+
raise 'Tried to append nil node which is forbidden!' if ast_node.nil?
54+
end
55+
56+
# @api private
4857
def self.ensure_ast_root!(root_node)
4958
raise 'Not a tree root!' if root_node.type != :root
5059
end
@@ -95,6 +104,8 @@ def recreate_node(node_attr_name, new_node, stmt_type)
95104
end
96105

97106
def prepend_ast(new_node, stmt_type)
107+
return prepend_ast_without_statement(new_node) if ast_node.leaf?
108+
98109
create_node(
99110
ast_node.type,
100111
[
@@ -108,6 +119,18 @@ def prepend_ast(new_node, stmt_type)
108119
)
109120
end
110121

122+
# @api private
123+
def prepend_ast_without_statement(new_node)
124+
create_node(
125+
ast_node.type,
126+
[
127+
ast_node.value,
128+
new_node,
129+
nil
130+
]
131+
)
132+
end
133+
111134
# @api private
112135
def create_node(*args)
113136
Filterly::Node.new(*args)

spec/unit/pipeline/param_builder_spec.rb

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,28 @@
7373
[
7474
:and,
7575
[
76-
:statement,
76+
:expression,
7777
[
78-
:and,
78+
:op_in,
79+
[:attr_name, [:category_ids, [], []]],
7980
[
80-
:expression,
81+
:attr_array,
8182
[
82-
:op_in,
83-
[:attr_name, [:category_ids, [], []]],
84-
[
85-
:attr_array,
86-
[
87-
12,
88-
[:attr_array, [34, [], []]],
89-
[:attr_array, [54, [], []]]
90-
]
91-
]
92-
]
93-
],
94-
[
95-
:expression,
96-
[
97-
:op_equal,
98-
[:attr_name, [:course_semester_id, [], []]],
99-
[:attr_value, [123, [], []]]
83+
12,
84+
[:attr_array, [34, [], []]],
85+
[:attr_array, [54, [], []]]
10086
]
10187
]
10288
]
10389
],
104-
[]
90+
[
91+
:expression,
92+
[
93+
:op_equal,
94+
[:attr_name, [:course_semester_id, [], []]],
95+
[:attr_value, [123, [], []]]
96+
]
97+
]
10598
]
10699
]
107100
]

spec/unit/tree_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@
9999
end
100100

101101
describe '#extend_ast' do
102+
it 'tries to extend with nil node and fails' do
103+
tree = described_class.initialize_with_filters
104+
105+
expect { tree.extend_ast('name', nil, :or) }.to raise_error(
106+
'Tried to append nil node which is forbidden!'
107+
)
108+
end
109+
102110
it 'adds a new node to specific node and returns root' do
103111
subject.extend_ast(node_attr_name, new_node, :or)
104112

@@ -226,5 +234,36 @@
226234
]
227235
)
228236
end
237+
238+
it 'adds a new node without statement for empty tree' do
239+
tree = described_class.initialize_with_filters
240+
tree.prepend_ast(new_node, :or)
241+
242+
expect(tree.to_ast.to_a).to match_array(
243+
[
244+
:root,
245+
[
246+
:filters,
247+
[
248+
:expression,
249+
[
250+
:op_equal,
251+
[:attr_name, [:whatever_id, [], []]],
252+
[:attr_value, [13, [], []]]
253+
]
254+
],
255+
[]
256+
]
257+
]
258+
)
259+
end
260+
261+
it 'tries to add nil node and fails' do
262+
tree = described_class.initialize_with_filters
263+
264+
expect { tree.prepend_ast(nil, :or) }.to raise_error(
265+
'Tried to append nil node which is forbidden!'
266+
)
267+
end
229268
end
230269
end

0 commit comments

Comments
 (0)