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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! All types related to merging one [`Kind`] into another.
use super::Field;
use std::ops::BitOr;
use super::{Collection, Kind};
impl Kind {
/// Merge `other` into `self`, using the provided `Strategy`.
pub fn merge(&mut self, other: Self, strategy: Strategy) {
self.merge_keep(other, strategy.collisions.is_shallow());
}
fn merge_primitives(&mut self, other: &Self) {
self.bytes = self.bytes.or(other.bytes);
self.integer = self.integer.or(other.integer);
self.float = self.float.or(other.float);
self.boolean = self.boolean.or(other.boolean);
self.timestamp = self.timestamp.or(other.timestamp);
self.regex = self.regex.or(other.regex);
self.null = self.null.or(other.null);
self.undefined = self.undefined.or(other.undefined);
}
fn merge_objects(&mut self, other: Option<Collection<Field>>, overwrite: bool) {
match (self.object.as_mut(), other) {
(None, rhs @ Some(_)) => self.object = rhs,
(Some(lhs), Some(rhs)) => lhs.merge(rhs, overwrite),
_ => {}
}
}
/// Returns the union of self and other.
#[must_use]
pub fn union(&self, other: Self) -> Self {
let mut kind = self.clone();
kind.merge_keep(other, false);
kind
}
/// Merge `other` into `self`, optionally overwriting on conflicts.
// deprecated
pub fn merge_keep(&mut self, other: Self, overwrite: bool) {
self.merge_primitives(&other);
self.merge_objects(other.object, overwrite);
match (self.array.as_mut(), other.array) {
(None, Some(rhs)) => self.array = Some(rhs),
(Some(lhs), Some(rhs)) => lhs.merge(rhs, overwrite),
_ => {}
}
}
}
/// The strategy to apply to the merge between two `Kind`s.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Strategy {
/// How to deal with types in a collection if both specify a type.
/// This only applies to types in a collection (not the root type)
pub collisions: CollisionStrategy,
}
/// The choice of "depth" to apply when merging two [`Kind`]s.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CollisionStrategy {
/// Use the 2nd value. This should no longer be used, and will be removed in the future.
/// Try using `Kind::insert` or a custom function instead.
Overwrite,
/// Merge both together
Union,
}
impl CollisionStrategy {
/// Check if `shallow` strategy is enabled.
#[must_use]
pub const fn is_shallow(self) -> bool {
matches!(self, Self::Overwrite)
}
/// Check if `deep` strategy is enabled.
#[must_use]
pub const fn is_deep(self) -> bool {
matches!(self, Self::Union)
}
}
impl BitOr for Kind {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
self.union(rhs)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::{BTreeMap, HashMap};
#[test]
#[allow(clippy::too_many_lines)]
fn test_merge() {
struct TestCase {
this: Kind,
other: Kind,
strategy: Strategy,
merged: Kind,
}
for (
title,
TestCase {
mut this,
other,
strategy,
merged,
},
) in HashMap::from([
(
"object field with unknown",
TestCase {
this: Kind::object(Collection::any()),
other: Kind::object(BTreeMap::from([("x".into(), Kind::integer())])),
strategy: Strategy {
collisions: CollisionStrategy::Union,
},
merged: {
let mut collection =
Collection::from(BTreeMap::from([("x".into(), Kind::any())]));
collection.set_unknown(Kind::any());
Kind::object(collection)
},
},
),
(
"primitives shallow",
TestCase {
this: Kind::bytes(),
other: Kind::integer(),
strategy: Strategy {
collisions: CollisionStrategy::Overwrite,
},
merged: Kind::bytes().or_integer(),
},
),
(
"primitives deep",
TestCase {
this: Kind::bytes(),
other: Kind::integer(),
strategy: Strategy {
collisions: CollisionStrategy::Union,
},
merged: Kind::bytes().or_integer(),
},
),
(
"mixed unknown shallow",
TestCase {
this: Kind::bytes().or_object(Collection::from_unknown(Kind::integer())),
other: Kind::bytes().or_object(Collection::from_unknown(Kind::bytes())),
strategy: Strategy {
collisions: CollisionStrategy::Overwrite,
},
merged: Kind::bytes()
.or_object(Collection::from_unknown(Kind::integer().or_bytes())),
},
),
(
"mixed unknown deep",
TestCase {
this: Kind::bytes().or_object(Collection::from_unknown(Kind::integer())),
other: Kind::bytes().or_object(Collection::from_unknown(Kind::bytes())),
strategy: Strategy {
collisions: CollisionStrategy::Union,
},
merged: Kind::bytes()
.or_object(Collection::from_unknown(Kind::integer().or_bytes())),
},
),
(
"mixed known shallow",
TestCase {
this: Kind::bytes().or_object(BTreeMap::from([
(
"foo".into(),
Kind::object(BTreeMap::from([
("qux".into(), Kind::bytes()),
("quux".into(), Kind::boolean()),
("this".into(), Kind::timestamp()),
])),
),
("bar".into(), Kind::integer()),
])),
other: Kind::integer().or_object(BTreeMap::from([
(
"foo".into(),
Kind::object(BTreeMap::from([
("qux".into(), Kind::integer()),
("quux".into(), Kind::regex()),
("that".into(), Kind::null()),
])),
),
("baz".into(), Kind::boolean()),
])),
strategy: Strategy {
collisions: CollisionStrategy::Overwrite,
},
merged: Kind::bytes().or_integer().or_object(BTreeMap::from([
(
"foo".into(),
Kind::object(BTreeMap::from([
("qux".into(), Kind::integer()),
("quux".into(), Kind::regex()),
("that".into(), Kind::null()),
])),
),
("bar".into(), Kind::integer()),
("baz".into(), Kind::boolean()),
])),
},
),
(
"mixed known deep",
TestCase {
this: Kind::bytes().or_object(BTreeMap::from([
(
"foo".into(),
Kind::object(BTreeMap::from([
("qux".into(), Kind::bytes()),
("quux".into(), Kind::boolean()),
("this".into(), Kind::timestamp()),
])),
),
("bar".into(), Kind::integer()),
])),
other: Kind::integer().or_object(BTreeMap::from([
(
"foo".into(),
Kind::object(BTreeMap::from([
("qux".into(), Kind::integer()),
("quux".into(), Kind::regex()),
("that".into(), Kind::null()),
])),
),
("baz".into(), Kind::boolean()),
])),
strategy: Strategy {
collisions: CollisionStrategy::Union,
},
merged: Kind::bytes().or_integer().or_object(BTreeMap::from([
(
"foo".into(),
Kind::object(BTreeMap::from([
("qux".into(), Kind::bytes().or_integer()),
("quux".into(), Kind::boolean().or_regex()),
("this".into(), Kind::timestamp().or_undefined()),
("that".into(), Kind::null().or_undefined()),
])),
),
("bar".into(), Kind::integer().or_undefined()),
("baz".into(), Kind::boolean().or_undefined()),
])),
},
),
]) {
this.merge(other, strategy);
assert_eq!(this, merged, "{title}");
}
}
}