def flatten_dict()

in ecs_logging/_utils.py [0:0]


def flatten_dict(value: Mapping[str, Any]) -> Dict[str, Any]:
    """Adds dots to all nested fields in dictionaries.
    Raises an error if there are entries which are represented
    with different forms of nesting. (ie {"a": {"b": 1}, "a.b": 2})
    """
    top_level = {}
    for key, val in value.items():
        if not isinstance(val, collections.abc.Mapping):
            if key in top_level:
                raise ValueError(f"Duplicate entry for '{key}' with different nesting")
            top_level[key] = val
        else:
            val = flatten_dict(val)
            for vkey, vval in val.items():
                vkey = f"{key}.{vkey}"
                if vkey in top_level:
                    raise ValueError(
                        f"Duplicate entry for '{vkey}' with different nesting"
                    )
                top_level[vkey] = vval

    return top_level