mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 18:36:30 +02:00
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.
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
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)
|
|
})
|
|
}
|