簡體   English   中英

在Go中使用“ oneof”構建protobuf消息

[英]Building protobuf messages in Go that use “oneof”

我最近開始玩GoLang和Protocol Buffer,我嘗試使用以下消息

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編譯器會為包裝器消息生成一個結構,並為其包裝的消息類型生成一個結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM