tinc_build/codegen/cel/functions/
filter.rs

1use proc_macro2::TokenStream;
2use quote::{ToTokens, quote};
3use syn::parse_quote;
4use tinc_cel::CelValue;
5
6use super::Function;
7use crate::codegen::cel::compiler::{CompileError, CompiledExpr, CompilerCtx, ConstantCompiledExpr, RuntimeCompiledExpr};
8use crate::codegen::cel::types::CelType;
9use crate::types::{ProtoModifiedValueType, ProtoType, ProtoValueType};
10
11#[derive(Debug, Clone, Default)]
12pub(crate) struct Filter;
13
14fn native_impl(iter: TokenStream, item_ident: syn::Ident, compare: impl ToTokens) -> syn::Expr {
15    parse_quote!({
16        let mut collected = Vec::new();
17        let mut iter = (#iter).into_iter();
18        loop {
19            let Some(#item_ident) = iter.next() else {
20                break ::tinc::__private::cel::CelValue::List(collected.into());
21            };
22
23            if {
24                let #item_ident = #item_ident.clone();
25                #compare
26            } {
27                collected.push(#item_ident);
28            }
29        }
30    })
31}
32
33// this.filter(<ident>, <expr>)
34impl Function for Filter {
35    fn name(&self) -> &'static str {
36        "filter"
37    }
38
39    fn syntax(&self) -> &'static str {
40        "<this>.filter(<ident>, <expr>)"
41    }
42
43    fn compile(&self, ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
44        let Some(this) = &ctx.this else {
45            return Err(CompileError::syntax("missing this", self));
46        };
47
48        if ctx.args.len() != 2 {
49            return Err(CompileError::syntax("invalid number of args", self));
50        }
51
52        let cel_parser::Expression::Ident(variable) = &ctx.args[0] else {
53            return Err(CompileError::syntax("first argument must be an ident", self));
54        };
55
56        match this {
57            CompiledExpr::Runtime(RuntimeCompiledExpr { expr, ty }) => {
58                let mut child_ctx = ctx.child();
59
60                match ty {
61                    CelType::CelValue => {
62                        child_ctx.add_variable(variable, CompiledExpr::runtime(CelType::CelValue, parse_quote!(item)));
63                    }
64                    CelType::Proto(ProtoType::Modified(
65                        ProtoModifiedValueType::Repeated(ty) | ProtoModifiedValueType::Map(ty, _),
66                    )) => {
67                        child_ctx.add_variable(
68                            variable,
69                            CompiledExpr::runtime(CelType::Proto(ProtoType::Value(ty.clone())), parse_quote!(item)),
70                        );
71                    }
72                    v => {
73                        return Err(CompileError::TypeConversion {
74                            ty: Box::new(v.clone()),
75                            message: "type cannot be iterated over".to_string(),
76                        });
77                    }
78                };
79
80                let arg = child_ctx.resolve(&ctx.args[1])?.into_bool(&child_ctx);
81
82                Ok(CompiledExpr::runtime(
83                    CelType::CelValue,
84                    match ty {
85                        CelType::CelValue => parse_quote! {
86                            ::tinc::__private::cel::CelValue::cel_filter(#expr, |item| {
87                                ::core::result::Result::Ok(
88                                    #arg
89                                )
90                            })?
91                        },
92                        CelType::Proto(ProtoType::Modified(ProtoModifiedValueType::Map(ty, _))) => {
93                            let cel_ty =
94                                CompiledExpr::runtime(CelType::Proto(ProtoType::Value(ty.clone())), parse_quote!(item))
95                                    .into_cel()?;
96
97                            native_impl(
98                                quote!(
99                                    (#expr).keys().map(|item| #cel_ty)
100                                ),
101                                parse_quote!(item),
102                                arg,
103                            )
104                        }
105                        CelType::Proto(ProtoType::Modified(ProtoModifiedValueType::Repeated(ty))) => {
106                            let cel_ty =
107                                CompiledExpr::runtime(CelType::Proto(ProtoType::Value(ty.clone())), parse_quote!(item))
108                                    .into_cel()?;
109
110                            native_impl(
111                                quote!(
112                                    (#expr).iter().map(|item| #cel_ty)
113                                ),
114                                parse_quote!(item),
115                                arg,
116                            )
117                        }
118                        _ => unreachable!(),
119                    },
120                ))
121            }
122            CompiledExpr::Constant(ConstantCompiledExpr {
123                value: value @ (CelValue::List(_) | CelValue::Map(_)),
124            }) => {
125                let compile_val = |value: CelValue<'static>| {
126                    let mut child_ctx = ctx.child();
127
128                    child_ctx.add_variable(variable, CompiledExpr::constant(value.clone()));
129
130                    child_ctx.resolve(&ctx.args[1]).map(|v| (value, v.into_bool(&child_ctx)))
131                };
132
133                let collected: Result<Vec<_>, _> = match value {
134                    CelValue::List(item) => item.iter().cloned().map(compile_val).collect(),
135                    CelValue::Map(item) => item.iter().map(|(key, _)| key).cloned().map(compile_val).collect(),
136                    _ => unreachable!(),
137                };
138
139                let collected = collected?;
140                if collected.iter().any(|(_, c)| matches!(c, CompiledExpr::Runtime(_))) {
141                    let collected = collected.into_iter().map(|(item, expr)| {
142                        let item = CompiledExpr::constant(item);
143                        quote! {
144                            if #expr {
145                                collected.push(#item);
146                            }
147                        }
148                    });
149
150                    Ok(CompiledExpr::runtime(
151                        CelType::Proto(ProtoType::Value(ProtoValueType::Bool)),
152                        parse_quote!({
153                            let mut collected = Vec::new();
154                            #(#collected)*
155                            ::tinc::__private::cel::CelValue::List(collected.into())
156                        }),
157                    ))
158                } else {
159                    Ok(CompiledExpr::constant(CelValue::List(
160                        collected
161                            .into_iter()
162                            .filter_map(|(item, c)| match c {
163                                CompiledExpr::Constant(ConstantCompiledExpr { value }) => {
164                                    if value.to_bool() {
165                                        Some(item)
166                                    } else {
167                                        None
168                                    }
169                                }
170                                _ => unreachable!("all values must be constant"),
171                            })
172                            .collect(),
173                    )))
174                }
175            }
176            CompiledExpr::Constant(ConstantCompiledExpr { value }) => Err(CompileError::TypeConversion {
177                ty: Box::new(CelType::CelValue),
178                message: format!("{value:?} cannot be iterated over"),
179            }),
180        }
181    }
182}
183
184#[cfg(test)]
185#[cfg(feature = "prost")]
186#[cfg_attr(coverage_nightly, coverage(off))]
187mod tests {
188    use quote::quote;
189    use syn::parse_quote;
190    use tinc_cel::{CelValue, CelValueConv};
191
192    use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
193    use crate::codegen::cel::functions::{Filter, Function};
194    use crate::codegen::cel::types::CelType;
195    use crate::types::{ProtoModifiedValueType, ProtoType, ProtoTypeRegistry, ProtoValueType};
196
197    #[test]
198    fn test_filter_syntax() {
199        let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
200        let compiler = Compiler::new(&registry);
201        insta::assert_debug_snapshot!(Filter.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
202        Err(
203            InvalidSyntax {
204                message: "missing this",
205                syntax: "<this>.filter(<ident>, <expr>)",
206            },
207        )
208        "#);
209
210        insta::assert_debug_snapshot!(Filter.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("hi".into()))), &[])), @r#"
211        Err(
212            InvalidSyntax {
213                message: "invalid number of args",
214                syntax: "<this>.filter(<ident>, <expr>)",
215            },
216        )
217        "#);
218
219        insta::assert_debug_snapshot!(Filter.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("hi".into()))), &[
220            cel_parser::parse("x").unwrap(),
221            cel_parser::parse("dyn(x >= 1)").unwrap(),
222        ])), @r#"
223        Err(
224            TypeConversion {
225                ty: CelValue,
226                message: "String(Borrowed(\"hi\")) cannot be iterated over",
227            },
228        )
229        "#);
230
231        insta::assert_debug_snapshot!(Filter.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::runtime(CelType::Proto(ProtoType::Value(ProtoValueType::Bool)), parse_quote!(input))), &[
232            cel_parser::parse("x").unwrap(),
233            cel_parser::parse("dyn(x >= 1)").unwrap(),
234        ])), @r#"
235        Err(
236            TypeConversion {
237                ty: Proto(
238                    Value(
239                        Bool,
240                    ),
241                ),
242                message: "type cannot be iterated over",
243            },
244        )
245        "#);
246
247        insta::assert_debug_snapshot!(Filter.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List([
248            CelValueConv::conv(0),
249            CelValueConv::conv(1),
250            CelValueConv::conv(-50),
251            CelValueConv::conv(50),
252        ].into_iter().collect()))), &[
253            cel_parser::parse("x").unwrap(),
254            cel_parser::parse("x >= 1").unwrap(),
255        ])), @r"
256        Ok(
257            Constant(
258                ConstantCompiledExpr {
259                    value: List(
260                        [
261                            Number(
262                                I64(
263                                    1,
264                                ),
265                            ),
266                            Number(
267                                I64(
268                                    50,
269                                ),
270                            ),
271                        ],
272                    ),
273                },
274            ),
275        )
276        ");
277
278        let input = CompiledExpr::constant(CelValue::Map(
279            [
280                (CelValueConv::conv("key0"), CelValueConv::conv(0)),
281                (CelValueConv::conv("key1"), CelValueConv::conv(1)),
282                (CelValueConv::conv("key2"), CelValueConv::conv(-50)),
283                (CelValueConv::conv("key3"), CelValueConv::conv(50)),
284            ]
285            .into_iter()
286            .collect(),
287        ));
288
289        let mut ctx = compiler.child();
290        ctx.add_variable("input", input.clone());
291
292        insta::assert_debug_snapshot!(Filter.compile(CompilerCtx::new(ctx, Some(input), &[
293            cel_parser::parse("x").unwrap(),
294            cel_parser::parse("input[x] >= 1").unwrap(),
295        ])), @r#"
296        Ok(
297            Constant(
298                ConstantCompiledExpr {
299                    value: List(
300                        [
301                            String(
302                                Borrowed(
303                                    "key1",
304                                ),
305                            ),
306                            String(
307                                Borrowed(
308                                    "key3",
309                                ),
310                            ),
311                        ],
312                    ),
313                },
314            ),
315        )
316        "#);
317    }
318
319    #[test]
320    #[cfg(not(valgrind))]
321    fn test_filter_runtime_map() {
322        let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
323        let mut compiler = Compiler::new(&registry);
324
325        let string_value = CompiledExpr::runtime(
326            CelType::Proto(ProtoType::Modified(ProtoModifiedValueType::Map(
327                ProtoValueType::String,
328                ProtoValueType::Int32,
329            ))),
330            parse_quote!(input),
331        );
332
333        compiler.add_variable("input", string_value.clone());
334
335        let output = Filter
336            .compile(CompilerCtx::new(
337                compiler.child(),
338                Some(string_value),
339                &[cel_parser::parse("x").unwrap(), cel_parser::parse("input[x] >= 1").unwrap()],
340            ))
341            .unwrap();
342
343        insta::assert_snapshot!(postcompile::compile_str!(
344            postcompile::config! {
345                test: true,
346                dependencies: vec![
347                    postcompile::Dependency::version("tinc", "*"),
348                ],
349            },
350            quote! {
351                fn filter(input: &std::collections::BTreeMap<String, i32>) -> Result<::tinc::__private::cel::CelValue<'_>, ::tinc::__private::cel::CelError<'_>> {
352                    Ok(#output)
353                }
354
355                #[test]
356                fn test_filter() {
357                    assert_eq!(filter(&{
358                        let mut map = std::collections::BTreeMap::new();
359                        map.insert("0".to_string(), 0);
360                        map.insert("1".to_string(), 1);
361                        map.insert("-50".to_string(), -50);
362                        map.insert("50".to_string(), 50);
363                        map
364                    }).unwrap(), ::tinc::__private::cel::CelValue::List([
365                        ::tinc::__private::cel::CelValueConv::conv("1"),
366                        ::tinc::__private::cel::CelValueConv::conv("50"),
367                    ].into_iter().collect()));
368                }
369            },
370        ));
371    }
372
373    #[test]
374    #[cfg(not(valgrind))]
375    fn test_filter_runtime_repeated() {
376        let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
377        let compiler = Compiler::new(&registry);
378
379        let string_value = CompiledExpr::runtime(
380            CelType::Proto(ProtoType::Modified(ProtoModifiedValueType::Repeated(ProtoValueType::Int32))),
381            parse_quote!(input),
382        );
383
384        let output = Filter
385            .compile(CompilerCtx::new(
386                compiler.child(),
387                Some(string_value),
388                &[cel_parser::parse("x").unwrap(), cel_parser::parse("x >= 1").unwrap()],
389            ))
390            .unwrap();
391
392        insta::assert_snapshot!(postcompile::compile_str!(
393            postcompile::config! {
394                test: true,
395                dependencies: vec![
396                    postcompile::Dependency::version("tinc", "*"),
397                ],
398            },
399            quote! {
400                fn filter(input: &Vec<i32>) -> Result<::tinc::__private::cel::CelValue<'_>, ::tinc::__private::cel::CelError<'_>> {
401                    Ok(#output)
402                }
403
404                #[test]
405                fn test_filter() {
406                    assert_eq!(filter(&vec![0, 1, -50, 50]).unwrap(), ::tinc::__private::cel::CelValue::List([
407                        ::tinc::__private::cel::CelValueConv::conv(1),
408                        ::tinc::__private::cel::CelValueConv::conv(50),
409                    ].into_iter().collect()));
410                }
411            },
412        ));
413    }
414
415    #[test]
416    #[cfg(not(valgrind))]
417    fn test_filter_runtime_cel_value() {
418        let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
419        let compiler = Compiler::new(&registry);
420
421        let string_value = CompiledExpr::runtime(CelType::CelValue, parse_quote!(input));
422
423        let output = Filter
424            .compile(CompilerCtx::new(
425                compiler.child(),
426                Some(string_value),
427                &[cel_parser::parse("x").unwrap(), cel_parser::parse("x > 5").unwrap()],
428            ))
429            .unwrap();
430
431        insta::assert_snapshot!(postcompile::compile_str!(
432            postcompile::config! {
433                test: true,
434                dependencies: vec![
435                    postcompile::Dependency::version("tinc", "*"),
436                ],
437            },
438            quote! {
439                fn filter<'a>(input: &'a ::tinc::__private::cel::CelValue<'a>) -> Result<::tinc::__private::cel::CelValue<'a>, ::tinc::__private::cel::CelError<'a>> {
440                    Ok(#output)
441                }
442
443                #[test]
444                fn test_filter() {
445                    assert_eq!(filter(&tinc::__private::cel::CelValue::List([
446                        tinc::__private::cel::CelValueConv::conv(5),
447                        tinc::__private::cel::CelValueConv::conv(1),
448                        tinc::__private::cel::CelValueConv::conv(50),
449                         tinc::__private::cel::CelValueConv::conv(-50),
450                    ].into_iter().collect())).unwrap(), tinc::__private::cel::CelValue::List([
451                        tinc::__private::cel::CelValueConv::conv(50),
452                    ].into_iter().collect()));
453                }
454            },
455        ));
456    }
457
458    #[test]
459    #[cfg(not(valgrind))]
460    fn test_filter_const_requires_runtime() {
461        let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
462        let compiler = Compiler::new(&registry);
463
464        let list_value = CompiledExpr::constant(CelValue::List(
465            [CelValueConv::conv(5), CelValueConv::conv(0), CelValueConv::conv(1)]
466                .into_iter()
467                .collect(),
468        ));
469
470        let output = Filter
471            .compile(CompilerCtx::new(
472                compiler.child(),
473                Some(list_value),
474                &[cel_parser::parse("x").unwrap(), cel_parser::parse("dyn(x >= 1)").unwrap()],
475            ))
476            .unwrap();
477
478        insta::assert_snapshot!(postcompile::compile_str!(
479            postcompile::config! {
480                test: true,
481                dependencies: vec![
482                    postcompile::Dependency::version("tinc", "*"),
483                ],
484            },
485            quote! {
486                fn filter() -> Result<::tinc::__private::cel::CelValue<'static>, ::tinc::__private::cel::CelError<'static>> {
487                    Ok(#output)
488                }
489
490                #[test]
491                fn test_filter() {
492                    assert_eq!(filter().unwrap(), ::tinc::__private::cel::CelValue::List([
493                        ::tinc::__private::cel::CelValueConv::conv(5),
494                        ::tinc::__private::cel::CelValueConv::conv(1),
495                    ].into_iter().collect()));
496                }
497            },
498        ));
499    }
500}