From a0072cf133c768284ffca14f88e90997378d72ed Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Fri, 22 Sep 2023 21:44:28 +0200 Subject: [PATCH] Improve `type::is::record()` method (#2736) --- lib/src/fnc/type.rs | 7 +++++-- lib/src/sql/value/value.rs | 11 +++++++++++ lib/tests/function.rs | 12 +++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/src/fnc/type.rs b/lib/src/fnc/type.rs index 687bd574..f51cc4d3 100644 --- a/lib/src/fnc/type.rs +++ b/lib/src/fnc/type.rs @@ -201,8 +201,11 @@ pub mod is { Ok(matches!(arg, Value::Geometry(Geometry::Polygon(_))).into()) } - pub fn record((arg,): (Value,)) -> Result { - Ok(arg.is_record().into()) + pub fn record((arg, table): (Value, Option)) -> Result { + Ok(match table { + Some(tb) => arg.is_record_of_table(tb).into(), + None => arg.is_record().into(), + }) } pub fn string((arg,): (Value,)) -> Result { diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 114fcba0..96a61fe6 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -922,6 +922,17 @@ impl Value { matches!(self, Value::Thing(_)) } + /// Check if this Value is a Thing, and belongs to a certain table + pub fn is_record_of_table(&self, table: String) -> bool { + match self { + Value::Thing(Thing { + tb, + .. + }) => *tb == table, + _ => false, + } + } + /// Check if this Value is a Geometry pub fn is_geometry(&self) -> bool { matches!(self, Value::Geometry(_)) diff --git a/lib/tests/function.rs b/lib/tests/function.rs index b82c4e32..3ee93713 100644 --- a/lib/tests/function.rs +++ b/lib/tests/function.rs @@ -5117,11 +5117,21 @@ async fn function_type_is_record() -> Result<(), Error> { let sql = r#" RETURN type::is::record(person:john); RETURN type::is::record("123"); + RETURN type::is::record(person:john, 'person'); + RETURN type::is::record(person:john, 'user'); "#; let dbs = new_ds().await?; let ses = Session::owner().with_ns("test").with_db("test"); let res = &mut dbs.execute(sql, &ses, None).await?; - assert_eq!(res.len(), 2); + assert_eq!(res.len(), 4); + // + let tmp = res.remove(0).result?; + let val = Value::from(true); + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::from(false); + assert_eq!(tmp, val); // let tmp = res.remove(0).result?; let val = Value::from(true);