Register a klass as the factory for a given type URL.

    :type klass: :class:`type`
    :param klass: class to be used as a factory for the given type

    :type type_url: str
    :param type_url: (Optional) URL naming the type. If not provided,
                     infers the URL from the type descriptor.

    :raises ValueError: if a registration already exists for the URL.

def register_type(klass, type_url=None):
    """Register a klass as the factory for a given type URL.

    :type klass: :class:`type`
    :param klass: class to be used as a factory for the given type

    :type type_url: str
    :param type_url: (Optional) URL naming the type. If not provided,
                     infers the URL from the type descriptor.

    :raises ValueError: if a registration already exists for the URL.
    """
    if type_url is None:
        type_url = _compute_type_url(klass)
    if type_url in _TYPE_URL_MAP:
        if _TYPE_URL_MAP[type_url] is not klass:
            raise ValueError("Conflict: %s" % (_TYPE_URL_MAP[type_url],))

    _TYPE_URL_MAP[type_url] = klass