简体   繁体   English

如何在 Go 中使用协议缓冲区 FieldMask

[英]How to use protocol buffer FieldMask in Go

Python Generated Code lists Well Known Types , but there is no equivalent in Go Generated Code . Python 生成的代码列出了众所周知的类型,但Go 生成的代码中没有等效的类型 I assume if there was, it would document this package google.golang.org/protobuf/types/known .我假设如果有,它会记录这个包google.golang.org/protobuf/types/known

Specifically, I was looking for documentation on FieldMask .具体来说,我正在寻找有关FieldMask文档。 While it's completely absent on Go Generated Code , I did find it on go.dev ( https://pkg.go.dev/google.golang.org/protobuf/types/known/fieldmaskpb ).虽然它在Go Generated Code上完全不存在,但我确实在go.dev ( https://pkg.go.dev/google.golang.org/protobuf/types/known/fieldmaskpb ) 上找到了它。

Question

The Python implementation of FieldMask provides a MergeMessage function that merges fields specified in FieldMask from source to destination. FieldMask的 Python 实现提供了一个MergeMessage函数,该函数将FieldMask指定的FieldMask从源合并到目标。 This is really useful in API update operations because you can easily merge 2 proto messages while honoring the FieldMask :这在API 更新操作中非常有用,因为您可以在遵守FieldMask同时轻松合并 2 条 proto 消息:

# get field mask and message from request
updated_message = request.message
field_mask      = request.mask

# load original message from database 
original_message = read_from_db(request.id)

# source, destination
field_mask.MergeMessage(updated_message, original_message)

# original_message is now updated according to the field mask

Is there an equivalent convenience function in Go? Go 中是否有等效的便利功能? If not, how should proto messages be merged using a FieldMask ?如果没有,应该如何使用FieldMask合并原始消息? Is there a reference implementation or example I could follow?是否有我可以遵循的参考实现或示例? I couldn't find any use of FieldMask in grpc-go/examples/ .我在grpc-go/examples/ 中找不到任何FieldMask用途

While there is a proto Merge function , it merges all fields and there is no way to incorporate a FieldMask to merge only specific fields.虽然有一个 proto Merge 函数,但它合并所有字段,并且无法合并FieldMask以仅合并特定字段。

I think it's developer's job to deal with the protobuf FieldMask in go.我认为在 go 中处理 protobuf FieldMask是开发人员的工作。

fieldmaskpb.FieldMask type only provides IsValid and Normalize methods. fieldmaskpb.FieldMask类型仅提供IsValidNormalize方法。

There are some great repo for this problem, fieldmask-utils , and fmutils .这个问题有一些很好的 repo, fieldmask-utilsfmutils

Based on my understanding, after you get the FieldMask value, which is a []string :根据我的理解,在获得FieldMask值后,它是一个[]string

  1. with go's v1 api, you need to use reflect on the struct (which converted from pb message) or use a map[string]interface{} (converted from pb message) to deal with the FieldMask使用 go 的 v1 api,您需要使用reflect struct (从 pb 消息转换而来)或使用map[string]interface{} (从 pb 消息转换)来处理FieldMask
  2. or with go's v2 api, it becomes easier, as for the reflect api on proto.Message .或者使用 go 的 v2 api,它变得更容易,至于proto.Message上的反射 api。 Check the code检查代码

You can go for more code details in the above repo to help you write your own code related to FieldMask .您可以在上述 repo 中查看更多代码详细信息,以帮助您编写自己的与FieldMask相关的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM