diff --git a/hash/structhash/structhash.go b/hash/structhash/structhash.go index 0d50188dd..9d706515b 100644 --- a/hash/structhash/structhash.go +++ b/hash/structhash/structhash.go @@ -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 } diff --git a/hash/structhash/structhash_test.go b/hash/structhash/structhash_test.go index 5ac573f46..b7c86a513 100644 --- a/hash/structhash/structhash_test.go +++ b/hash/structhash/structhash_test.go @@ -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 @@ -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 {