From ec0c28fa741c397c5213c716874d80ef6b0c71bf Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Thu, 15 Aug 2024 10:17:00 +0200 Subject: [PATCH] `.chain()` method (#4513) --- core/src/fnc/mod.rs | 3 +++ core/src/fnc/shared.rs | 16 ++++++++++++++++ lib/tests/function.rs | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 core/src/fnc/shared.rs diff --git a/core/src/fnc/mod.rs b/core/src/fnc/mod.rs index 03143b95..e304a1fb 100644 --- a/core/src/fnc/mod.rs +++ b/core/src/fnc/mod.rs @@ -27,6 +27,7 @@ pub mod rand; pub mod script; pub mod search; pub mod session; +pub mod shared; pub mod sleep; pub mod string; pub mod time; @@ -700,6 +701,8 @@ pub async fn idiom( "to_uuid" => r#type::uuid, // "repeat" => array::repeat, + // + "chain" => shared::chain((stk, ctx, opt, doc)).await, ) } v => v, diff --git a/core/src/fnc/shared.rs b/core/src/fnc/shared.rs new file mode 100644 index 00000000..c54320f7 --- /dev/null +++ b/core/src/fnc/shared.rs @@ -0,0 +1,16 @@ +use reblessive::tree::Stk; + +use crate::ctx::Context; +use crate::dbs::Options; +use crate::doc::CursorDoc; +use crate::err::Error; +use crate::sql::value::Value; +use crate::sql::{Closure, Function}; + +pub async fn chain( + (stk, ctx, opt, doc): (&mut Stk, &Context<'_>, &Options, Option<&CursorDoc<'_>>), + (value, worker): (Value, Closure), +) -> Result { + let fnc = Function::Anonymous(worker.into(), vec![value]); + fnc.compute(stk, ctx, opt, doc).await +} diff --git a/lib/tests/function.rs b/lib/tests/function.rs index 9aa4bd11..0921cc9f 100644 --- a/lib/tests/function.rs +++ b/lib/tests/function.rs @@ -6349,6 +6349,7 @@ async fn function_idiom_chaining() -> Result<(), Error> { // String is one of the types in the initial match statement, // this test ensures that the dispatch macro does not exit early "string".is_bool(); + ["1", "2"].join('').chain(|$v| $v); "#; Test::new(sql) .await? @@ -6358,7 +6359,8 @@ async fn function_idiom_chaining() -> Result<(), Error> { .expect_val("true")? .expect_error("There was a problem running the doesnt_exist() function. no such method found for the bool type")? .expect_val("true")? - .expect_val("false")?; + .expect_val("false")? + .expect_val("12")?; Ok(()) }