Browse Source

x/model: rename func for display with Display prefix

Blake Mizerany 1 năm trước cách đây
mục cha
commit
bf8e0c09c9
2 tập tin đã thay đổi với 59 bổ sung21 xóa
  1. 55 17
      x/model/name.go
  2. 4 4
      x/model/name_test.go

+ 55 - 17
x/model/name.go

@@ -168,26 +168,64 @@ func (r Name) MapHash() uint64 {
 // Format returns a string representation of the ref with the given
 // concreteness. If a part is missing, it is replaced with a loud
 // placeholder.
-func (r Name) Full() string {
-	r.host = cmp.Or(r.host, "!(MISSING DOMAIN)")
-	r.namespace = cmp.Or(r.namespace, "!(MISSING NAMESPACE)")
-	r.model = cmp.Or(r.model, "!(MISSING NAME)")
-	r.tag = cmp.Or(r.tag, "!(MISSING TAG)")
-	r.build = cmp.Or(r.build, "!(MISSING BUILD)")
-	return r.String()
+func (r Name) DisplayFull() string {
+	return (Name{
+		host:      cmp.Or(r.host, "!(MISSING DOMAIN)"),
+		namespace: cmp.Or(r.namespace, "!(MISSING NAMESPACE)"),
+		model:     cmp.Or(r.model, "!(MISSING NAME)"),
+		tag:       cmp.Or(r.tag, "!(MISSING TAG)"),
+		build:     cmp.Or(r.build, "!(MISSING BUILD)"),
+	}).String()
 }
 
-func (r Name) ModelAndTag() string {
-	r.host = ""
-	r.namespace = ""
-	r.build = ""
-	return r.String()
+func (r Name) DisplayModel() string {
+	return r.model
 }
 
-func (r Name) ModelTagAndBuild() string {
-	r.host = ""
-	r.namespace = ""
-	return r.String()
+func (r Name) Has(kind NamePart) bool {
+	switch kind {
+	case Host:
+		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.
@@ -340,7 +378,7 @@ func NameParts(s string) iter.Seq2[NamePart, string] {
 func (r Name) Valid() bool {
 	// Parts ensures we only have valid parts, so no need to validate
 	// 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_\.-]

+ 4 - 4
x/model/name_test.go

@@ -131,10 +131,10 @@ func TestNameStringVariants(t *testing.T) {
 		t.Run(tt.in, func(t *testing.T) {
 			p := ParseName(tt.in)
 			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)
 			}
-			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)
 			}
 		})
@@ -166,8 +166,8 @@ func TestNameFull(t *testing.T) {
 		t.Run(tt.in, func(t *testing.T) {
 			p := ParseName(tt.in)
 			t.Logf("ParseName(%q) = %#v", tt.in, p)
-			if g := p.Full(); g != tt.wantFull {
-				t.Errorf("Full(%q) = %q; want %q", tt.in, g, tt.wantFull)
+			if g := p.DisplayFull(); g != tt.wantFull {
+				t.Errorf("DisplayFull(%q) = %q; want %q", tt.in, g, tt.wantFull)
 			}
 		})
 	}