From 78ca94550afc6b37c344d24c1df7004c6609c8c2 Mon Sep 17 00:00:00 2001 From: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:03:43 -0500 Subject: [PATCH] Improve DigestSet logic and JSON marshalling - Change the signature of `Equal()` and `ToNameMap()` functions to accept a pointer Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> --- attestation/product/product_test.go | 3 ++- cryptoutil/digestset.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/attestation/product/product_test.go b/attestation/product/product_test.go index ece998d9..76502b74 100644 --- a/attestation/product/product_test.go +++ b/attestation/product/product_test.go @@ -36,7 +36,8 @@ func TestFromDigestMap(t *testing.T) { testDigestSet["test"] = testDigest result := fromDigestMap(testDigestSet) assert.Len(t, result, 1) - assert.True(t, result["test"].Digest.Equal(testDigest)) + digest := result["test"].Digest + assert.True(t, digest.Equal(testDigest)) } func TestAttestorName(t *testing.T) { diff --git a/cryptoutil/digestset.go b/cryptoutil/digestset.go index 4c70eddd..ec291da4 100644 --- a/cryptoutil/digestset.go +++ b/cryptoutil/digestset.go @@ -96,9 +96,9 @@ func HashFromString(name string) (crypto.Hash, error) { // Equal returns true if every digest for hash functions both artifacts have in common are equal. // If the two artifacts don't have any digests from common hash functions, equal will return false. // If any digest from common hash functions differ between the two artifacts, equal will return false. -func (first DigestSet) Equal(second DigestSet) bool { +func (ds *DigestSet) Equal(second DigestSet) bool { hasMatchingDigest := false - for hash, digest := range first { + for hash, digest := range *ds { otherDigest, ok := second[hash] if !ok { continue @@ -114,9 +114,9 @@ func (first DigestSet) Equal(second DigestSet) bool { return hasMatchingDigest } -func (first DigestSet) ToNameMap() (map[string]string, error) { +func (ds *DigestSet) ToNameMap() (map[string]string, error) { nameMap := make(map[string]string) - for hash, digest := range first { + for hash, digest := range *ds { name, ok := hashNames[hash] if !ok { return nameMap, ErrUnsupportedHash(hash.String()) @@ -190,8 +190,8 @@ func CalculateDigestSetFromFile(path string, hashes []crypto.Hash) (DigestSet, e return CalculateDigestSet(file, hashes) } -func (first DigestSet) MarshalJSON() ([]byte, error) { - nameMap, err := first.ToNameMap() +func (ds DigestSet) MarshalJSON() ([]byte, error) { + nameMap, err := ds.ToNameMap() if err != nil { return nil, err } @@ -199,7 +199,7 @@ func (first DigestSet) MarshalJSON() ([]byte, error) { return json.Marshal(nameMap) } -func (first *DigestSet) UnmarshalJSON(data []byte) error { +func (ds *DigestSet) UnmarshalJSON(data []byte) error { nameMap := make(map[string]string) err := json.Unmarshal(data, &nameMap) if err != nil { @@ -211,7 +211,7 @@ func (first *DigestSet) UnmarshalJSON(data []byte) error { return err } - *first = newDs + *ds = newDs return nil }