fmutils
fmutils copied to clipboard
Golang protobuf FieldMask missing utils
Golang protobuf FieldMask utils
Filter a protobuf message with a FieldMask applied
// Keeps the fields mentioned in the paths untouched, all the other fields will be cleared.
fmutils.Filter(protoMessage, []string{"a.b.c", "d"})
Prune a protobuf message with a FieldMask applied
// Clears all the fields mentioned in the paths, all the other fields will be left untouched.
fmutils.Prune(protoMessage, []string{"a.b.c", "d"})
Working with Golang protobuf APIv1
This library uses the new Go API for protocol buffers.
If your *.pb.go
files are generated with the old version APIv1 then you have 2 choices:
- migrate to the new APIv2
google.golang.org/protobuf
- upgrade an existing APIv1 version to
github.com/golang/[email protected]
that implements the new API
In both cases you'll need to regenerate *.pb.go
files.
If you decide to stay with APIv1 then you need to use the proto.MessageV2
function like this:
import protov1 "github.com/golang/protobuf/proto"
fmutils.Filter(protov1.MessageV2(protoMessage), []string{"a.b.c", "d"})
Read more about the Go protobuf API versions.
Examples
See the examples_test.go for real life examples.