From c487391b3d244207a481f04f6906d8432bc99ba4 Mon Sep 17 00:00:00 2001 From: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> Date: Wed, 25 Oct 2023 07:10:31 -0500 Subject: [PATCH 1/2] Changed to pointer receiver when both were mixed - Changed to pointer receiver when both were mixed. - https://go.dev/doc/faq#methods_on_values_or_pointers Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> --- attestation/material/material.go | 6 +++--- attestation/product/product.go | 6 +++--- cryptoutil/digestset.go | 12 ++++++------ registry/option.go | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/attestation/material/material.go b/attestation/material/material.go index 801289ac..84394d33 100644 --- a/attestation/material/material.go +++ b/attestation/material/material.go @@ -47,15 +47,15 @@ type Attestor struct { materials map[string]cryptoutil.DigestSet } -func (a Attestor) Name() string { +func (a *Attestor) Name() string { return Name } -func (a Attestor) Type() string { +func (a *Attestor) Type() string { return Type } -func (rc *Attestor) RunType() attestation.RunType { +func (a *Attestor) RunType() attestation.RunType { return RunType } diff --git a/attestation/product/product.go b/attestation/product/product.go index 81a9a7da..3ef24571 100644 --- a/attestation/product/product.go +++ b/attestation/product/product.go @@ -125,15 +125,15 @@ func fromDigestMap(digestMap map[string]cryptoutil.DigestSet) map[string]attesta return products } -func (a Attestor) Name() string { +func (a *Attestor) Name() string { return Name } -func (a Attestor) Type() string { +func (a *Attestor) Type() string { return Type } -func (rc *Attestor) RunType() attestation.RunType { +func (a *Attestor) RunType() attestation.RunType { return RunType } diff --git a/cryptoutil/digestset.go b/cryptoutil/digestset.go index d97ef70a..4c70eddd 100644 --- a/cryptoutil/digestset.go +++ b/cryptoutil/digestset.go @@ -114,9 +114,9 @@ func (first DigestSet) Equal(second DigestSet) bool { return hasMatchingDigest } -func (ds DigestSet) ToNameMap() (map[string]string, error) { +func (first DigestSet) ToNameMap() (map[string]string, error) { nameMap := make(map[string]string) - for hash, digest := range ds { + for hash, digest := range first { 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 (ds DigestSet) MarshalJSON() ([]byte, error) { - nameMap, err := ds.ToNameMap() +func (first DigestSet) MarshalJSON() ([]byte, error) { + nameMap, err := first.ToNameMap() if err != nil { return nil, err } @@ -199,7 +199,7 @@ func (ds DigestSet) MarshalJSON() ([]byte, error) { return json.Marshal(nameMap) } -func (ds *DigestSet) UnmarshalJSON(data []byte) error { +func (first *DigestSet) UnmarshalJSON(data []byte) error { nameMap := make(map[string]string) err := json.Unmarshal(data, &nameMap) if err != nil { @@ -211,7 +211,7 @@ func (ds *DigestSet) UnmarshalJSON(data []byte) error { return err } - *ds = newDs + *first = newDs return nil } diff --git a/registry/option.go b/registry/option.go index 71ba1669..28591008 100644 --- a/registry/option.go +++ b/registry/option.go @@ -37,7 +37,7 @@ type ConfigOption[T any, TOption Option] struct { setter func(T, TOption) (T, error) } -func (co ConfigOption[T, TOption]) Name() string { +func (co *ConfigOption[T, TOption]) Name() string { if len(co.prefix) == 0 { return co.name } @@ -49,15 +49,15 @@ func (co *ConfigOption[T, TOption]) SetPrefix(prefix string) { co.prefix = prefix } -func (co ConfigOption[T, TOption]) DefaultVal() TOption { +func (co *ConfigOption[T, TOption]) DefaultVal() TOption { return co.defaultVal } -func (co ConfigOption[T, TOption]) Description() string { +func (co *ConfigOption[T, TOption]) Description() string { return co.description } -func (co ConfigOption[T, TOption]) Setter() func(T, TOption) (T, error) { +func (co *ConfigOption[T, TOption]) Setter() func(T, TOption) (T, error) { return co.setter } 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 2/2] 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 }