x/model: add MustParseName
This commit is contained in:
parent
d721228a2b
commit
ab9e476551
@ -152,6 +152,14 @@ func ParseName(s string) Name {
|
|||||||
return 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.
|
// Fill fills in the missing parts of dst with the parts of src.
|
||||||
//
|
//
|
||||||
// The returned Name will only be valid if dst is valid.
|
// The returned Name will only be valid if dst is valid.
|
||||||
|
@ -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{
|
var testNames = map[string]fields{
|
||||||
"mistral:latest": {model: "mistral", tag: "latest"},
|
"mistral:latest": {model: "mistral", tag: "latest"},
|
||||||
"mistral": {model: "mistral"},
|
"mistral": {model: "mistral"},
|
||||||
@ -419,7 +411,7 @@ func TestNameTextMarshal(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("UnmarshalText into valid Name", func(t *testing.T) {
|
t.Run("UnmarshalText into valid Name", func(t *testing.T) {
|
||||||
// UnmarshalText should not be called on a valid Name.
|
// 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 {
|
if err := p.UnmarshalText([]byte("mistral:latest+Q4_0")); err == nil {
|
||||||
t.Error("UnmarshalText() = nil; want error")
|
t.Error("UnmarshalText() = nil; want error")
|
||||||
}
|
}
|
||||||
@ -455,7 +447,7 @@ func TestNameTextMarshal(t *testing.T) {
|
|||||||
|
|
||||||
func TestSQL(t *testing.T) {
|
func TestSQL(t *testing.T) {
|
||||||
t.Run("Scan for already valid Name", func(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 {
|
if err := p.Scan("mistral:latest+Q4_0"); err == nil {
|
||||||
t.Error("Scan() = nil; want error")
|
t.Error("Scan() = nil; want error")
|
||||||
}
|
}
|
||||||
@ -470,7 +462,7 @@ func TestSQL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("Value", func(t *testing.T) {
|
t.Run("Value", func(t *testing.T) {
|
||||||
p := mustParse("x")
|
p := MustParseName("x")
|
||||||
if g, err := p.Value(); err != nil {
|
if g, err := p.Value(); err != nil {
|
||||||
t.Errorf("Value() error = %v; want nil", err)
|
t.Errorf("Value() error = %v; want nil", err)
|
||||||
} else if g != "x" {
|
} else if g != "x" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user