.chain() method (#4513)

This commit is contained in:
Micha de Vries 2024-08-15 10:17:00 +02:00 committed by GitHub
parent 6502b5ed8a
commit ec0c28fa74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 1 deletions

View file

@ -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,

16
core/src/fnc/shared.rs Normal file
View file

@ -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<Value, Error> {
let fnc = Function::Anonymous(worker.into(), vec![value]);
fnc.compute(stk, ctx, opt, doc).await
}

View file

@ -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| <int> $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(())
}