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.
I ran into a similar issue trying to use firestore along with protobuf in Golang. There does not seem to be support for using proto field masks and structs to do updates.
Here's a workaround with Set
and fieldmask-utils:
import (
"github.com/golang/protobuf/protoc-gen-go/generator"
fieldmask_utils "github.com/mennanov/fieldmask-utils"
)
var (
id string
request UpdateFooRequest
)
mask, err: = fieldmask_utils.MaskFromProtoFieldMask(request.FieldMask, generator.CamelCase)
// handle err...
m := make(map[string]interface{}) // a map to copy to
err := fieldmask_utils.StructToMap(mask, request.Foo, m)
// handle err..
_, err = r.client.Collection(r.collection).Doc(id).Set(ctx, m, firestore.MergeAll)
// handle err..