-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiff.rs
165 lines (146 loc) · 4.64 KB
/
diff.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
extern crate alloc;
use alloc::format;
use alloc::string::{String, ToString};
use core::ffi::c_int;
use sqlite::ResultCode;
use sqlite_nostd as sqlite;
use sqlite_nostd::{Connection, Context, Value};
use serde_json as json;
use crate::create_sqlite_text_fn;
use crate::error::SQLiteError;
fn powersync_diff_impl(
_ctx: *mut sqlite::context,
args: &[*mut sqlite::value],
) -> Result<String, SQLiteError> {
let data_old = args[0].text();
let data_new = args[1].text();
let ignore_removed = args.get(2).map_or(false, |v| v.int() != 0);
diff_objects_with_options(data_old, data_new, ignore_removed)
}
/// Returns a JSON object containing entries from [data_new] that are not present in [data_old].
///
/// When [ignore_removed_columns] is set, columns that are present in [data_old] but not in
/// [data_new] will not be present in the returned object. Otherwise, they will be set to `null`.
fn diff_objects_with_options(
data_old: &str,
data_new: &str,
ignore_removed_columns: bool,
) -> Result<String, SQLiteError> {
let v_new: json::Value = json::from_str(data_new)?;
let v_old: json::Value = json::from_str(data_old)?;
if let (json::Value::Object(mut left), json::Value::Object(mut right)) = (v_new, v_old) {
// Remove all null values
right.retain(|_, v| !v.is_null());
left.retain(|_, v| !v.is_null());
if right.len() == 0 {
// Simple case
return Ok(json::Value::Object(left).to_string());
}
// Add missing nulls to left
if !ignore_removed_columns {
for key in right.keys() {
if !left.contains_key(key) {
left.insert(key.clone(), json::Value::Null);
}
}
}
left.retain(|key, value| {
let r = right.get(key);
if let Some(r) = r {
// Check if value is different
value != r
} else {
// Value not present in right
true
}
});
Ok(json::Value::Object(left).to_string())
} else {
Err(SQLiteError::from(ResultCode::MISMATCH))
}
}
create_sqlite_text_fn!(powersync_diff, powersync_diff_impl, "powersync_diff");
pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
db.create_function_v2(
"powersync_diff",
2,
sqlite::UTF8 | sqlite::DETERMINISTIC,
None,
Some(powersync_diff),
None,
None,
None,
)?;
db.create_function_v2(
"powersync_diff",
3,
sqlite::UTF8 | sqlite::DETERMINISTIC,
None,
Some(powersync_diff),
None,
None,
None,
)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn diff_objects(data_old: &str, data_new: &str) -> Result<String, SQLiteError> {
diff_objects_with_options(data_old, data_new, false)
}
#[test]
fn basic_diff_test() {
assert_eq!(diff_objects("{}", "{}").unwrap(), "{}");
assert_eq!(diff_objects(r#"{"a": null}"#, "{}").unwrap(), "{}");
assert_eq!(diff_objects(r#"{}"#, r#"{"a": null}"#).unwrap(), "{}");
assert_eq!(
diff_objects(r#"{"b": 1}"#, r#"{"a": null, "b": 1}"#).unwrap(),
"{}"
);
assert_eq!(
diff_objects(r#"{"b": 1}"#, r#"{"a": null, "b": 2}"#).unwrap(),
r#"{"b":2}"#
);
assert_eq!(
diff_objects(r#"{"a": 0, "b": 1}"#, r#"{"a": null, "b": 2}"#).unwrap(),
r#"{"a":null,"b":2}"#
);
assert_eq!(
diff_objects(r#"{"a": 1}"#, r#"{"a": null}"#).unwrap(),
r#"{"a":null}"#
);
assert_eq!(
diff_objects(r#"{"a": 1}"#, r#"{}"#).unwrap(),
r#"{"a":null}"#
);
assert_eq!(
diff_objects(r#"{"a": 1}"#, r#"{"a": 2}"#).unwrap(),
r#"{"a":2}"#
);
assert_eq!(
diff_objects(r#"{"a": 1}"#, r#"{"a": "1"}"#).unwrap(),
r#"{"a":"1"}"#
);
assert_eq!(
diff_objects(r#"{"a": 1}"#, r#"{"a": 1.0}"#).unwrap(),
r#"{"a":1.0}"#
);
assert_eq!(
diff_objects(r#"{"a": 1.00}"#, r#"{"a": 1.0}"#).unwrap(),
r#"{}"#
);
assert_eq!(
diff_objects(r#"{}"#, r#"{"a": 1.0}"#).unwrap(),
r#"{"a":1.0}"#
);
assert_eq!(
diff_objects(r#"{}"#, r#"{"a": [1,2,3]}"#).unwrap(),
r#"{"a":[1,2,3]}"#
);
assert_eq!(
diff_objects(r#"{"a": 1}"#, r#"{"a": [1,2,3]}"#).unwrap(),
r#"{"a":[1,2,3]}"#
);
}
}