Merge Strategies

Advanced merging strategies for Confii.

This module provides different merge strategies for combining configurations, allowing fine-grained control over how values are merged.

class confii.merge_strategies.AdvancedConfigMerger(default_strategy: MergeStrategy = MergeStrategy.MERGE)[source]

Bases: object

Advanced configuration merger with strategy support.

This class provides configurable merge strategies for combining configurations with fine-grained control over merge behavior.

__init__(default_strategy: MergeStrategy = MergeStrategy.MERGE) None[source]

Initialize the advanced config merger.

Parameters:

default_strategy – Default merge strategy to use

merge(base: Dict[str, Any], new: Dict[str, Any], path: str = '') Dict[str, Any][source]

Merge two configurations using configured strategies.

Parameters:
  • base – Base configuration dictionary

  • new – New configuration dictionary to merge in

  • path – Current path prefix (for strategy lookup)

Returns:

Merged configuration dictionary

Example

>>> merger = AdvancedConfigMerger()
>>> base = {"database": {"host": "localhost"}, "app": {"debug": True}}
>>> new = {"database": {"port": 5432}}
>>> result = merger.merge(base, new)
>>> # Result: {"database": {"host": "localhost", "port": 5432}, "app": {"debug": True}}
set_strategy(key_path: str, strategy: MergeStrategy) None[source]

Set merge strategy for a specific key path.

Parameters:
  • key_path – Dot-separated key path (e.g., β€œdatabase”, β€œapp.debug”)

  • strategy – Merge strategy to use for this key

Example

>>> merger = AdvancedConfigMerger()
>>> merger.set_strategy("database", MergeStrategy.REPLACE)
>>> merger.set_strategy("app.debug", MergeStrategy.REPLACE)
class confii.merge_strategies.MergeStrategy(*values)[source]

Bases: Enum

Merge strategies for configuration values.

This enum defines the available strategies for merging configuration values when combining configurations from multiple sources.

REPLACE

Replace the base value entirely with the new value. Use this when you want new values to completely override existing ones.

MERGE

Deep merge nested dictionaries while preserving existing values. Use this for nested configurations where you want to combine values.

APPEND

Append new list items to the end of existing lists. Use this when you want to combine list values.

PREPEND

Prepend new list items to the beginning of existing lists. Use this when new list items should have priority.

INTERSECTION

Keep only keys that exist in both configurations. Use this to create a configuration with only common settings.

UNION

Keep all keys from both configurations, merging values for common keys. Use this to combine all available configuration options.

Example

>>> from confii.merge_strategies import (
...     MergeStrategy,
...     AdvancedConfigMerger,
... )
>>> merger = AdvancedConfigMerger(MergeStrategy.MERGE)
>>> merger.set_strategy("database", MergeStrategy.REPLACE)
>>> result = merger.merge(base_config, override_config)