如何序列化/反序列化使用 ScalaPB 的“oneof”的 protobuf 消息? (How to serialize/deserialize a protobuf message that uses 'oneof' with ScalaPB?)


問題描述

如何序列化/反序列化使用 ScalaPB 的“oneof”的 protobuf 消息? (How to serialize/deserialize a protobuf message that uses 'oneof' with ScalaPB?)

我正在使用 ScalaPB 編譯我的 Scala 案例類以序列化我的 protobuf 消息。

我有一個包含以下消息的 .proto 文件:

message WrapperMessage {
    oneof msg {
        Login login = 1;
        Register register = 2;
    }
}

message Login {
    required string email = 1;
    required string password = 2;
}

message Register {
    required string email = 1;
    required string password = 2;
    optional string firstName = 3;
    optional string lastName = 4;
}

如果知道我想將 Login 消息放入 msg 中,如何創建我的 WrapperMessage

  val login = Login(email = "test@example.com", password = "testpass")
  val wrapperMessage = WrapperMessage(???)
  val wrapperMessageBytes = wrapperMessage.toByteArray

現在假設我正在通過網絡接收 WrapperMessage;如何使用 ScalaPB 案例類方法反序列化消息?


參考解法

方法 1:

ScalaPB has documentation which clearly provides examples for the questions I am asking. In this answer I tailor the examples provided on ScalaPB towards my question.

To initialize a message with oneof:

val login = Login(email = "test@example.com", password = "testpass")
val wrapperMessage = WrapperMessage().withLogin(login)

To match against a message's oneof field:

wrapperMessage.msg match {
  case Msg.Login(l) =>  // handle l
  case Msg.Register(r) =>  // handle r
  case Msg.Empty =>  // handle exceptional case...
}

方法 2:

You should be able to initialize WrapperMessage with an empty constructor and call .set_login(login)

You would deserialize to WrapperMessage and pattern match on message.WhichOneof which returns either "login" or "register". Then you would call the accessor on that specific message (ie. message.login).

(by Edward MaxedonEdward Maxedonkliew)

參考文件

  1. How to serialize/deserialize a protobuf message that uses 'oneof' with ScalaPB? (CC BY‑SA 2.5/3.0/4.0)

#protocol-buffers #scalapb #scala






相關問題

通過 Method.invoke() 調用靜態方法給了我 NPE (Static method invocation via Method.invoke() gave me NPE)

python中的protobuf到json (Protobuf to json in python)

如何序列化/反序列化使用 ScalaPB 的“oneof”的 protobuf 消息? (How to serialize/deserialize a protobuf message that uses 'oneof' with ScalaPB?)

序列化和反序列化未知的繼承類型 (Serializing and deserializing unknown inherited types)

Thrift 與協議緩衝區 (Thrift vs Protocol buffers)

如何在客戶端排除導入 (How to exclude an import on client side)

為什麼當我安裝了 Tensorflow 的所有庫後,會出現無目錄錯誤? (Why do I get a no directory error, when I have installed all the libraries for Tensorflow?)

如何為 C# <proto/> 定義傳遞experimental_allow_proto3_optional 以在proto3 中啟用可選? (How to pass experimental_allow_proto3_optional for C# <proto/> definitions to enable optional in proto3?)

CentOS7 的 libprotobuf-lite.so 文件在 CentOS8 機器上工作嗎? (is libprotobuf-lite.so file from CentOS7 working in CentOS8 machine?)

protogen - 支持 <Property> 指定的語法 (protogen - support the <Property>Specified syntax)

.Net 字典類型在 protobuff (.Net dictionary type in protobuff)

為什麼 protobuf 更喜歡代碼生成器而不是運行時動態加載 (why protobuf prefer code-generator other than dynamic loading at runtime)







留言討論