在 Go 中构建使用 "oneof" 的 protobuf 消息
Building protobuf messages in Go that use "oneof"
我最近开始玩弄 GoLang 和 Protocol Buffers,我尝试使用以下消息
message KumoAPIMessage {
oneof msg {
OpenStackEnvironmentContext osEnvContext = 1;
}
}
message OpenStackEnvironmentContext {
string username = 1;
string password = 2;
string domain = 3;
string project = 4;
string authUrl = 6;
string region = 7;
string contextName = 8;
}
这些消息被设计成可以将它们编组并通过 TCP 发送到服务器代理(用 Scala 编写)。我遇到的问题实际上是在 go 应用程序中构建消息。
我收到了一条 OpenStackEnvironmentContext
消息,但我不知道如何将它包装在 KumoAPIMessage
中,我尝试了以下方法
apiMessage := kumo.KumoAPIMessage{ Msg: context, }
但这只是在编译时抛出以下错误
cannot use context (type kumo.OpenStackEnvironmentContext) as type kumo.isKumoAPIMessage_Msg in field value:
kumo.OpenStackEnvironmentContext does not implement kumo.isKumoAPIMessage_Msg (missing kumo.isKumoAPIMessage_Msg method)
如果有人知道我哪里出错了,你就是我的英雄。
这一次我能够回答我自己的问题。通过大量的谷歌搜索和试验,我得到了以下解决方案
apiMessage := &kumo.KumoAPIMessage{&kumo.KumoAPIMessage_OsEnvContext{context}}
似乎 protobufs 的 GoLang 编译器为包装器消息生成了一个结构,并且为它正在包装的消息类型生成了一个结构
我最近开始玩弄 GoLang 和 Protocol Buffers,我尝试使用以下消息
message KumoAPIMessage {
oneof msg {
OpenStackEnvironmentContext osEnvContext = 1;
}
}
message OpenStackEnvironmentContext {
string username = 1;
string password = 2;
string domain = 3;
string project = 4;
string authUrl = 6;
string region = 7;
string contextName = 8;
}
这些消息被设计成可以将它们编组并通过 TCP 发送到服务器代理(用 Scala 编写)。我遇到的问题实际上是在 go 应用程序中构建消息。
我收到了一条 OpenStackEnvironmentContext
消息,但我不知道如何将它包装在 KumoAPIMessage
中,我尝试了以下方法
apiMessage := kumo.KumoAPIMessage{ Msg: context, }
但这只是在编译时抛出以下错误
cannot use context (type kumo.OpenStackEnvironmentContext) as type kumo.isKumoAPIMessage_Msg in field value:
kumo.OpenStackEnvironmentContext does not implement kumo.isKumoAPIMessage_Msg (missing kumo.isKumoAPIMessage_Msg method)
如果有人知道我哪里出错了,你就是我的英雄。
这一次我能够回答我自己的问题。通过大量的谷歌搜索和试验,我得到了以下解决方案
apiMessage := &kumo.KumoAPIMessage{&kumo.KumoAPIMessage_OsEnvContext{context}}
似乎 protobufs 的 GoLang 编译器为包装器消息生成了一个结构,并且为它正在包装的消息类型生成了一个结构