GRPC Nullable Field
1 min readDec 23, 2020
I’ve some problem in GRPC as we know that protocol schema very fast and i love to work on it.
In few years playing grpc and have a question about why grpc proto-gen generate a getter method like
type GetBlocksRequest struct {
Height int64 `protobuf:"varint,1,opt,name=Height,proto3" json:"Height,omitempty"`
}func (m *GetBlocksRequest) GetHeight() uint32 {
if m != nil {
return m.Height
}
return 0
}
I need to allow the Height
field is nullable field, How ?
So i was digging deeply to how to nullable field on grpc and found the answer. We can use type of that field as a new type we create one which mean created by grpc-proto-gen :).
syntax="proto3";
package model;
import "google/protobuf/struct.proto";
enum OrderBy {
DESC = 0;
ASC = 1;
}message NullableInt64 {
oneof Kind {
google.protobuf.NullValue Null = 1;
int64 Value = 2;
}
}message GetBlocksRequest {
string OrderField = 1;
OrderBy OrderBy = 2;
uint32 Page = 3;
uint32 Limit = 4;
NullableInt64 Height = 5;
}
As you can see in above there is a message named NullableInt64 it is a oneof kind with google.protobuf.NullValue Null = 1;
allowing to have an Null
value instead.