superposition_core 0.117.1

Core native library for Superposition FFI bindings
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
//! Resolution benchmark for the config evaluation hot path.
//!
//! This benchmark reproduces the shape of the workload reported from
//! production-scale data (hundreds of thousands of context override rules,
//! a handful of default configs, ~18 regular dimensions, and derived local
//! cohorts) and measures a single "resolve all config" call — the same
//! operation the OpenFeature provider performs on every flag evaluation via
//! `superposition_core::eval_config`.
//!
//! Two variants are compared:
//!   * `optimized_borrowed`     — the current implementation, which resolves
//!     directly against the borrowed context/override set.
//!   * `pre_optimization_cloned` — reproduces the pre-optimization overhead,
//!     where every resolve deep-cloned the entire context set (once to read
//!     the dimensions, once more to build a throwaway `Config`).
//!
//! Run with:
//!     cargo bench -p superposition_core --bench resolve
//!
//! The dataset size defaults to the production figure (468,006 contexts) and
//! can be overridden for quicker local runs:
//!     BENCH_CONTEXTS=50000 cargo bench -p superposition_core --bench resolve

use std::cell::Cell;
use std::collections::HashMap;
use std::time::{Duration, Instant};

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use serde_json::{json, Map, Value};
use superposition_core::{eval, eval_config, Config, MergeStrategy};
use superposition_types::{
    database::models::cac::{DependencyGraph, DimensionType},
    Cac, Condition, Context, DimensionInfo, ExtendedMap, OverrideWithKeys, Overrides,
};

/// Number of regular dimensions in the synthetic dataset (mirrors the reported data).
const NUM_DIMENSIONS: usize = 18;
/// Default number of contexts / override rules (reported production figure).
const DEFAULT_NUM_CONTEXTS: usize = 468_006;
/// Number of sampled query contexts to resolve against.
const NUM_QUERIES: usize = 100;
/// Per-dimension value cardinality; controls how many contexts a query matches.
const DIMENSION_CARDINALITY: usize = 12;
/// The (small) set of default config keys.
const DEFAULT_CONFIG_KEYS: [&str; 3] = ["config.alpha", "config.beta", "config.gamma"];

/// Tiny deterministic xorshift64 PRNG so the dataset is fully reproducible
/// without pulling in an external `rand` dependency.
struct Rng(u64);

impl Rng {
    fn new(seed: u64) -> Self {
        Rng(seed)
    }

    fn next_u64(&mut self) -> u64 {
        let mut x = self.0;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        self.0 = x;
        x
    }

    fn below(&mut self, n: usize) -> usize {
        (self.next_u64() % n as u64) as usize
    }
}

fn root_dimension(dim: usize) -> String {
    format!("d{dim}")
}

fn cohort_dimension(dim: usize) -> String {
    format!("lc{dim}")
}

fn root_value(val: usize) -> String {
    format!("v{val}")
}

fn cohort_value(val: usize) -> String {
    format!("c{val}")
}

fn build_dimensions() -> HashMap<String, DimensionInfo> {
    let mut dimensions = HashMap::with_capacity(NUM_DIMENSIONS * 2);

    for dim in 0..NUM_DIMENSIONS {
        let root = root_dimension(dim);
        let cohort = cohort_dimension(dim);

        dimensions.insert(
            root.clone(),
            DimensionInfo {
                schema: ExtendedMap::from(Map::new()),
                position: dim as i32,
                dimension_type: DimensionType::Regular {},
                dependency_graph: DependencyGraph(HashMap::from([
                    (root.clone(), vec![cohort.clone()]),
                    (cohort.clone(), Vec::new()),
                ])),
                value_compute_function_name: None,
                description: String::new(),
            },
        );

        let mut definitions = Map::new();
        let mut enum_values = Vec::with_capacity(DIMENSION_CARDINALITY + 1);
        for val in 0..DIMENSION_CARDINALITY {
            let cohort_val = cohort_value(val);
            enum_values.push(Value::String(cohort_val.clone()));
            definitions.insert(
                cohort_val,
                json!({
                    "==": [
                        { "var": root },
                        root_value(val)
                    ]
                }),
            );
        }
        enum_values.push(Value::String("otherwise".to_string()));

        let mut schema = Map::new();
        schema.insert("type".to_string(), json!("string"));
        schema.insert("enum".to_string(), Value::Array(enum_values));
        schema.insert("definitions".to_string(), Value::Object(definitions));

        dimensions.insert(
            cohort.clone(),
            DimensionInfo {
                schema: ExtendedMap::from(schema),
                position: (NUM_DIMENSIONS + dim) as i32,
                dimension_type: DimensionType::LocalCohort(root),
                dependency_graph: DependencyGraph(HashMap::from([(cohort, Vec::new())])),
                value_compute_function_name: None,
                description: String::new(),
            },
        );
    }

    dimensions
}

