I have two servers
- User Server: handle all the user CRUD operation
-
Product Server: Handle Product CRUD operation and fetch user info from the user server via gRPC call
type User struct { ID string `json:"id"` FirstName string `json:"firstName"` MiddleName *string `json:"middleName,omitempty"` LastName string `json:"lastName"` Email string `json:"email"` Disabled bool `json:"disabled"` LastSignedInAt *time.Time `json:"lastSignedInAt,omitempty"` Bio *string `json:"bio,omitempty"` BirthDate *time.Time `json:"birthDate,omitempty"` }
Here some fields are optionals and as I am using cockroachDB(extended postgreSQL), I kept them as a pointer so it's easy in scaning variable form query result.
And here is my proto file:
message User {
int64 id = 1;
string firstName = 2;
string lastName = 3;
string email = 5;
bool disabled = 6;
string lastSignedInAt = 8;
string bio = 9;
string birthdate = 10;
string profession = 14;
}
Now generated model from above proto file is like this:"
type User struct {
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
FirstName string `protobuf:"bytes,2,opt,name=firstName,proto3" json:"firstName,omitempty"`
LastName string `protobuf:"bytes,3,opt,name=lastName,proto3" json:"lastName,omitempty"`
Email string `protobuf:"bytes,4,opt,name=email,json=email,proto3" json:"email,omitempty"`
Disabled bool `protobuf:"varint,6,opt,name=disabled,proto3" json:"disabled,omitempty"`
LastSignedInAt string `protobuf:"bytes,8,opt,name=lastSignedInAt,proto3" json:"lastSignedInAt,omitempty"`
Bio string `protobuf:"bytes,9,opt,name=bio,proto3" json:"bio,omitempty"`
Birthdate string `protobuf:"bytes,10,opt,name=birthdate,proto3" json:"birthdate,omitempty"`
}
Now the problem is as I am using a pointer for the optional field it will store null in case of no value but on the opposite site gRPC won't understand the null value and throw the error.
I have tried google.protobuf.StringValue
value as a grpc type like this
google.protobuf.StringValue lastSignedInAt = 8;
This works but the problem is I have to write condition for each field in the handler:
if lastSignedInAt != nil {
user.LastSignedInAt = &wrappers.StringValue{Value:*lastSignedInAt}
}
What is the best way to tackle this issue? Should I change the database model or any changes in the gRPC model?