Skip to content

Commit

Permalink
Merge pull request #1321 from mesg-foundation/fix/struct-hash-calc-nico
Browse files Browse the repository at this point in the history
Only force hash calculation on interface containing struct
  • Loading branch information
Nicolas Mahé authored Sep 16, 2019
2 parents 3f4b289 + 9edd225 commit 9d2e3cd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
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

0 comments on commit 9d2e3cd

Please sign in to comment.