def doc_flat_to_nested(key_list, val):
    """ Convert a flat keys and value into a nested document.
    e.g.:

    { a.b.c: 1 }

    =>

    { a: { b: { c: 1 } } }
    """
    res = {}
    if len(key_list) > 1:
        res[key_list[0]] = doc_flat_to_nested(key_list[1:], val)
    elif len(key_list) == 1:
        res[key_list[0]] = val
    else:
        raise Exception('invalid key_list @%s' % doc_flat_to_nested.__name__)
    return res