x/model: rename func for display with Display prefix

This commit is contained in:
Blake Mizerany 2024-04-05 16:44:14 -07:00
parent a6b8bdf938
commit bf8e0c09c9
2 changed files with 59 additions and 21 deletions

View File

@ -168,26 +168,64 @@ func (r Name) MapHash() uint64 {
// Format returns a string representation of the ref with the given // Format returns a string representation of the ref with the given
// concreteness. If a part is missing, it is replaced with a loud // concreteness. If a part is missing, it is replaced with a loud
// placeholder. // placeholder.
func (r Name) Full() string { func (r Name) DisplayFull() string {
r.host = cmp.Or(r.host, "!(MISSING DOMAIN)") return (Name{
r.namespace = cmp.Or(r.namespace, "!(MISSING NAMESPACE)") host: cmp.Or(r.host, "!(MISSING DOMAIN)"),
r.model = cmp.Or(r.model, "!(MISSING NAME)") namespace: cmp.Or(r.namespace, "!(MISSING NAMESPACE)"),
r.tag = cmp.Or(r.tag, "!(MISSING TAG)") model: cmp.Or(r.model, "!(MISSING NAME)"),
r.build = cmp.Or(r.build, "!(MISSING BUILD)") tag: cmp.Or(r.tag, "!(MISSING TAG)"),
return r.String() build: cmp.Or(r.build, "!(MISSING BUILD)"),
}).String()
} }
func (r Name) ModelAndTag() string { func (r Name) DisplayModel() string {
r.host = "" return r.model
r.namespace = ""
r.build = ""
return r.String()
} }
func (r Name) ModelTagAndBuild() string { func (r Name) Has(kind NamePart) bool {
r.host = "" switch kind {
r.namespace = "" case Host:
return r.String() return r.host != ""
case Namespace:
return r.namespace != ""
case Model:
return r.model != ""
case Tag:
return r.tag != ""
case Build:
return r.build != ""
}
return false
}
// DisplayCompact returns a compact display string of the ref with only the
// model and tag parts.
func (r Name) DisplayCompact() string {
return (Name{
model: r.model,
tag: r.tag,
}).String()
}
// DisplayShort returns a short display string of the ref with only the
// model, tag, and build parts.
func (r Name) DisplayShort() string {
return (Name{
model: r.model,
tag: r.tag,
build: r.build,
}).String()
}
// DisplayLong returns a long display string of the ref including namespace,
// model, tag, and build parts.
func (r Name) DisplayLong() string {
return (Name{
namespace: r.namespace,
model: r.model,
tag: r.tag,
build: r.build,
}).String()
} }
// String returns the fully qualified ref string. // String returns the fully qualified ref string.
@ -340,7 +378,7 @@ func NameParts(s string) iter.Seq2[NamePart, string] {
func (r Name) Valid() bool { func (r Name) Valid() bool {
// Parts ensures we only have valid parts, so no need to validate // Parts ensures we only have valid parts, so no need to validate
// them here, only check if we have a name or not. // them here, only check if we have a name or not.
return r.model != "" return r.Has(Model)
} }
// isValidPart returns true if given part is valid ascii [a-zA-Z0-9_\.-] // isValidPart returns true if given part is valid ascii [a-zA-Z0-9_\.-]

View File

@ -131,10 +131,10 @@ func TestNameStringVariants(t *testing.T) {
t.Run(tt.in, func(t *testing.T) { t.Run(tt.in, func(t *testing.T) {
p := ParseName(tt.in) p := ParseName(tt.in)
t.Logf("ParseName(%q) = %#v", tt.in, p) t.Logf("ParseName(%q) = %#v", tt.in, p)
if g := p.ModelAndTag(); g != tt.nameAndTag { if g := p.DisplayCompact(); g != tt.nameAndTag {
t.Errorf("ModelAndTag(%q) = %q; want %q", tt.in, g, tt.nameAndTag) t.Errorf("ModelAndTag(%q) = %q; want %q", tt.in, g, tt.nameAndTag)
} }
if g := p.ModelTagAndBuild(); g != tt.nameTagAndBuild { if g := p.DisplayShort(); g != tt.nameTagAndBuild {
t.Errorf("ModelTagAndBuild(%q) = %q; want %q", tt.in, g, tt.nameTagAndBuild) t.Errorf("ModelTagAndBuild(%q) = %q; want %q", tt.in, g, tt.nameTagAndBuild)
} }
}) })
@ -166,8 +166,8 @@ func TestNameFull(t *testing.T) {
t.Run(tt.in, func(t *testing.T) { t.Run(tt.in, func(t *testing.T) {
p := ParseName(tt.in) p := ParseName(tt.in)
t.Logf("ParseName(%q) = %#v", tt.in, p) t.Logf("ParseName(%q) = %#v", tt.in, p)
if g := p.Full(); g != tt.wantFull { if g := p.DisplayFull(); g != tt.wantFull {
t.Errorf("Full(%q) = %q; want %q", tt.in, g, tt.wantFull) t.Errorf("DisplayFull(%q) = %q; want %q", tt.in, g, tt.wantFull)
} }
}) })
} }