瀏覽代碼

x/model: more comments on UnmarshalText and immutability

Blake Mizerany 1 年之前
父節點
當前提交
5615f60bb0
共有 1 個文件被更改,包括 4 次插入0 次删除
  1. 4 0
      x/model/name.go

+ 4 - 0
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.
 // 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() {
 	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")
 		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