mennanov / fmutils

Golang protobuf FieldMask missing utils
MIT License
109 stars 11 forks source link
fieldmask golang protobuf

Golang protobuf FieldMask utils

Build Status Coverage Status PkgGoDev

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"})

Merge protobuf messages with a FieldMask applied

// Overwrites the fields in the dst from src.
// Only the fields listed in the field mask will be copied.
fmutils.Overwrite(src, dst, []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:

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.