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 toNullable<T>
whereT
is the corresponding non-nullable type. For example, a field of typeDoubleValue
results in a C# property of typeNullable<double>
.Fields of type
StringWrapper
orBytesWrapper
result in C# properties of typestring
andByteString
being generated, but with a default value ofnull
, and allowingnull
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?
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.
Protobuf messages are either present (possibly default) valued or optional but they can't be null.
"Protobuf (Protocol buffers) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With