x/model: make UnmarshalText illegal for an already valid Name
This commit is contained in:
parent
81e8c49ac2
commit
348378ef56
@ -316,7 +316,12 @@ func (r Name) MarshalText() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalText implements [encoding.TextUnmarshaler].
|
// UnmarshalText implements [encoding.TextUnmarshaler].
|
||||||
|
//
|
||||||
|
// It is an error to call UnmarshalText on a valid Name.
|
||||||
func (r *Name) UnmarshalText(text []byte) error {
|
func (r *Name) UnmarshalText(text []byte) error {
|
||||||
|
if r.Valid() {
|
||||||
|
return errors.New("model.Name: UnmarshalText on valid Name")
|
||||||
|
}
|
||||||
// unsafeString is safe here because the contract of UnmarshalText
|
// unsafeString is safe here because the contract of UnmarshalText
|
||||||
// that text belongs to us for the duration of the call.
|
// that text belongs to us for the duration of the call.
|
||||||
*r = ParseName(unsafeString(text))
|
*r = ParseName(unsafeString(text))
|
||||||
|
@ -25,6 +25,14 @@ func fieldsFromName(p Name) fields {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mustParse(s string) Name {
|
||||||
|
p := ParseName(s)
|
||||||
|
if !p.Valid() {
|
||||||
|
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"},
|
||||||
@ -374,12 +382,22 @@ func TestNameTextMarshal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNameTextUnmarshalCallOnValidName(t *testing.T) {
|
||||||
|
// UnmarshalText should not be called on a valid Name.
|
||||||
|
p := mustParse("x")
|
||||||
|
if err := p.UnmarshalText([]byte("mistral:latest+Q4_0")); err == nil {
|
||||||
|
t.Error("UnmarshalText() = nil; want error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNameTextMarshalAllocs(t *testing.T) {
|
||||||
var data []byte
|
var data []byte
|
||||||
name := ParseName("example.com/ns/mistral:latest+Q4_0")
|
name := ParseName("example.com/ns/mistral:latest+Q4_0")
|
||||||
if !name.Complete() {
|
if !name.Complete() {
|
||||||
// sanity check
|
// sanity check
|
||||||
t.Fatal("name is not complete")
|
panic("sanity check failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
allocs := testing.AllocsPerRun(1000, func() {
|
allocs := testing.AllocsPerRun(1000, func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user