...

/

The oneof Protobuf Field

The oneof Protobuf Field

Learn about the oneof keyword in Protobuf and its usage in C#.

In Protobuf, there is an ability to have a group of fields and only set one of these conditionally. This is achieved by using the oneof keyword, which is what we'll cover in this lesson.

Applying oneof in a Protobuf file

We'll continue building upon the gRPC client and server applications we developed previously. We can do so in the interactive code editor below:

syntax = "proto3";

option csharp_namespace = "BasicGrpcService";

package basic_grpc_service;

service Chatbot {
  rpc SendMessage (ChatRequest) returns (ChatReply);
}

message ChatRequest {
  string name = 1;
  string message = 2;
}

message ChatReply {
  string message = 1;
  bool answer_found = 2;
  bytes reply_in_bytes = 3;
  map <int32, ChatHistoryEntry> message_history = 4;
  AnswerType answer_type = 5;
  ChatHistoryEntry.ResponseType response_type = 6;
}

message ChatHistoryEntry
{
  string request_message = 1;
  string response_message = 2;
  enum ResponseType
  {
	UNKNOWN = 0;
	HELP = 1;
	GREETING = 2;
  }
}

enum AnswerType
{
  UNKNOWN = 0;
  HELP = 1;
  GREETING = 2;
}
The complete setup of both the gRPC client and gRPC server projects

To demonstrate the usage of oneof, we've made the highlighted modifications to the chatbot.proto file in both the ...