如何在 gRPC 中使用标量类型作为函数参数?

off*_*555 0 protocol-buffers grpc

在 gRPC 中,它期望参数是 protobuf 消息。假设我想发送一个int,我必须创建一个消息message Int { int32 value = 1; }并使用这个消息发送一个int。

下面是函数定义的一个例子: rpc Range(Int) returns (stream Int) {}

但我不想为每个默认标量类型创建这样的消息结构,例如 bool、int32、int64、float 等。如何在 gRPC 函数参数中使用标量类型?

cre*_*oup 5

Protobuf 提供了包装器类型,您可以使用Int32Value.

它应该是这样的

import "google/protobuf/wrappers.proto";

service YourSerice {
  rpc Range(google.protobuf.Int32Value) returns (stream google.protobuf.Int32Value) {}
}
Run Code Online (Sandbox Code Playgroud)

您可以在 protobuf github ( wrappers.proto )上检查其他原始包装