@@ -277,3 +277,66 @@ func TestOptimize_filter_last(t *testing.T) {
277
277
278
278
assert .Equal (t , ast .Dump (expected ), ast .Dump (tree .Node ))
279
279
}
280
+
281
+ func TestOptimize_filter_map (t * testing.T ) {
282
+ tree , err := parser .Parse (`map(filter(users, .Name == "Bob"), .Age)` )
283
+ require .NoError (t , err )
284
+
285
+ err = optimizer .Optimize (& tree .Node , nil )
286
+ require .NoError (t , err )
287
+
288
+ expected := & ast.BuiltinNode {
289
+ Name : "filter" ,
290
+ Arguments : []ast.Node {
291
+ & ast.IdentifierNode {Value : "users" },
292
+ & ast.ClosureNode {
293
+ Node : & ast.BinaryNode {
294
+ Operator : "==" ,
295
+ Left : & ast.MemberNode {
296
+ Node : & ast.PointerNode {},
297
+ Property : & ast.StringNode {Value : "Name" },
298
+ },
299
+ Right : & ast.StringNode {Value : "Bob" },
300
+ },
301
+ },
302
+ },
303
+ Map : & ast.MemberNode {
304
+ Node : & ast.PointerNode {},
305
+ Property : & ast.StringNode {Value : "Age" },
306
+ },
307
+ }
308
+
309
+ assert .Equal (t , ast .Dump (expected ), ast .Dump (tree .Node ))
310
+ }
311
+
312
+ func TestOptimize_filter_map_first (t * testing.T ) {
313
+ tree , err := parser .Parse (`first(map(filter(users, .Name == "Bob"), .Age))` )
314
+ require .NoError (t , err )
315
+
316
+ err = optimizer .Optimize (& tree .Node , nil )
317
+ require .NoError (t , err )
318
+
319
+ expected := & ast.BuiltinNode {
320
+ Name : "find" ,
321
+ Arguments : []ast.Node {
322
+ & ast.IdentifierNode {Value : "users" },
323
+ & ast.ClosureNode {
324
+ Node : & ast.BinaryNode {
325
+ Operator : "==" ,
326
+ Left : & ast.MemberNode {
327
+ Node : & ast.PointerNode {},
328
+ Property : & ast.StringNode {Value : "Name" },
329
+ },
330
+ Right : & ast.StringNode {Value : "Bob" },
331
+ },
332
+ },
333
+ },
334
+ Map : & ast.MemberNode {
335
+ Node : & ast.PointerNode {},
336
+ Property : & ast.StringNode {Value : "Age" },
337
+ },
338
+ Throws : false ,
339
+ }
340
+
341
+ assert .Equal (t , ast .Dump (expected ), ast .Dump (tree .Node ))
342
+ }
0 commit comments