From 5615f60bb05d687df842f002ffb8c2ebb063d50b Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Sat, 6 Apr 2024 21:56:28 -0700 Subject: [PATCH] x/model: more comments on UnmarshalText and immutability --- x/model/name.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/model/name.go b/x/model/name.go index 0319c7ea6..1a4fa4d42 100644 --- a/x/model/name.go +++ b/x/model/name.go @@ -320,6 +320,10 @@ func (r Name) MarshalText() ([]byte, error) { // It is an error to call UnmarshalText on a valid Name. func (r *Name) UnmarshalText(text []byte) error { if r.Valid() { + // The invariant of UnmarshalText is that it should only be + // called on an invalid/zero Name. If we allow UnmarshalText + // on a valid Name, then the Name will be mutated, breaking + // the immutability of the Name. return errors.New("model.Name: UnmarshalText on valid Name") } // unsafeString is safe here because the contract of UnmarshalText