Protobuf

-

I've been trying to talk to a network device that uses Googles Protocol Buffers for it's data exchange. Fair enough and thankfully someone had already done the work and created the needed protocol file. Of course, it was created a while back and protobuf has moved onto proto3!

Enter proto3!

After copying the original proto file across to my project, I edited it to adapt to the newer proto3 specification and pressed on with actually using it.

syntax="proto3";
package main;

message Register {
  bytes uuid = 1;
  ...

All progressed well, until...

Required Field

One of the message structures has 3 fields and all 3 must be present for things to work, but one has a value of 0 - which means proto3 protobuf ignores it! Huh?

So, apparently there is a lot of flamewar stuff out there about this, and to be honest I'm not interested and can see both sides of the argument. All I want is for it to work. After looking around for a solution I came up empty (there seemed to be a solution if I wanted JSON output), so decided to try something different...

One Becomes Two

There is no reason why every proto file has to be proto3 syntax and you can have a lot of different .proto files in a project. I took the problem message structure and created a new file with the syntax set to proto2 instead of proto3. Simples!

syntax="proto2";
package main;

message Register {
  required bytes uuid = 1;
  ...

The addition of the "required" keyword is the magic foo.

It even works :-)

If there is a better solution, feel free to let me know!