Skip to main content

tonneau_build/codegen/cel/functions/
map.rs

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