2022-01-13 17:36:41 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Runtime;
|
2022-02-15 01:00:30 +00:00
|
|
|
use crate::dbs::Transaction;
|
2022-01-20 23:00:57 +00:00
|
|
|
use crate::err::Error;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::object::Object;
|
2022-02-22 19:05:58 +00:00
|
|
|
use crate::sql::part::Part;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
impl Value {
|
2022-01-14 08:12:56 +00:00
|
|
|
pub async fn object(
|
|
|
|
&mut self,
|
|
|
|
ctx: &Runtime,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-02-22 19:05:58 +00:00
|
|
|
path: &[Part],
|
2022-01-20 23:00:57 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-01-13 17:36:41 +00:00
|
|
|
let val = Value::from(Object::default());
|
2022-02-15 01:00:30 +00:00
|
|
|
self.set(ctx, opt, txn, path, val).await
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-20 23:00:57 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::dbs::test::mock;
|
2022-02-22 19:05:58 +00:00
|
|
|
use crate::sql::idiom::Idiom;
|
2022-01-20 23:00:57 +00:00
|
|
|
use crate::sql::test::Parse;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn object_none() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-20 23:00:57 +00:00
|
|
|
let idi = Idiom::default();
|
|
|
|
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("{}");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.object(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-20 23:00:57 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn object_path() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-20 23:00:57 +00:00
|
|
|
let idi = Idiom::parse("test");
|
|
|
|
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("{ test: {} }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.object(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-20 23:00:57 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
}
|