types/model: restrict digest hash part to a minimum of 2 characters (#3858)

This allows users of a valid Digest to know it has a minimum of 2
characters in the hash part for use when sharding.

This is a reasonable restriction as the hash part is a SHA256 hash which
is 64 characters long, which is the common hash used. There is no
anticipation of using a hash with less than 2 characters.

Also, add MustParseDigest.

Also, replace Digest.Type with Digest.Split for getting both the type
and hash parts together, which is most the common case when asking for
either.
This commit is contained in:
Blake Mizerany
2024-04-23 18:24:17 -07:00
committed by GitHub
parent 16b52331a4
commit 4dc4f1be34
3 changed files with 34 additions and 19 deletions

View File

@@ -7,6 +7,7 @@ import (
"hash/maphash"
"io"
"log/slog"
"path"
"path/filepath"
"slices"
"strings"
@@ -589,10 +590,20 @@ func ParseNameFromURLPath(s, fill string) Name {
// Example:
//
// ParseName("example.com/namespace/model:tag+build").URLPath() // returns "/example.com/namespace/model:tag"
func (r Name) URLPath() string {
func (r Name) DisplayURLPath() string {
return r.DisplayShortest(MaskNothing)
}
// URLPath returns a complete, canonicalized, relative URL path using the parts of a
// complete Name in the form:
//
// <host>/<namespace>/<model>/<tag>
//
// The parts are downcased.
func (r Name) URLPath() string {
return strings.ToLower(path.Join(r.parts[:PartBuild]...))
}
// ParseNameFromFilepath parses a file path into a Name. The input string must be a
// valid file path representation of a model name in the form:
//