예제 #1
0
def deserialize_typespec(
    spec: Typespec,
) -> Union[PrimitiveType, Dict[Union[PrimitiveType, "Proxytype"], Union[
        PrimitiveType, "Proxytype"]], "Proxytype", ]:
    component = spec.WhichOneof("component")
    if component == "primitive":
        primitive_field = spec.primitive.WhichOneof("value")
        assert primitive_field is not None, "Primitive message must have a value set"
        return getattr(spec.primitive, primitive_field)
    elif component == "type":
        try:
            return types[spec.type]
        except KeyError:
            raise ValueError("No known type {!r}".format(spec.type))
    elif component == "map":
        return {
            deserialize_typespec(param.key): deserialize_typespec(param.val)
            for param in spec.map.items
        }
    elif component == "composite":
        generic = deserialize_typespec(Typespec(type=spec.composite.type))
        params = tuple(
            deserialize_typespec(param) for param in spec.composite.params)
        return generic[params]
    else:
        raise ValueError(
            "Invalid typespec in ``deserialize_typespec``: none of the `component` fields are set."
        )
예제 #2
0
def typespec_to_unmarshal_str(typespec: Typespec) -> str:
    component = typespec.WhichOneof("component")
    if component == "type":
        marshal_type = typespec.type
    elif component == "composite":
        marshal_type = typespec.composite.type
    else:
        raise ValueError(
            "Invalid typespec: the `type` or `composite` field must be set in `component`."
        )

    if marshal_type not in unmarshal.registry:
        raise TypeError(
            "{!r} is not a computable type. Note that if this is a function-like type, "
            "you should call it and compute the result, "
            "not the function itself.".format(marshal_type))
    return marshal_type