Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Nullable types be sent through Protocol Buffers?

The Proto3 C# Reference contains the following text:

Wrapper Type Fields

Most of the well-known types in proto3 do not affect code generation, but the wrapper types (StringWrapper, Int32Wrapper etc) change the type and behaviour of the properties.

All of the wrapper types that correspond to C# value types (Int32Wrapper, DoubleWrapper, BoolWrapper etc) are mapped to Nullable<T> where T is the corresponding non-nullable type. For example, a field of type DoubleValue results in a C# property of type Nullable<double>.

Fields of type StringWrapper or BytesWrapper result in C# properties of type string and ByteString being generated, but with a default value of null, and allowing null to be set as the property value.

For all wrapper types, null values are not permitted in a repeated field, but are permitted as the values for map entries.

When trying to generate a .cs file from a .proto file, If I try to declare a field as Int32Wrapper in the .proto file, protoc.exe throws an error about Int32Wrapper not existing.

syntax ="proto3";
package prototest;
import "MessageIdentifier.proto";


message TestMessage {
    string messageTest = 1;
    fixed64 messageTimestampTicks = 2;
    uint32 sequenceNumber = 3;
    MessageUniqueID uniqueID = 4;
    Int32Wrapper nullableInt = 5; 
}

It seems there is some additional step that is missing here, does anyone know how to enable these types?

like image 817
pquest Avatar asked Apr 25 '17 15:04

pquest


People also ask

Does Protobuf support null?

Protobuf treats strings as primitive types and therefore they can not be null. Instead of checking if the string is not null use standard libraries, like apache commons, to check if the string is not blank. This is clear that the value will be inserted if the value is not blank.

Is Google Protobuf timestamp Nullable?

Protobuf messages are either present (possibly default) valued or optional but they can't be null.

What is protocol buffer message?

"Protobuf (Protocol buffers) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

How are nullable types declared in C#?

You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.


2 Answers

I will try to improve Nick's answer as it hasn't helped me. grpc compiler claimed that he has no information on google.protobuf.Int32Wrapper type. I have found it is actually called google.protobuf.Int32Value (https://github.com/protocolbuffers/protobuf/blob/48234f5f012582843bb476ee3afef36cda94cb66/src/google/protobuf/wrappers.proto#L88), though google really calls it Int32Wrapper. So the code that helped me was the following:

...
import "google/protobuf/wrappers.proto";
...
message TestMessage {
    ...
    google.protobuf.Int32Value nullableInt = 5; 
}

Other links:

  • C# lib source - https://github.com/protocolbuffers/protobuf/blob/48234f5f012582843bb476ee3afef36cda94cb66/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs#L781
  • C# doc - https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/well-known-types/int32-value
like image 145
zetroot Avatar answered Sep 25 '22 01:09

zetroot


In respect that https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto

You need to import google/protobuf/wrappers.proto in order for this to work.

syntax ="proto3";
package prototest;
import "MessageIdentifier.proto";
import "google/protobuf/wrappers.proto";

message TestMessage {
    string messageTest = 1;
    fixed64 messageTimestampTicks = 2;
    uint32 sequenceNumber = 3;
    MessageUniqueID uniqueID = 4;
    google.protobuf.Int32Value nullableInt = 5; 
}

You can then use it as an int? ,eg nullableInt.HasValue and nullableInt.Value

like image 41
Nick Randell Avatar answered Sep 24 '22 01:09

Nick Randell