diff --git a/internal/pkg/pkg.go b/internal/pkg/pkg.go index 40bad6f..64df5df 100644 --- a/internal/pkg/pkg.go +++ b/internal/pkg/pkg.go @@ -24,6 +24,16 @@ type ( ID Checksum ) +// Encode is abbreviation for base64.URLEncoding.EncodeToString(checksum[:]). +func Encode(checksum Checksum) string { + return base64.URLEncoding.EncodeToString(checksum[:]) +} + +// encode is abbreviation for base64.URLEncoding.EncodeToString(checksum[:]). +func encode(checksum *Checksum) string { + return base64.URLEncoding.EncodeToString(checksum[:]) +} + // MustDecode decodes a string representation of [Checksum] and panics if there // is a decoding error or the resulting data is too short. func MustDecode(s string) (checksum Checksum) { @@ -143,7 +153,7 @@ func (c *Cache) LoadFile(id ID) ( ) { pathname = c.base.Append( dirIdentifier, - base64.URLEncoding.EncodeToString(id[:]), + Encode(id), ) c.mu.RLock() @@ -160,15 +170,15 @@ type ChecksumMismatchError struct { } func (e *ChecksumMismatchError) Error() string { - return "got " + base64.URLEncoding.EncodeToString(e.Got[:]) + - " instead of " + base64.URLEncoding.EncodeToString(e.Want[:]) + return "got " + Encode(e.Got) + + " instead of " + Encode(e.Want) } // pathname returns the content-addressed pathname for a [Checksum]. func (c *Cache) pathname(checksum *Checksum) *check.Absolute { return c.base.Append( dirChecksum, - base64.URLEncoding.EncodeToString(checksum[:]), + encode(checksum), ) } @@ -176,7 +186,7 @@ func (c *Cache) pathname(checksum *Checksum) *check.Absolute { func (c *Cache) pathnameIdent(id *ID) *check.Absolute { return c.base.Append( dirIdentifier, - base64.URLEncoding.EncodeToString(id[:]), + encode((*Checksum)(id)), ) } diff --git a/internal/pkg/pkg_test.go b/internal/pkg/pkg_test.go index 1151a68..f295ef0 100644 --- a/internal/pkg/pkg_test.go +++ b/internal/pkg/pkg_test.go @@ -3,7 +3,6 @@ package pkg_test import ( "bytes" "crypto/sha512" - "encoding/base64" "io/fs" "net/http" "os" @@ -51,8 +50,8 @@ func TestIdent(t *testing.T) { if got := tc.kind.Ident(tc.params, tc.deps...); got != tc.want { t.Errorf("Ident: %s, want %s", - base64.URLEncoding.EncodeToString(got[:]), - base64.URLEncoding.EncodeToString(tc.want[:])) + pkg.Encode(got), + pkg.Encode(tc.want)) } }) } @@ -132,7 +131,7 @@ func TestCache(t *testing.T) { return (pkg.Checksum)(h.Sum(nil)) }() - testdataChecksumString := base64.URLEncoding.EncodeToString(testdataChecksum[:]) + testdataChecksumString := pkg.Encode(testdataChecksum) testCases := []cacheTestCase{ {"file", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) { @@ -267,7 +266,7 @@ func TestCache(t *testing.T) { var gotChecksum pkg.Checksum wantPathnameG := base.Append( "identifier", - base64.URLEncoding.EncodeToString(wantChecksum[:]), + pkg.Encode(wantChecksum), ) if pathname, err := c.StoreFile( wantChecksum,