fn query_from_condition(condition: &Condition) -> Map<String, Value> {
    condition
        .iter()
        .map(|(key, value)| {
            if let Some(dim) = key.strip_prefix("lc") {
                let root_key = format!("d{dim}");
                let root_val = value
                    .as_str()
                    .and_then(|v| v.strip_prefix('c'))
                    .map(|v| Value::String(format!("v{v}")))
                    .unwrap_or_else(|| value.clone());
                (root_key, root_val)
            } else {
                (key.clone(), value.clone())
            }
        })
        .collect()
}

#[derive(Clone)]
struct Dataset {
    config: Config,
    queries: Vec<Map<String, Value>>,
}

fn build_dataset(num_contexts: usize) -> Dataset {
    let mut rng = Rng::new(0x9E37_79B9_7F4A_7C15);

    let default_config: ExtendedMap = DEFAULT_CONFIG_KEYS
        .iter()
        .map(|k| (k.to_string(), json!("default")))
        .collect();

    let mut contexts = Vec::with_capacity(num_contexts);
    let mut overrides = HashMap::with_capacity(num_contexts);

    for i in 0..num_contexts {
        // Each context conditions on 1..=3 dimensions with concrete values.
        let num_dims = 1 + rng.below(3);
        let start = rng.below(NUM_DIMENSIONS);
        let mut cond = Map::new();
        for j in 0..num_dims {
            let dim = (start + j) % NUM_DIMENSIONS;
            let val = rng.below(DIMENSION_CARDINALITY);
            let (key, value) = if (i + j) % 3 == 0 {
                (cohort_dimension(dim), cohort_value(val))
            } else {
                (root_dimension(dim), root_value(val))
            };
            cond.insert(key, json!(value));
        }
        let condition = Cac::<Condition>::try_from(cond)
            .expect("non-empty condition")
            .into_inner();

        let override_key = format!("o{i}");
        let target = DEFAULT_CONFIG_KEYS[i % DEFAULT_CONFIG_KEYS.len()];
        let mut override_map = Map::new();
        override_map.insert(target.to_string(), json!(format!("override_{i}")));
        let override_value = Cac::<Overrides>::try_from(override_map)
            .expect("non-empty override")
            .into_inner();
        overrides.insert(override_key.clone(), override_value);

        contexts.push(Context {
            id: format!("ctx_{i}"),
            condition,
            priority: (i % 100) as i32,
            weight: 1,
            override_with_keys: OverrideWithKeys::new(override_key),
        });
    }

    let mut queries = Vec::with_capacity(NUM_QUERIES);
    for _ in 0..NUM_QUERIES {
        let idx = rng.below(num_contexts);
        queries.push(query_from_condition(&contexts[idx].condition));
    }

    let config = Config {
        default_configs: default_config,
        contexts,
        overrides,
        dimensions: build_dimensions(),
    };
    Dataset { config, queries }
}

