protocol-buffers - 在 protobuf 消息中定义嵌套的 oneof 消息

标签 protocol-buffers grpc

我试图在 protobuf 消息中定义嵌套的 oneof 消息。我们可以在 protobuf 消息中嵌套 oneof 消息吗?

谢谢

最佳答案

您可以使用包含消息的“oneof”定义一条消息。这些消息还可能包含“oneof”。

例如,如果我有一个 API 来控制我的硬件,包括冷却器和注入(inject)器泵,并且它们各自支持自己的操作,可能每个都有不同的参数,我的 API 允许对单个设备发出单个命令并仅强制执行使用了正确的参数:

syntax="proto3";

message ChillerSetTemperature {
    int32 temperature=1;
}

message ChillerGetTemperature {
    bool noArg=1;
}

message Chiller {
    oneof command {
        ChillerSetTemperature Set = 1;
        ChillerGetTemperature Get = 2;
    }
}

message SyringePumpAspirate {
    int32 volume = 1;
    int32 speed = 2;
}

message SyringePumpDispense {
    int32 volume = 1;
    int32 speed = 2;
}

message SyringePumpSelectPort {
    int32 position=1;
}

message Syringe {
    oneof command {
        SyringePumpAspirate Aspirate = 1;
        SyringePumpDispense Dispense = 2;
        SyringePumpSelectPort SelectPort = 3;
    }
}

message Action {
    oneof device {
        Syringe syringe = 1;
        Chiller chiller = 2;
    }
}

关于protocol-buffers - 在 protobuf 消息中定义嵌套的 oneof 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737849/

相关文章:

asp.net-core - grpc 服务器在 docker-compose 中不起作用

grpc - 将 retryPolicy 与 python GRPC 客户端一起使用

grpc - 如何在 gRPC 的 responseObserver.onError() 中传输详细的异常消息

c - 读取和写入内存数据

c++ - 如何使用 grpc 和 google Protocol Buffer 对数组进行就地修改?

scala - 使用 scalapb 在 Spark Streaming 中解码 Proto Buf 消息时出错

c# - 我可以使用 gRpc 代替 SignalR 来更新客户端通知吗?

c++ - 找不到 Protobuf(缺少 : Protobuf_PROTOC_EXECUTABLE)

python - 如何使用 Python 传输二进制数据?

python - 异步链接 grpc 调用