Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support comparison of message embed structs #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions testing/protocmp/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func TestEqual(t *testing.T) {

allTypesDesc := (*testpb.TestAllTypes)(nil).ProtoReflect().Descriptor()

type embedMessage struct {
*testpb.TestAllTypes
Extra int
}
type hasEmbedMessage struct {
Embed *embedMessage
}

// Test nil and empty messages of differing types.
tests = append(tests, []test{{
x: (*testpb.TestAllTypes)(nil),
Expand Down Expand Up @@ -146,6 +154,49 @@ func TestEqual(t *testing.T) {
y: struct{ M proto.Message }{dynamicpb.NewMessage(allTypesDesc)},
opts: cmp.Options{Transform()},
want: true,
}, {
x: &hasEmbedMessage{},
y: &hasEmbedMessage{},
opts: cmp.Options{Transform()},
want: true,
}, {
x: &hasEmbedMessage{
Embed: &embedMessage{
TestAllTypes: &testpb.TestAllTypes{
DefaultInt64: proto.Int64(10),
},
Extra: 10,
},
},
y: &hasEmbedMessage{
Embed: &embedMessage{
TestAllTypes: &testpb.TestAllTypes{
DefaultInt64: proto.Int64(20),
},
Extra: 10,
},
},
opts: cmp.Options{Transform()},
want: false,
}, {
x: &hasEmbedMessage{
Embed: &embedMessage{
TestAllTypes: &testpb.TestAllTypes{
DefaultInt64: proto.Int64(10),
},
Extra: 10,
},
},
y: &hasEmbedMessage{
Embed: &embedMessage{
TestAllTypes: &testpb.TestAllTypes{
DefaultInt64: proto.Int64(10),
},
Extra: 20,
},
},
opts: cmp.Options{Transform()},
want: false,
}}...)

// Test message values.
Expand Down
26 changes: 24 additions & 2 deletions testing/protocmp/xform.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func Transform(opts ...option) cmp.Option {
return cmp.FilterPath(func(p cmp.Path) bool {
ps := p.Last()
if isMessageType(addrType(ps.Type())) {
return true
// Check if message is embed to support message embed struct
return !isMessageEmbed(ps.Type())
}

// Check whether the concrete values of an interface both satisfy
Expand All @@ -222,7 +223,9 @@ func Transform(opts ...option) cmp.Option {
if !vx.IsValid() || vx.IsNil() || !vy.IsValid() || vy.IsNil() {
return false
}
return isMessageType(addrType(vx.Elem().Type())) && isMessageType(addrType(vy.Elem().Type()))
return isMessageType(addrType(vx.Elem().Type())) && isMessageType(addrType(vy.Elem().Type())) &&
// Check if message is embed to support message embed struct
!isMessageEmbed(addrType(vx.Elem().Type())) && !isMessageEmbed(addrType(vy.Elem().Type()))
}

return false
Expand All @@ -247,6 +250,25 @@ func Transform(opts ...option) cmp.Option {
}))
}

func isMessageEmbed(t reflect.Type) bool {
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return false
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.Anonymous {
continue
}
if isMessageType(t.Field(i).Type) {
return true
}
}
return false
}

func isMessageType(t reflect.Type) bool {
// Avoid transforming the Message itself.
if t == reflect.TypeOf(Message(nil)) || t == reflect.TypeOf((*Message)(nil)) {
Expand Down