Merging
Merging
Coyaml merges sources in the order they are added. Later sources override earlier ones. Dictionaries are merged deeply; lists are replaced.
Multiple sources and order
"""Merging multiple sources: later sources override earlier ones; dicts are deep-merged.
Run:
PYTHONPATH=src uv run python examples/merging/30_multi_source_merge_order.py
"""
from __future__ import annotations
from coyaml import YSettings
from coyaml.sources.yaml import YamlFileSource
def main() -> None:
cfg = YSettings()
# Base config
cfg.add_source(YamlFileSource('tests/config/config.yaml'))
# Overlay with changes
cfg.add_source(
YamlFileSource('tests/config/extra.yaml')
) # this is a different shape; used here only to show precedence
# Resolve templates after all sources are added
cfg.resolve_templates()
print('llm =', cfg['llm'])
print('debug.db.user =', cfg['debug.db.user'])
if __name__ == '__main__':
main()
Lists semantics
"""List merge semantics: lists are replaced, not merged per item.
Run:
PYTHONPATH=src uv run python examples/merging/31_lists_and_merge_semantics.py
"""
from __future__ import annotations
from coyaml import YSettings
def main() -> None:
base = {'features': ['a', 'b']}
override = {'features': ['c']}
cfg = YSettings(base)
cfg2 = YSettings(override)
# Emulate merge as done by add_source: the latest source replaces the list
cfg._data.update(cfg2._data) # for demonstration; add_source would do a deep merge for dicts only
print('features =', cfg['features']) # ['c']
if __name__ == '__main__':
main()