Add not function for negation of a value (#1542)

This commit is contained in:
Finn Bear 2022-12-30 01:34:46 -08:00 committed by GitHub
parent c2dce39f91
commit 54f337c15b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 0 deletions

View file

@ -13,6 +13,7 @@ pub mod http;
pub mod is; pub mod is;
pub mod math; pub mod math;
pub mod meta; pub mod meta;
pub mod not;
pub mod operate; pub mod operate;
pub mod parse; pub mod parse;
pub mod rand; pub mod rand;
@ -136,6 +137,8 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"meta::table" => meta::tb, "meta::table" => meta::tb,
"meta::tb" => meta::tb, "meta::tb" => meta::tb,
// //
"not" => not::not,
//
"parse::email::host" => parse::email::host, "parse::email::host" => parse::email::host,
"parse::email::user" => parse::email::user, "parse::email::user" => parse::email::user,
"parse::url::domain" => parse::url::domain, "parse::url::domain" => parse::url::domain,

7
lib/src/fnc/not.rs Normal file
View 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())
}

View file

@ -223,6 +223,7 @@ fn function_names(i: &str) -> IResult<&str, &str> {
function_is, function_is,
function_math, function_math,
function_meta, function_meta,
function_not,
function_parse, function_parse,
function_rand, function_rand,
function_session, 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) 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> { fn function_parse(i: &str) -> IResult<&str, &str> {
alt(( alt((
tag("parse::email::host"), tag("parse::email::host"),
@ -477,6 +482,16 @@ mod tests {
assert_eq!(out, Function::Normal(String::from("count"), vec![])); 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] #[test]
fn function_module() { fn function_module() {
let sql = "rand::uuid()"; let sql = "rand::uuid()";

View file

@ -299,3 +299,50 @@ async fn function_string_slice() -> Result<(), Error> {
// //
Ok(()) 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(())
}