/// Current implementation: `eval` borrows the contexts, overrides and
/// dimensions, so a caller that keeps its dataset around only hands over a copy
/// of the default config on every resolve.
fn resolve_optimized_borrowed(
    config: &Config,
    query: Map<String, Value>,
) -> Map<String, Value> {
    eval(
        config.default_configs.clone(),
        &config.contexts,
        &config.overrides,
        &config.dimensions,
        query,
        MergeStrategy::MERGE,
        None,
        None,
    )
}

fn resolve_optimized_owned(
    config: Config,
    query: Map<String, Value>,
) -> Map<String, Value> {
    eval_config(config, query, MergeStrategy::MERGE, None, None)
}

/// Pre-optimization behavior: every resolve deep-cloned the full context set
/// twice — once while extracting dimensions, once while building a throwaway
/// `Config` for (unused) prefix filtering.
fn resolve_pre_optimization(
    ds: &Dataset,
    query: &Map<String, Value>,
) -> Map<String, Value> {
    // (1) Old `get_dimensions_info`: cloned the whole Config just to read
    //     `.dimensions`.
    let full = ds.config.clone();
    let _dimensions = full.dimensions.clone();

    // (2) Old `eval_config`: cloned contexts + overrides into a temporary
    //     Config before resolving.
    let contexts = ds.config.contexts.clone();
    let overrides = ds.config.overrides.clone();
    pre_optimization::eval_config(
        full.default_configs.into_inner(),
        &contexts,
        &overrides,
        &ds.config.dimensions,
        query,
        MergeStrategy::MERGE,
        None,
        None,
    )
    .expect("resolve")
}

