impl#
In object-oriented programming, the inheritance hierarchy is a pattern where child objects inherit attributes and methods from parent objects. Similarly, in configuration management, the global configuration often acts as the default value, allowing for the possibility of overriding specific values in environment-specific configurations.
This module implements a simple dialect of JSON that supports inheritance for better reusability of configuration data. For example, consider the following config data:
>>> {
... "_shared": {
... "*.username": "root",
... },
... "server1": {
... "password": "password1",
... },
... "server2": {
... "password": "password2",
... }
... }
It will be transformed into:
>>> {
... "server1": {
... "username": "root",
... "password": "password1",
... },
... "server2": {
... "username": "root",
... "password": "password2",
... },
... }
The _shared key is used to specify the shared values. It is a key value
pair where the key is a path to the value.
Try to inherit a shared value to a specific item in a list or dict. If the value is already set, then do nothing. It will update the data inplace.
- Parameters:
path – JSON path in dot notation to the value to be set. Valid path examples:
key1.key2,databases.*.username. It cannot ends with.*.value – the value to be set.
data – the data to be updated.
Transform the data inplace by applying the shared values. It uses deep-first search to traverse the data, because deeper node value may override the value of the same key in the upper node.