def stringToNestedObject()

in 02_bootstrap-scripts-python/bootstrap/context.py [0:0]


def stringToNestedObject(locator: str, value: Any, ob: dict, separator: str = "."):
    """
    This helper functions takes a string to represent a JSONPath like string, and a value,
    and puts the value in the correct nested object inside the provided object.

    :param: locator: JSONPath-like string to denote object hierarchy
    :param: value: The value to insert
    :param: ob: The object to insert the value into
    :param: separator (optional): The delimiter in the locator to use, defaults to "."
    """
    splitit = locator.split(separator)
    buffer = ob
    for idx, val in enumerate(splitit):
        if val not in buffer.keys():
            buffer[val] = {}
        if idx < len(splitit) - 1:
            buffer = buffer[val]
        else:
            if type(value) == type(str()):
                if value.lower() == 'false':
                    buffer[val] = bool("")
                elif value.lower() == 'true':
                    buffer[val] = bool(value)
                else:
                    buffer[val] = value
            else:
                buffer[val] = value