给定以下 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;
}
此 API 与后端 Firestore“foos”集合对话。
实现是在 Golang 中。该firestore
包有一个Update()
方法,但它需要提供一个firestore.Update
结构列表:
type Update struct {
Path string // Will be split on dots, and must not contain any of "˜*/[]".
FieldPath FieldPath
Value interface{}
}
我在编写 RPC 的实现时遇到问题UpdateFoo
:我不能轻易地构造firestore.Update
结构。同时,我要确保提供的路径update_mask
是有效的。
这就是我正在尝试的:
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}
}
任何提示将不胜感激。