فهرست منبع

x/model: add MustParseName

Blake Mizerany 1 سال پیش
والد
کامیت
ab9e476551
2فایلهای تغییر یافته به همراه11 افزوده شده و 11 حذف شده
  1. 8 0
      x/model/name.go
  2. 3 11
      x/model/name_test.go

+ 8 - 0
x/model/name.go

@@ -152,6 +152,14 @@ func ParseName(s string) Name {
 	return Name{}
 }
 
+func MustParseName(s string) Name {
+	r := ParseName(s)
+	if !r.IsValid() {
+		panic("model.MustParseName: invalid name: " + s)
+	}
+	return r
+}
+
 // Fill fills in the missing parts of dst with the parts of src.
 //
 // The returned Name will only be valid if dst is valid.

+ 3 - 11
x/model/name_test.go

@@ -27,14 +27,6 @@ func fieldsFromName(p Name) fields {
 	}
 }
 
-func mustParse(s string) Name {
-	p := ParseName(s)
-	if !p.IsValid() {
-		panic(fmt.Sprintf("invalid name: %q", s))
-	}
-	return p
-}
-
 var testNames = map[string]fields{
 	"mistral:latest":                 {model: "mistral", tag: "latest"},
 	"mistral":                        {model: "mistral"},
@@ -419,7 +411,7 @@ func TestNameTextMarshal(t *testing.T) {
 
 	t.Run("UnmarshalText into valid Name", func(t *testing.T) {
 		// UnmarshalText should not be called on a valid Name.
-		p := mustParse("x")
+		p := MustParseName("x")
 		if err := p.UnmarshalText([]byte("mistral:latest+Q4_0")); err == nil {
 			t.Error("UnmarshalText() = nil; want error")
 		}
@@ -455,7 +447,7 @@ func TestNameTextMarshal(t *testing.T) {
 
 func TestSQL(t *testing.T) {
 	t.Run("Scan for already valid Name", func(t *testing.T) {
-		p := mustParse("x")
+		p := MustParseName("x")
 		if err := p.Scan("mistral:latest+Q4_0"); err == nil {
 			t.Error("Scan() = nil; want error")
 		}
@@ -470,7 +462,7 @@ func TestSQL(t *testing.T) {
 		}
 	})
 	t.Run("Value", func(t *testing.T) {
-		p := mustParse("x")
+		p := MustParseName("x")
 		if g, err := p.Value(); err != nil {
 			t.Errorf("Value() error = %v; want nil", err)
 		} else if g != "x" {