Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which 'oneof' proto3 field is set in C#

For the following Protocol Buffer message (proto3) how to I determine which type is set? There does not seem to be a "has_reply" method as is the case for the generated C++ version.

message Event {
  oneof type {
    Connection connection = 1;
    StatusReply reply = 2;
    Error error = 3;
    End end = 4;
    Empty empty = 5;
  };
}
like image 417
kyrre Avatar asked Aug 31 '17 10:08

kyrre


People also ask

What is oneof in Protobuf?

Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.

How do I set default value in Protobuf?

For bool s, the default value is false. For numeric types, the default value is zero. For enums , the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.

Why are Protobuf fields numbered?

Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.

What is repeated field in Protobuf?

repeated : this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.


1 Answers

https://developers.google.com/protocol-buffers/docs/reference/csharp-generated#oneof suggests that TypeOneofCase will tell you which is set:

Oneof Fields

Each field within a oneof has a separate property, like a regular singular field. However, the compiler also generates an additional property to determine which field in the enum has been set, along with an enum and a method to clear the oneof. For example, for this oneof field definition

oneof avatar {
  string image_url = 1;
  bytes image_data = 2;
}

The compiler will generate these public members:

enum AvatarOneofCase
{
  None = 0,
  ImageUrl = 1,
  ImageData = 2
}

public AvatarOneofCase AvatarCase { get; }
public void ClearAvatar();
public string ImageUrl { get; set; }
public ByteString ImageData { get; set; }

If a property is the current oneof "case", fetching that property will return the value set for that property. Otherwise, fetching the property will return the default value for the property's type - only one member of a oneof can be set at a time.

Setting any constituent property of the oneof will change the reported "case" of the oneof. As with a regular singular field, you cannot set a oneof field with a string or bytes type to a null value. Setting a message-type field to null is equivalent to calling the oneof-specific Clear method.

like image 147
mjwills Avatar answered Oct 09 '22 05:10

mjwills