Given the following API:
syntax = "proto3";
service FooService {
rpc UpdateFoo(UpdateFooRequest) returns (Foo) {}
}
message Foo {
string name = 1;
string description = 2;
string action = 3;
}
message UpdateFooRequest {
Foo foo = 1;
// The list of fields to be updated. For the `FieldMask` definition,
// see https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#fieldmask
google.protobuf.FieldMask update_mask = 2;
}
This API talks to a backend Firestore "foos" collection.
The implementation is in Golang. The firestore
package has an Update()
method, but it requires to provide a list of firestore.Update
structs:
type Update struct {
Path string // Will be split on dots, and must not contain any of "˜*/[]".
FieldPath FieldPath
Value interface{}
}
I have problems writing the implementation of the RPC UpdateFoo
: I cannot easily construct the firestore.Update
struct. At the same time, I want to ensure that the paths provided in the update_mask
are valid.
This is what I am trying:
func (s *Server) UpdateFoo(ctx context.Context, req *pb.UpdateFooRequest) (*pb.Foo, error) {
var updates []firestore.Update
// Loop all paths in the update_mask
for _, p := range req.GetUpdateMask().GetPaths() {
// Check that path is valid field of Foo message.
// How to do this?
...
...
if path is not valid {
return nil, status.Errorf(codes.InvalidArgument, "invalid path %s", p)
}
// When path is valid, I need to take the value from the req.GetFoo() message
// How to do this?
newValue := req.GetFoo().Get????<this is p variable>
update = append(update, &firestore.Update{Path: p, Value: newValue}
}
Any hints would be greatly appreciated.