From ab9e4765514e1ea574d6956ca6a7096e14f72ef3 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Sun, 7 Apr 2024 20:44:25 -0700 Subject: [PATCH] x/model: add MustParseName --- x/model/name.go | 8 ++++++++ x/model/name_test.go | 14 +++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/x/model/name.go b/x/model/name.go index 3fdfe30e..678288af 100644 --- a/x/model/name.go +++ b/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. diff --git a/x/model/name_test.go b/x/model/name_test.go index a6c96b18..00321915 100644 --- a/x/model/name_test.go +++ b/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" {