2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-20 23:00:57 +00:00
|
|
|
use crate::dbs::Options;
|
2022-02-15 01:00:30 +00:00
|
|
|
use crate::dbs::Transaction;
|
2022-01-20 23:00:57 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub async fn merge(
|
|
|
|
&mut self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-07-07 09:58:35 +00:00
|
|
|
val: Value,
|
2022-01-20 23:00:57 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-07-07 09:58:35 +00:00
|
|
|
match val {
|
2022-09-29 10:09:20 +00:00
|
|
|
v if v.is_object() => {
|
2022-10-20 14:32:25 +00:00
|
|
|
for k in v.every(false, false).iter() {
|
2022-09-29 10:09:20 +00:00
|
|
|
match v.get(ctx, opt, txn, &k.0).await? {
|
|
|
|
Value::None => self.del(ctx, opt, txn, &k.0).await?,
|
|
|
|
v => self.set(ctx, opt, txn, &k.0, v).await?,
|
|
|
|
}
|
2022-01-20 23:00:57 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-13 23:39:10 +00:00
|
|
|
_ => Ok(()),
|
2022-01-20 23:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 14:32:25 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::dbs::test::mock;
|
|
|
|
use crate::sql::test::Parse;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn merge_none() {
|
|
|
|
let (ctx, opt, txn) = mock().await;
|
|
|
|
let mut res = Value::parse(
|
|
|
|
"{
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
initials: 'TMH',
|
|
|
|
},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
let mrg = Value::None;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
initials: 'TMH',
|
|
|
|
},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
res.merge(&ctx, &opt, &txn, mrg).await.unwrap();
|
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn merge_basic() {
|
|
|
|
let (ctx, opt, txn) = mock().await;
|
|
|
|
let mut res = Value::parse(
|
|
|
|
"{
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
initials: 'TMH',
|
|
|
|
},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
let mrg = Value::parse(
|
|
|
|
"{
|
|
|
|
name: {
|
|
|
|
title: 'Mr',
|
|
|
|
initials: NONE,
|
|
|
|
},
|
|
|
|
tags: ['Rust', 'Golang', 'JavaScript'],
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
name: {
|
|
|
|
title: 'Mr',
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
},
|
|
|
|
tags: ['Rust', 'Golang', 'JavaScript'],
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
res.merge(&ctx, &opt, &txn, mrg).await.unwrap();
|
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
}
|