fn bench_resolve(c: &mut Criterion) {
    let num_contexts = std::env::var("BENCH_CONTEXTS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_NUM_CONTEXTS);

    let start = Instant::now();
    let ds = build_dataset(num_contexts);
    eprintln!(
        "Built dataset: {} contexts, {} regular dimensions, {} local cohorts, {} queries in {:.2?}",
        num_contexts,
        NUM_DIMENSIONS,
        NUM_DIMENSIONS,
        ds.queries.len(),
        start.elapsed()
    );

    let mut group = c.benchmark_group("resolve_all_config");
    // One iteration == one full resolve of one query context.
    group.sample_size(10);
    group.warm_up_time(Duration::from_secs(1));
    group.measurement_time(Duration::from_secs(15));

    group.bench_function("optimized_borrowed", |b| {
        let counter = Cell::new(0usize);
        b.iter(|| {
            let q = &ds.queries[counter.get() % ds.queries.len()];
            counter.set(counter.get() + 1);
            black_box(resolve_optimized_borrowed(&ds.config, q.clone()))
        })
    });

    group.bench_function("optimization_owned", |b| {
        let counter = Cell::new(0usize);
        b.iter(|| {
            let q = &ds.queries[counter.get() % ds.queries.len()];
            counter.set(counter.get() + 1);
            black_box(resolve_optimized_owned(ds.config.clone(), q.clone()))
        })
    });

    group.bench_function("pre_optimization_cloned", |b| {
        let counter = Cell::new(0usize);
        b.iter(|| {
            let q = &ds.queries[counter.get() % ds.queries.len()];
            counter.set(counter.get() + 1);
            black_box(resolve_pre_optimization(&ds, q))
        })
    });

    group.finish();
}

criterion_group!(benches, bench_resolve);
criterion_main!(benches);

mod pre_optimization {
    use std::collections::HashMap;

    use serde_json::{Map, Value};
    pub use superposition_types::api::config::MergeStrategy;
    use superposition_types::{
        database::models::cac::{DependencyGraph, DimensionType},
        Config, ConfigFilter, Context, DimensionInfo, Overrides, PrefixList,
    };

    #[inline]
    fn apply_logic(
        condition: &Map<String, Value>,
        context: &Map<String, Value>,
        partial: bool,
    ) -> bool {
        for (dimension, value) in condition {
            if let Some(context_value) = context.get(dimension) {
                if dimension == "variantIds" {
                    if let Value::Array(ref context_values) = context_value {
                        if !context_values.contains(value) {
                            return false;
                        }
                    } else {
                        return false;
                    }
                } else if *context_value != *value {
                    return false;
                }
            } else if partial {
                continue;
            } else {
                return false;
            }
        }
        true
    }

    /// Core context application logic - checks if all dimensions in condition are satisfied by context
    /// Only exact matches are considered valid, except for "variantIds" dimension where containment is checked
    /// Returns true if condition is satisfied by context, false otherwise
    pub fn apply(condition: &Map<String, Value>, context: &Map<String, Value>) -> bool {
        apply_logic(condition, context, false)
    }

    fn _evaluate_local_cohort_dimension(
        cohort_based_on: &str,
        cohort_based_on_value: &Value,
        schema: &Map<String, Value>,
    ) -> Option<String> {
        let definitions_object = schema.get("definitions")?.as_object()?;

        // Get the array of cohort names from the "enum" field and remove "otherwise"
        let cohort_enums = schema
            .get("enum")?
            .as_array()?
            .iter()
            .filter_map(|v| v.as_str())
            .filter(|s| *s != "otherwise")
            .collect::<Vec<_>>();

        for cohort_option in cohort_enums {
            let jsonlogic = definitions_object.get(cohort_option)?;
            // Find the first matching cohort definition
            let evaluation_data =
                serde_json::json!({cohort_based_on: cohort_based_on_value});
            if jsonlogic::apply(jsonlogic, &evaluation_data) == Ok(Value::Bool(true)) {
                return Some(cohort_option.to_string());
            }
        }

        None
    }

    fn evaluate_local_cohort_dimension(
        cohort_based_on: &str,
        cohort_based_on_value: &Value,
        schema: &Map<String, Value>,
    ) -> String {
        _evaluate_local_cohort_dimension(cohort_based_on, cohort_based_on_value, schema)
            .unwrap_or_else(|| "otherwise".to_string())
    }

    /// Evaluates local cohort dependencies in a depth-first manner
    fn evaluate_local_cohorts_dependency(
        dimension: &str,
        value: &Value,
        dependency_graph: &DependencyGraph,
        dimensions: &HashMap<String, DimensionInfo>,
        modified_context: &mut Map<String, Value>,
        query_data: &Map<String, Value>,
    ) {
        let mut stack = dependency_graph
            .get(dimension)
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|d| (d, dimension.to_string(), value.clone()))
            .collect::<Vec<_>>();

        // Depth-first traversal of dependencies
        while let Some((cohort_dimension, based_on, based_on_val)) = stack.pop() {
            if let Some(dimension_info) = dimensions.get(&cohort_dimension) {
                let mut cohort_val = None;
                match &dimension_info.dimension_type {
                    DimensionType::LocalCohort(_) => {
                        let cohort_value =
                            Value::String(evaluate_local_cohort_dimension(
                                &based_on,
                                &based_on_val,
                                &dimension_info.schema,
                            ));
                        modified_context
                            .insert(cohort_dimension.clone(), cohort_value.clone());
                        cohort_val = Some(cohort_value);
                    }
                    _ => {
                        if let Some(value) = query_data.get(&cohort_dimension) {
                            modified_context
                                .insert(cohort_dimension.clone(), value.clone());
                            cohort_val = Some(value.clone());
                        }
                    }
                }

                if let Some(cohort_val) = cohort_val {
                    stack.extend(
                        dimension_info
                            .dependency_graph
                            .get(&cohort_dimension)
                            .cloned()
                            .unwrap_or_default()
                            .into_iter()
                            .map(|d| (d, cohort_dimension.clone(), cohort_val.clone()))
                            .collect::<Vec<_>>(),
                    );
                }
            }
        }
    }

    fn _evaluate_local_cohorts(
        dimensions: &HashMap<String, DimensionInfo>,
        query_data: &Map<String, Value>,
        skip_unresolved: bool,
    ) -> Map<String, Value> {
        if dimensions.is_empty() {
            return query_data.clone();
        }

        let mut modified_context = Map::new();

        // Start from dimensions that are closest to root in each tree
        for dimension_key in dimensions_to_start_from(dimensions, query_data) {
            if let Some(value) = query_data.get(&dimension_key) {
                if let Some(dimension_info) = dimensions.get(&dimension_key) {
                    modified_context.insert(dimension_key.to_string(), value.clone());
                    evaluate_local_cohorts_dependency(
                        &dimension_key,
                        value,
                        &dimension_info.dependency_graph,
                        dimensions,
                        &mut modified_context,
                        query_data,
                    );
                }
            }
        }

        if skip_unresolved {
            return modified_context;
        }

        // For any local cohort dimension not yet set, set it to "otherwise"
        for dimension_key in dimensions.keys() {
            if let Some(dimension_info) = dimensions.get(dimension_key) {
                if matches!(dimension_info.dimension_type, DimensionType::LocalCohort(_))
                    && !modified_context.contains_key(dimension_key)
                {
                    modified_context.insert(
                        dimension_key.to_string(),
                        Value::String("otherwise".to_string()),
                    );
                }
            }
        }

        modified_context
    }

    /// Evaluates all local cohort dimensions based on the provided query data and dimension definitions
    /// First all local cohorts which are computable from the query data are evaluated, then any remaining local cohorts are set to "otherwise"
    /// Computation starts from such a point, such that dependencies can be resolved in a depth-first manner
    ///
    /// Values of regular and remote cohort dimensions in query_data are retained as is.
    /// Returned value, might have a different value for local cohort dimensions based on its based on dimensions,
    /// if the value provided for the local cohort was incorrect in the query data.
    pub fn evaluate_local_cohorts(
        dimensions: &HashMap<String, DimensionInfo>,
        query_data: &Map<String, Value>,
    ) -> Map<String, Value> {
        _evaluate_local_cohorts(dimensions, query_data, false)
    }

    /// Identifies starting dimensions for evaluation based on query data and dimension definitions
    /// For each tree in the dependency graph, picks the node closest to root from query_data for each branch of the tree.
    /// If nothing is found and a local cohort is encountered, picks that local cohort as start point from that branch.
    pub fn dimensions_to_start_from(
        dimensions: &HashMap<String, DimensionInfo>,
        query_data: &Map<String, Value>,
    ) -> Vec<String> {
        let mut start_dimensions = Vec::new();

        let regular_dimensions = dimensions
            .iter()
            .filter(|(_, data)| matches!(data.dimension_type, DimensionType::Regular {}))
            .map(|(dim_name, _)| dim_name.clone())
            .collect::<Vec<String>>();

        for root_dimension in regular_dimensions {
            let dependency_graph = &dimensions
                .get(&root_dimension)
                .map(|data| data.dependency_graph.clone())
                .unwrap_or_default();

            let mut stack = vec![root_dimension];

            while let Some(current_dimension) = stack.pop() {
                if query_data.contains_key(&current_dimension) {
                    start_dimensions.push(current_dimension);
                    continue;
                }

                if let Some(data) = dimensions.get(&current_dimension) {
                    if matches!(data.dimension_type, DimensionType::LocalCohort(_)) {
                        start_dimensions.push(current_dimension);
                        continue;
                    }
                }

                stack.extend(
                    dependency_graph
                        .get(&current_dimension)
                        .cloned()
                        .unwrap_or_default(),
                );
            }
        }

        start_dimensions
    }

    #[allow(clippy::too_many_arguments)]
    pub fn eval_config(
        default_config: Map<String, Value>,
        contexts: &[Context],
        overrides: &HashMap<String, Overrides>,
        dimensions: &HashMap<String, DimensionInfo>,
        query_data: &Map<String, Value>,
        merge_strategy: MergeStrategy,
        filter_prefixes: Option<Vec<String>>,
        filter_exclude_prefixes: Option<Vec<String>>,
    ) -> Result<Map<String, Value>, String> {
        // Local cohort evaluation only reads `dimensions`, which prefix filtering
        // leaves untouched, so it is safe to compute once regardless of the path.
        let modified_query_data = evaluate_local_cohorts(dimensions, query_data);

        let filter_prefixes = filter_prefixes.filter(|p| !p.is_empty());
        let filter_exclude_prefixes = filter_exclude_prefixes.filter(|p| !p.is_empty());

        // Fast path: no prefix filtering. Resolve directly against the borrowed
        // contexts/overrides instead of deep-cloning the entire context set (which
        // can be hundreds of thousands of entries) into a temporary `Config`.
        if filter_prefixes.is_none() && filter_exclude_prefixes.is_none() {
            let overrides_map = get_overrides(
                &modified_query_data,
                contexts,
                overrides,
                &merge_strategy,
                None,
            )?;

            let mut result_config = default_config;
            merge_overrides_on_default_config(
                &mut result_config,
                overrides_map,
                &merge_strategy,
            );
            return Ok(result_config);
        }

        // Slow path: prefix filtering needs an owned, filtered `Config`.
        let config = Config {
            default_configs: default_config.into(),
            contexts: contexts.to_vec(),
            overrides: overrides.clone(),
            dimensions: dimensions.clone(),
        }
        .filter_by_prefix(
            &PrefixList::from(filter_prefixes),
            &PrefixList::from(filter_exclude_prefixes),
        );

        let overrides_map: Map<String, Value> = get_overrides(
            &modified_query_data,
            &config.contexts,
            &config.overrides,
            &merge_strategy,
            None,
        )?;

        // Apply overrides to default config
        let mut result_config = config.default_configs;
        merge_overrides_on_default_config(
            &mut result_config,
            overrides_map,
            &merge_strategy,
        );

        Ok(result_config.into_inner())
    }

    pub fn merge(doc: &mut Value, patch: &Value) {
        if !patch.is_object() {
            *doc = patch.clone();
            return;
        }

        if !doc.is_object() {
            *doc = Value::Object(Map::new());
        }

        let map = doc.as_object_mut().unwrap();
        for (key, value) in patch.as_object().unwrap() {
            merge(map.entry(key.as_str()).or_insert(Value::Null), value);
        }
    }

    fn get_overrides(
        query_data: &Map<String, Value>,
        contexts: &[Context],
        overrides: &HashMap<String, Overrides>,
        merge_strategy: &MergeStrategy,
        mut on_override_select: Option<&mut dyn FnMut(Context)>,
    ) -> Result<Map<String, Value>, String> {
        let mut required_overrides = Map::new();

        for context in contexts {
            if !apply(&context.condition, query_data) {
                continue;
            }

            let override_key = context.override_with_keys.get_key();
            let Some(overriden_value) = overrides.get(override_key) else {
                continue;
            };

            match merge_strategy {
                MergeStrategy::REPLACE => {
                    for (key, value) in overriden_value.iter() {
                        required_overrides.insert(key.clone(), value.clone());
                    }
                }
                MergeStrategy::MERGE => {
                    for (key, value) in overriden_value.iter() {
                        merge(
                            required_overrides
                                .entry(key.as_str())
                                .or_insert(Value::Null),
                            value,
                        );
                    }
                }
            }
            // Only pay for a `Context` clone when a caller actually consumes it; the
            // borrow keeps the common (callback-less) resolution path allocation-free.
            if let Some(ref mut func) = on_override_select {
                func(context.clone());
            }
        }

        Ok(required_overrides)
    }

    fn merge_overrides_on_default_config(
        default_config: &mut Map<String, Value>,
        overrides: Map<String, Value>,
        merge_strategy: &MergeStrategy,
    ) {
        overrides.into_iter().for_each(|(key, val)| {
            if let Some(og_val) = default_config.get_mut(&key) {
                match merge_strategy {
                    MergeStrategy::REPLACE => {
                        let _ = default_config.insert(key.clone(), val.clone());
                    }
                    MergeStrategy::MERGE => merge(og_val, &val),
                }
            } else {
                log::error!("Config: found non-default_config key: {key} in overrides");
            }
        })
    }
}