Is the protobuf compiler failing to produce a method for querying the presence of a message field?

42 Views Asked by At

When compiling a .proto file with syntax = "proto3", the compiler should generate a method to query the presence of a message field regardless, see documentation

Message field always explicit presence

For a field e.g. "optional int32 test = 1", after compiling the .proto file to C# code, the class contains a query-method "bool HasTest()". but, when the field is of type Singular message no query-method is generated.

  1. I downloaded protoc from here
  2. A proto file called Hazzer.proto contains:

//----- Hazzer.proto Begin ----- syntax = "proto3";

message Payload
{
int32 pay = 1;
}

message Conveyor
{
Payload load = 1;
optional int32 sequence = 2;
}

//----- Hazzer.proto End-----

  1. I compile Hazzer.proto (which resides in a folder called Proto): protoc.exe --csharp_out=./ProtocOutput ./Protos/Hazzer.proto

My understanding is that under the explicit presence discipline, there should be generated a query method. Hence, in Conveyor.cs I should find "bool HasSequence()" and "bool HasLoad()".

Only HasSequence() is generated. What am I missing?

1

There are 1 best solutions below

2
Marc Gravell On BEST ANSWER

The Has... mechanism is only needed for primitives; in the case of messages, the fact that the message itself can be tested for null is used as the "has" test; i.e.

if (obj.Load is not null)
{
  // yay, it has a value; do something funky
}

(and yes, it could have used int? for Sequence, but: it doesn't)