Improve type::is::record() method (#2736)

This commit is contained in:
Micha de Vries 2023-09-22 21:44:28 +02:00 committed by GitHub
parent fba24969b6
commit a0072cf133
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 3 deletions

View file

@ -201,8 +201,11 @@ pub mod is {
Ok(matches!(arg, Value::Geometry(Geometry::Polygon(_))).into()) Ok(matches!(arg, Value::Geometry(Geometry::Polygon(_))).into())
} }
pub fn record((arg,): (Value,)) -> Result<Value, Error> { pub fn record((arg, table): (Value, Option<String>)) -> Result<Value, Error> {
Ok(arg.is_record().into()) Ok(match table {
Some(tb) => arg.is_record_of_table(tb).into(),
None => arg.is_record().into(),
})
} }
pub fn string((arg,): (Value,)) -> Result<Value, Error> { pub fn string((arg,): (Value,)) -> Result<Value, Error> {

View file

@ -922,6 +922,17 @@ impl Value {
matches!(self, Value::Thing(_)) 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 /// Check if this Value is a Geometry
pub fn is_geometry(&self) -> bool { pub fn is_geometry(&self) -> bool {
matches!(self, Value::Geometry(_)) matches!(self, Value::Geometry(_))

View file

@ -5117,11 +5117,21 @@ async fn function_type_is_record() -> Result<(), Error> {
let sql = r#" let sql = r#"
RETURN type::is::record(person:john); RETURN type::is::record(person:john);
RETURN type::is::record("123"); 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 dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test"); let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?; 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 tmp = res.remove(0).result?;
let val = Value::from(true); let val = Value::from(true);