internal/pkg: wrap checksum string encoding
All checks were successful
Test / Create distribution (push) Successful in 43s
Test / Sandbox (push) Successful in 2m36s
Test / ShareFS (push) Successful in 3m46s
Test / Hpkg (push) Successful in 4m24s
Test / Sandbox (race detector) (push) Successful in 4m53s
Test / Hakurei (race detector) (push) Successful in 5m48s
Test / Flake checks (push) Successful in 1m45s
Test / Hakurei (push) Successful in 2m30s

This wraps base64.URLEncoding.EncodeToString for cleaner call site.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-03 22:02:15 +09:00
parent 8ad9909065
commit 863d3dcf9f
2 changed files with 19 additions and 10 deletions

View File

@@ -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)),
)
}

View File

@@ -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,