mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 16:59:22 +02:00
protoutil: add OverwriteMasked method (#4651)
Add a method to copy selected fields from one proto message to another (of the same type), using a FieldMask. This is intended for use in a new databroker Patch method.
This commit is contained in:
parent
2472490075
commit
2cc82ed706
3 changed files with 225 additions and 0 deletions
55
pkg/protoutil/fieldmasktree_test.go
Normal file
55
pkg/protoutil/fieldmasktree_test.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package protoutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/types/known/fieldmaskpb"
|
||||
)
|
||||
|
||||
func TestFieldMaskTree(t *testing.T) {
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
tr := newFieldMaskTree(&fieldmaskpb.FieldMask{})
|
||||
assert.Equal(t, fieldMaskTree(nil), tr)
|
||||
})
|
||||
t.Run("basic", func(t *testing.T) {
|
||||
tr := newFieldMaskTree(&fieldmaskpb.FieldMask{
|
||||
Paths: []string{"foo", "bar", "baz"},
|
||||
})
|
||||
assert.Equal(t, fieldMaskTree{
|
||||
"foo": {},
|
||||
"bar": {},
|
||||
"baz": {},
|
||||
}, tr)
|
||||
})
|
||||
t.Run("nested", func(t *testing.T) {
|
||||
tr := newFieldMaskTree(&fieldmaskpb.FieldMask{
|
||||
Paths: []string{"foo.bar.baz", "foo.bar.xyz", "foo.quux"},
|
||||
})
|
||||
assert.Equal(t, fieldMaskTree{
|
||||
"foo": {
|
||||
"bar": {
|
||||
"baz": {},
|
||||
"xyz": {},
|
||||
},
|
||||
"quux": {},
|
||||
},
|
||||
}, tr)
|
||||
})
|
||||
t.Run("overlapping fields 1", func(t *testing.T) {
|
||||
tr := newFieldMaskTree(&fieldmaskpb.FieldMask{
|
||||
Paths: []string{"foo", "foo.bar"},
|
||||
})
|
||||
assert.Equal(t, fieldMaskTree{
|
||||
"foo": {},
|
||||
}, tr)
|
||||
})
|
||||
t.Run("overlapping fields 2", func(t *testing.T) {
|
||||
tr := newFieldMaskTree(&fieldmaskpb.FieldMask{
|
||||
Paths: []string{"foo.bar", "foo"},
|
||||
})
|
||||
assert.Equal(t, fieldMaskTree{
|
||||
"foo": {},
|
||||
}, tr)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue