Fixed low-level bug that failed to deal with null/empty hashing.

Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
Dave Shanley
2023-08-09 07:33:19 -04:00
committed by quobix
parent f7ab737f0a
commit f05abdbcb5
2 changed files with 9 additions and 0 deletions

View File

@@ -698,6 +698,9 @@ func AreEqual(l, r Hashable) bool {
// GenerateHashString will generate a SHA36 hash of any object passed in. If the object is Hashable // GenerateHashString will generate a SHA36 hash of any object passed in. If the object is Hashable
// then the underlying Hash() method will be called. // then the underlying Hash() method will be called.
func GenerateHashString(v any) string { func GenerateHashString(v any) string {
if v == nil {
return ""
}
if h, ok := v.(Hashable); ok { if h, ok := v.(Hashable); ok {
if h != nil { if h != nil {
return fmt.Sprintf(HASH, h.Hash()) return fmt.Sprintf(HASH, h.Hash())

View File

@@ -1669,6 +1669,12 @@ func TestGenerateHashString(t *testing.T) {
assert.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", assert.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
GenerateHashString("hello")) GenerateHashString("hello"))
assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
GenerateHashString(""))
assert.Equal(t, "",
GenerateHashString(nil))
} }
func TestGenerateHashString_Pointer(t *testing.T) { func TestGenerateHashString_Pointer(t *testing.T) {