Skip to main content

surrealdb_expr/val/value/
replace.rs

1use anyhow::{Result, ensure};
2
3use crate::expr::Error;
4use crate::val::Value;
5
6impl Value {
7	pub fn replace(&mut self, val: Value) -> Result<()> {
8		// If this value is not an object, then error
9		ensure!(
10			val.is_object(),
11			Error::InvalidContent {
12				value: val,
13			}
14		);
15		// Otherwise replace the current value
16		*self = val;
17		Ok(())
18	}
19}
20
21#[cfg(test)]
22mod tests {
23
24	use crate::syn;
25
26	macro_rules! parse_val {
27		($input:expr) => {
28			crate::val::convert_public_value_to_internal(syn::value($input).unwrap())
29		};
30	}
31
32	#[tokio::test]
33	async fn replace() {
34		let mut val = parse_val!("{ test: { other: null, something: 123 } }");
35		let res = parse_val!("{ other: true }");
36		let obj = parse_val!("{ other: true }");
37		val.replace(obj).unwrap();
38		assert_eq!(res, val);
39	}
40}