def process(request: plugin.CodeGeneratorRequest,
            response: plugin.CodeGeneratorResponse) -> None:
    response.supported_features = plugin.CodeGeneratorResponse.FEATURE_PROTO3_OPTIONAL
    for proto_file in request.proto_file:
        has_service = False
        file = plugin.CodeGeneratorResponse.File()
        proto_file_name = proto_file.name.split('.')[0]
        file.name = "{}.client.ts".format(proto_file_name)
        file_content = ""
        file_content += f"import * as {proto_file_name} from './{proto_file_name}';\n"
        file_content += "import init from '../wasm/pkg/wasm';\n"
        file_content += "import * as wasm from '../wasm/pkg/wasm';\n\n"
        file_content += "export async function WasmInit() {\n"
        file_content += "  await init();\n"
        file_content += "}\n\n"
        for service in proto_file.service:
            has_service = True
            service_name = service.name
            for method in service.method:
                # type names pave . as prefix
                input_type = method.input_type[1:]
                output_type = method.output_type[1:]
                snake_method_name = camel_to_snake(method.name)
                file_content += f"export function {service_name}{method.name}(input: {input_type}): {output_type} {{\n"
                file_content += f"  let input_bytes = {input_type}.toBinary(input);\n"
                file_content += f"  let output_bytes = wasm.{snake_method_name}(input_bytes);\n"
                file_content += f"  return {output_type}.fromBinary(output_bytes);\n"
                file_content += "}\n\n"

        if has_service:
            file.content = file_content
            response.file.append(file)
Esempio n. 2
0
def main() -> None:
    with os.fdopen(sys.stdin.fileno(), 'rb') as inp:
        request = CodeGeneratorRequest.FromString(inp.read())

    types_map: Dict[str, str] = {}
    for pf in request.proto_file:
        for mt in pf.message_type:
            types_map.update(_type_names(pf, mt))

    response = CodeGeneratorResponse()

    # See https://github.com/protocolbuffers/protobuf/blob/v3.12.0/docs/implementing_proto3_presence.md  # noqa
    if hasattr(CodeGeneratorResponse, 'Feature'):
        response.supported_features = (  # type: ignore
            CodeGeneratorResponse.FEATURE_PROTO3_OPTIONAL  # type: ignore
        )

    for file_to_generate in request.file_to_generate:
        proto_file = _get_proto(request, file_to_generate)

        imports = [
            _proto2pb2_module_name(dep)
            for dep in list(proto_file.dependency) + [file_to_generate]
        ]

        services = []
        for service in proto_file.service:
            methods = []
            for method in service.method:
                cardinality = _CARDINALITY[(method.client_streaming,
                                            method.server_streaming)]
                methods.append(
                    Method(
                        name=method.name,
                        cardinality=cardinality,
                        request_type=types_map[method.input_type],
                        reply_type=types_map[method.output_type],
                    ))
            services.append(Service(name=service.name, methods=methods))

        file = response.file.add()
        module_name = _proto2grpc_module_name(file_to_generate)
        file.name = module_name.replace(".", "/") + ".py"
        file.content = render(
            proto_file=proto_file.name,
            package=proto_file.package,
            imports=imports,
            services=services,
        )

    with os.fdopen(sys.stdout.fileno(), 'wb') as out:
        out.write(response.SerializeToString())