vector_config/schema/visitors/
merge.rs

1#![allow(clippy::borrowed_box)]
2
3use std::mem::discriminant;
4
5use serde_json::Value;
6use vector_config_common::schema::*;
7
8/// A type that can be merged with itself.
9pub trait Mergeable {
10    fn merge(&mut self, other: &Self);
11}
12
13impl Mergeable for SchemaObject {
14    fn merge(&mut self, other: &Self) {
15        // The logic is pretty straightforward: we merge `other` into `self`, with `self` having the
16        // higher precedence.
17        //
18        // Additionally, we only merge logical schema chunks: if the destination schema has object
19        // properties defined, and the source schema has some object properties that don't exist in
20        // the destination, they will be merged, but if there is any overlap, then the object
21        // properties in the destination would remain untouched. This merging logic applies to all
22        // map-based types.
23        //
24        // For standalone fields, such as title or description, the destination always has higher
25        // precedence. For optional fields, whichever version (destination or source) is present
26        // will win, except for when both are present, then the individual fields within the
27        // optional type will be merged according to the normal precedence rules.
28        merge_optional(&mut self.reference, other.reference.as_ref());
29        merge_schema_metadata(&mut self.metadata, other.metadata.as_ref());
30        merge_schema_instance_type(&mut self.instance_type, other.instance_type.as_ref());
31        merge_schema_format(&mut self.format, other.format.as_ref());
32        merge_schema_enum_values(&mut self.enum_values, other.enum_values.as_ref());
33        merge_schema_const_value(&mut self.const_value, other.const_value.as_ref());
34        merge_schema_subschemas(&mut self.subschemas, other.subschemas.as_ref());
35        merge_schema_number_validation(&mut self.number, other.number.as_ref());
36        merge_schema_string_validation(&mut self.string, other.string.as_ref());
37        merge_schema_array_validation(&mut self.array, other.array.as_ref());
38        merge_schema_object_validation(&mut self.object, other.object.as_ref());
39        merge_schema_extensions(&mut self.extensions, &other.extensions);
40    }
41}
42
43impl Mergeable for Value {
44    fn merge(&mut self, other: &Self) {
45        // We do a check here for ensuring both value discriminants are the same type. This is
46        // specific to `Value` but we should never really be merging identical keys together that
47        // have differing value types, as that is indicative of a weird overlap in keys between
48        // different schemas.
49        //
50        // We _may_ need to relax this in practice/in the future, but it's a solid invariant to
51        // enforce for the time being.
52        if discriminant(self) != discriminant(other) {
53            panic!("Tried to merge two `Value` types together with differing types!\n\nSelf: {self:?}\n\nOther: {other:?}");
54        }
55
56        match (self, other) {
57            // Maps get merged recursively.
58            (Value::Object(self_map), Value::Object(other_map)) => {
59                self_map.merge(other_map);
60            }
61            // Arrays get merged together indiscriminately.
62            (Value::Array(self_array), Value::Array(other_array)) => {
63                self_array.extend(other_array.iter().cloned());
64            }
65            // We don't merge any other value types together.
66            _ => {}
67        }
68    }
69}
70
71impl Mergeable for Schema {
72    fn merge(&mut self, other: &Self) {
73        match (self, other) {
74            // We don't merge schemas together if either of them is a boolean schema.
75            (Schema::Bool(_), _) | (_, Schema::Bool(_)) => {}
76            (Schema::Object(self_schema), Schema::Object(other_schema)) => {
77                self_schema.merge(other_schema);
78            }
79        }
80    }
81}
82
83impl Mergeable for serde_json::Map<String, Value> {
84    fn merge(&mut self, other: &Self) {
85        for (key, value) in other {
86            match self.get_mut(key) {
87                None => {
88                    self.insert(key.clone(), value.clone());
89                }
90                Some(existing) => existing.merge(value),
91            }
92        }
93    }
94}
95
96impl<K, V> Mergeable for Map<K, V>
97where
98    K: Clone + Eq + Ord,
99    V: Clone + Mergeable,
100{
101    fn merge(&mut self, other: &Self) {
102        for (key, value) in other {
103            match self.get_mut(key) {
104                None => {
105                    self.insert(key.clone(), value.clone());
106                }
107                Some(existing) => existing.merge(value),
108            }
109        }
110    }
111}
112
113fn merge_schema_metadata(destination: &mut Option<Box<Metadata>>, source: Option<&Box<Metadata>>) {
114    merge_optional_with(destination, source, |existing, new| {
115        merge_optional(&mut existing.id, new.id.as_ref());
116        merge_optional(&mut existing.title, new.title.as_ref());
117        merge_optional(&mut existing.description, new.description.as_ref());
118        merge_optional(&mut existing.default, new.default.as_ref());
119        merge_bool(&mut existing.deprecated, new.deprecated);
120        merge_bool(&mut existing.read_only, new.read_only);
121        merge_bool(&mut existing.write_only, new.write_only);
122        merge_collection(&mut existing.examples, &new.examples);
123    });
124}
125
126fn merge_schema_instance_type(
127    destination: &mut Option<SingleOrVec<InstanceType>>,
128    source: Option<&SingleOrVec<InstanceType>>,
129) {
130    merge_optional_with(destination, source, |existing, new| {
131        let mut deduped = existing.into_iter().chain(new).cloned().collect::<Vec<_>>();
132        deduped.dedup();
133
134        *existing = deduped.into();
135    });
136}
137
138fn merge_schema_format(destination: &mut Option<String>, source: Option<&String>) {
139    merge_optional(destination, source);
140}
141
142fn merge_schema_enum_values(destination: &mut Option<Vec<Value>>, source: Option<&Vec<Value>>) {
143    merge_optional_with(destination, source, merge_collection);
144}
145
146fn merge_schema_const_value(destination: &mut Option<Value>, source: Option<&Value>) {
147    merge_optional(destination, source);
148}
149
150fn merge_schema_subschemas(
151    destination: &mut Option<Box<SubschemaValidation>>,
152    source: Option<&Box<SubschemaValidation>>,
153) {
154    merge_optional_with(destination, source, |existing, new| {
155        merge_optional_with(&mut existing.all_of, new.all_of.as_ref(), merge_collection);
156        merge_optional_with(&mut existing.any_of, new.any_of.as_ref(), merge_collection);
157        merge_optional_with(&mut existing.one_of, new.one_of.as_ref(), merge_collection);
158        merge_optional(&mut existing.if_schema, new.if_schema.as_ref());
159        merge_optional(&mut existing.then_schema, new.then_schema.as_ref());
160        merge_optional(&mut existing.else_schema, new.else_schema.as_ref());
161        merge_optional(&mut existing.not, new.not.as_ref());
162    });
163}
164
165fn merge_schema_number_validation(
166    destination: &mut Option<Box<NumberValidation>>,
167    source: Option<&Box<NumberValidation>>,
168) {
169    merge_optional_with(destination, source, |existing, new| {
170        merge_optional(&mut existing.multiple_of, new.multiple_of.as_ref());
171        merge_optional(&mut existing.maximum, new.maximum.as_ref());
172        merge_optional(
173            &mut existing.exclusive_maximum,
174            new.exclusive_minimum.as_ref(),
175        );
176        merge_optional(&mut existing.minimum, new.minimum.as_ref());
177        merge_optional(
178            &mut existing.exclusive_minimum,
179            new.exclusive_minimum.as_ref(),
180        );
181    });
182}
183
184fn merge_schema_string_validation(
185    destination: &mut Option<Box<StringValidation>>,
186    source: Option<&Box<StringValidation>>,
187) {
188    merge_optional_with(destination, source, |existing, new| {
189        merge_optional(&mut existing.max_length, new.max_length.as_ref());
190        merge_optional(&mut existing.min_length, new.min_length.as_ref());
191        merge_optional(&mut existing.pattern, new.pattern.as_ref());
192    });
193}
194
195fn merge_schema_array_validation(
196    destination: &mut Option<Box<ArrayValidation>>,
197    source: Option<&Box<ArrayValidation>>,
198) {
199    merge_optional_with(destination, source, |existing, new| {
200        merge_optional_with(&mut existing.items, new.items.as_ref(), merge_collection);
201        merge_optional(
202            &mut existing.additional_items,
203            new.additional_items.as_ref(),
204        );
205        merge_optional(
206            &mut existing.unevaluated_items,
207            new.unevaluated_items.as_ref(),
208        );
209        merge_optional(&mut existing.max_items, new.max_items.as_ref());
210        merge_optional(&mut existing.min_items, new.min_items.as_ref());
211        merge_optional(&mut existing.unique_items, new.unique_items.as_ref());
212        merge_optional(&mut existing.contains, new.contains.as_ref());
213    });
214}
215
216fn merge_schema_object_validation(
217    destination: &mut Option<Box<ObjectValidation>>,
218    source: Option<&Box<ObjectValidation>>,
219) {
220    merge_optional_with(destination, source, |existing, new| {
221        merge_optional(&mut existing.max_properties, new.max_properties.as_ref());
222        merge_optional(&mut existing.min_properties, new.min_properties.as_ref());
223        merge_collection(&mut existing.required, &new.required);
224        merge_map(&mut existing.properties, &new.properties);
225        merge_map(&mut existing.pattern_properties, &new.pattern_properties);
226        merge_optional(
227            &mut existing.additional_properties,
228            new.additional_properties.as_ref(),
229        );
230        merge_optional(
231            &mut existing.unevaluated_properties,
232            new.unevaluated_properties.as_ref(),
233        );
234        merge_optional(&mut existing.property_names, new.property_names.as_ref());
235    });
236}
237
238fn merge_schema_extensions(destination: &mut Map<String, Value>, source: &Map<String, Value>) {
239    merge_map(destination, source);
240}
241
242fn merge_bool(destination: &mut bool, source: bool) {
243    // We only treat `true` as a merge-worthy value.
244    if source {
245        *destination = true;
246    }
247}
248
249fn merge_collection<'a, E, I, T>(destination: &mut E, source: I)
250where
251    E: Extend<T>,
252    I: IntoIterator<Item = &'a T>,
253    T: Clone + 'a,
254{
255    destination.extend(source.into_iter().cloned());
256}
257
258fn merge_map<K, V>(destination: &mut Map<K, V>, source: &Map<K, V>)
259where
260    K: Clone + Eq + Ord,
261    V: Clone + Mergeable,
262{
263    destination.merge(source);
264}
265
266fn merge_optional<T: Clone>(destination: &mut Option<T>, source: Option<&T>) {
267    merge_optional_with(destination, source, |_, _| {});
268}
269
270fn merge_optional_with<'a, T, F>(destination: &'a mut Option<T>, source: Option<&'a T>, f: F)
271where
272    T: Clone,
273    F: Fn(&'a mut T, &'a T),
274{
275    match destination {
276        // If the destination is empty, we use whatever we have in `source`. Otherwise, we leave
277        // `destination` as-is.
278        None => *destination = source.cloned(),
279        // If `destination` isn't empty, and neither is `source`, then pass them both to `f` to
280        // let it handle the actual merge logic.
281        Some(destination) => {
282            if let Some(source) = source {
283                f(destination, source);
284            }
285        }
286    }
287}