Add not
function for negation of a value (#1542)
This commit is contained in:
parent
c2dce39f91
commit
54f337c15b
4 changed files with 72 additions and 0 deletions
|
@ -13,6 +13,7 @@ pub mod http;
|
|||
pub mod is;
|
||||
pub mod math;
|
||||
pub mod meta;
|
||||
pub mod not;
|
||||
pub mod operate;
|
||||
pub mod parse;
|
||||
pub mod rand;
|
||||
|
@ -136,6 +137,8 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
|
|||
"meta::table" => meta::tb,
|
||||
"meta::tb" => meta::tb,
|
||||
//
|
||||
"not" => not::not,
|
||||
//
|
||||
"parse::email::host" => parse::email::host,
|
||||
"parse::email::user" => parse::email::user,
|
||||
"parse::url::domain" => parse::url::domain,
|
||||
|
|
7
lib/src/fnc/not.rs
Normal file
7
lib/src/fnc/not.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use crate::sql::Value;
|
||||
use crate::Error;
|
||||
|
||||
/// Returns a boolean that is false if the input is truthy and true otherwise.
|
||||
pub fn not((val,): (Value,)) -> Result<Value, Error> {
|
||||
Ok((!val.is_truthy()).into())
|
||||
}
|
|
@ -223,6 +223,7 @@ fn function_names(i: &str) -> IResult<&str, &str> {
|
|||
function_is,
|
||||
function_math,
|
||||
function_meta,
|
||||
function_not,
|
||||
function_parse,
|
||||
function_rand,
|
||||
function_session,
|
||||
|
@ -360,6 +361,10 @@ fn function_meta(i: &str) -> IResult<&str, &str> {
|
|||
alt((tag("meta::id"), tag("meta::table"), tag("meta::tb")))(i)
|
||||
}
|
||||
|
||||
fn function_not(i: &str) -> IResult<&str, &str> {
|
||||
tag("not")(i)
|
||||
}
|
||||
|
||||
fn function_parse(i: &str) -> IResult<&str, &str> {
|
||||
alt((
|
||||
tag("parse::email::host"),
|
||||
|
@ -477,6 +482,16 @@ mod tests {
|
|||
assert_eq!(out, Function::Normal(String::from("count"), vec![]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_single_not() {
|
||||
let sql = "not(1.2345)";
|
||||
let res = function(sql);
|
||||
assert!(res.is_ok());
|
||||
let out = res.unwrap().1;
|
||||
assert_eq!("not(1.2345)", format!("{}", out));
|
||||
assert_eq!(out, Function::Normal("not".to_owned(), vec![1.2345.into()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_module() {
|
||||
let sql = "rand::uuid()";
|
||||
|
|
|
@ -299,3 +299,50 @@ async fn function_string_slice() -> Result<(), Error> {
|
|||
//
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn function_not() -> Result<(), Error> {
|
||||
let sql = r#"
|
||||
RETURN not(true);
|
||||
RETURN not(not(true));
|
||||
RETURN not(false);
|
||||
RETURN not(not(false));
|
||||
RETURN not(0);
|
||||
RETURN not(1);
|
||||
RETURN not("hello");
|
||||
"#;
|
||||
let dbs = Datastore::new("memory").await?;
|
||||
let ses = Session::for_kv().with_ns("test").with_db("test");
|
||||
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
||||
assert_eq!(res.len(), 7);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::False;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::True;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::True;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::False;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::True;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::False;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::False;
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue