Skip to content

Commit

Permalink
Improve DigestSet logic and JSON marshalling
Browse files Browse the repository at this point in the history
- Change the signature of `Equal()` and `ToNameMap()` functions to accept a pointer

Signed-off-by: naveensrinivasan <[email protected]>
  • Loading branch information
naveensrinivasan authored and jkjell committed Nov 15, 2023
1 parent c487391 commit 78ca945
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion attestation/product/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions cryptoutil/digestset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down Expand Up @@ -190,16 +190,16 @@ 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
}

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 {
Expand All @@ -211,7 +211,7 @@ func (first *DigestSet) UnmarshalJSON(data []byte) error {
return err
}

*first = newDs
*ds = newDs
return nil
}

Expand Down

0 comments on commit 78ca945

Please sign in to comment.