types/model: export ParseNameBare and Merge (#3957)

These are useful outside this package.
This commit is contained in:
Blake Mizerany 2024-04-26 14:58:07 -07:00 committed by GitHub
parent 11d83386a5
commit b1390a7b37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 9 deletions

View file

@ -132,11 +132,12 @@ type Name struct {
// field values are left in an undefined state. Use [Name.IsValid] to check
// if the name is valid.
func ParseName(s string) Name {
return merge(parseName(s), DefaultName())
return Merge(ParseNameBare(s), DefaultName())
}
// parseName is the same as [ParseName] without a merge.
func parseName(s string) Name {
// ParseNameBare parses s as a name string and returns a Name. No merge with
// [DefaultName] is performed.
func ParseNameBare(s string) Name {
var n Name
var promised bool
@ -161,9 +162,9 @@ func parseName(s string) Name {
return n
}
// merge merges the host, namespace, and tag parts of the two names,
// Merge merges the host, namespace, and tag parts of the two names,
// preferring the non-empty parts of a.
func merge(a, b Name) Name {
func Merge(a, b Name) Name {
a.Host = cmp.Or(a.Host, b.Host)
a.Namespace = cmp.Or(a.Namespace, b.Namespace)
a.Tag = cmp.Or(a.Tag, b.Tag)

View file

@ -93,7 +93,7 @@ func TestParseNameParts(t *testing.T) {
for _, tt := range cases {
t.Run(tt.in, func(t *testing.T) {
got := parseName(tt.in)
got := ParseNameBare(tt.in)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseName(%q) = %v; want %v", tt.in, got, tt.want)
}
@ -166,7 +166,7 @@ func TestNameparseNameDefault(t *testing.T) {
func TestNameIsValid(t *testing.T) {
var numStringTests int
for s, want := range testCases {
n := parseName(s)
n := ParseNameBare(s)
t.Logf("n: %#v", n)
got := n.IsValid()
if got != want {
@ -175,7 +175,7 @@ func TestNameIsValid(t *testing.T) {
// Test roundtrip with String
if got {
got := parseName(s).String()
got := ParseNameBare(s).String()
if got != s {
t.Errorf("parseName(%q).String() = %q; want %q", s, got, s)
}
@ -221,7 +221,7 @@ func FuzzName(f *testing.F) {
f.Add(s)
}
f.Fuzz(func(t *testing.T, s string) {
n := parseName(s)
n := ParseNameBare(s)
if n.IsValid() {
parts := [...]string{n.Host, n.Namespace, n.Model, n.Tag, n.RawDigest}
for _, part := range parts {