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

Only force hash calculation on interface containing struct #1321

Merged
merged 4 commits into from
Sep 16, 2019
Merged
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
2 changes: 1 addition & 1 deletion hash/structhash/structhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func write(buf *bytes.Buffer, v reflect.Value) *bytes.Buffer {
// NOTE: structhash will allow to process all interface types.
// gogo/protobuf is not able to set tags for directly oneof interface.
// see: https://github.com/gogo/protobuf/issues/623
if to.name == "" && reflect.Zero(sf.Type).Kind() == reflect.Interface {
if to.name == "" && reflect.Zero(sf.Type).Kind() == reflect.Interface && (v.Field(i).Elem().Kind() == reflect.Struct || (v.Field(i).Elem().Kind() == reflect.Ptr && v.Field(i).Elem().Elem().Kind() == reflect.Struct)) {
to.skip = false
to.name = sf.Name
}
Expand Down
63 changes: 63 additions & 0 deletions hash/structhash/structhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestMd5(t *testing.T) {

//nolint:megacheck
func TestDump(t *testing.T) {
int1 := int(1)
tests := []struct {
v interface{}
s string
Expand Down Expand Up @@ -139,6 +140,68 @@ func TestDump(t *testing.T) {
}{b: false}},
"{a:{b:false}}",
},
{
struct {
a interface{}
}{
struct {
b int
c interface{}
}{
b: 1,
c: 2,
},
},
"{a:{}}",
},
{
struct {
a interface{}
}{
&struct {
b int `hash:"name:b"`
c interface{} `hash:"name:c"`
}{
b: 1,
c: 2,
},
},
"{a:{b:1,c:2}}",
},
{
struct {
a interface{}
}{
&struct {
b *int
c interface{}
}{
b: &int1,
c: &int1,
},
},
"{a:{}}",
},
{
struct {
a interface{}
}{
&struct {
b *int
c interface{}
}{
b: nil,
c: nil,
},
},
"{a:{}}",
},
{
struct {
a interface{}
}{nil},
"{}",
},
}

for _, tt := range tests {
Expand Down