def get_value_in_state(key, state):
    """
    Retrieve a potentially nested value in an object

    :param key: the `.` separated key (i.e: a.b.c.d)
    :param state: the object to access the key in
    :return: the value in the state
    """
    def traverse(hierarchy, current):
        level = hierarchy.pop(0)
        if level not in current:
            raise KeyError(f"specified key '{level}' does not exist")
        elif len(hierarchy) == 0:
            return current[level]
        elif type(current[level]) != dict:
            raise KeyError(f"cannot index non-dict item '{level}'")
        else:
            return traverse(hierarchy, current[level])

    keys = key.split(".")
    return traverse(keys, state)