reflect/protoreflect: replace Mutable with NewMessage

Remove the Mutable methods from KnownFields, List, and Map, replacing
them with methods which return a new, empty message value without adding
that value to the collection.

The new API is simpler, since it clearly applies only to message values,
and more orthogonal, since it provides a way to create a value without
mutating the collection. This latter point is particularly useful in
map deserialization, where the key may be unknown at the time the value
is deserialized.

Drop the Mutable interface, since it is no longer necessary.

Change-Id: Ic5f3d06a2aa331a5d5cd2b4e670a3dba4a74f77c
Reviewed-on: https://go-review.googlesource.com/c/153278
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/message_field.go b/internal/impl/message_field.go
index c55cbc8..06e571e 100644
--- a/internal/impl/message_field.go
+++ b/internal/impl/message_field.go
@@ -16,11 +16,11 @@
 type fieldInfo struct {
 	// TODO: specialize marshal and unmarshal functions?
 
-	has     func(pointer) bool
-	get     func(pointer) pref.Value
-	set     func(pointer, pref.Value)
-	clear   func(pointer)
-	mutable func(pointer) pref.Mutable
+	has        func(pointer) bool
+	get        func(pointer) pref.Value
+	set        func(pointer, pref.Value)
+	clear      func(pointer)
+	newMessage func() pref.Message
 }
 
 func fieldInfoForWeak(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
@@ -86,18 +86,9 @@
 			}
 			rv.Set(reflect.Zero(rv.Type()))
 		},
-		mutable: func(p pointer) pref.Mutable {
-			// Mutable is only valid for messages and panics for other kinds.
-			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
-			if rv.IsNil() || rv.Elem().Type().Elem() != ot {
-				rv.Set(reflect.New(ot))
-			}
-			rv = rv.Elem().Elem().Field(0)
-			if rv.IsNil() {
-				pv := pref.ValueOf(conv.MessageType.New().ProtoReflect())
-				rv.Set(conv.GoValueOf(pv))
-			}
-			return conv.PBValueOf(rv).Message()
+		newMessage: func() pref.Message {
+			// This is only valid for messages and panics for other kinds.
+			return conv.MessageType.New().ProtoReflect()
 		},
 	}
 }
@@ -135,10 +126,6 @@
 			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
 			rv.Set(reflect.Zero(rv.Type()))
 		},
-		mutable: func(p pointer) pref.Mutable {
-			v := p.Apply(fieldOffset).AsIfaceOf(fs.Type)
-			return pvalue.MapOf(v, keyConv, valConv)
-		},
 	}
 }
 
@@ -174,10 +161,6 @@
 			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
 			rv.Set(reflect.Zero(rv.Type()))
 		},
-		mutable: func(p pointer) pref.Mutable {
-			v := p.Apply(fieldOffset).AsIfaceOf(fs.Type)
-			return pvalue.ListOf(v, conv)
-		},
 	}
 }
 
@@ -253,9 +236,6 @@
 			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
 			rv.Set(reflect.Zero(rv.Type()))
 		},
-		mutable: func(p pointer) pref.Mutable {
-			panic("invalid mutable call")
-		},
 	}
 }
 
@@ -293,14 +273,8 @@
 			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
 			rv.Set(reflect.Zero(rv.Type()))
 		},
-		mutable: func(p pointer) pref.Mutable {
-			// Mutable is only valid for messages and panics for other kinds.
-			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
-			if rv.IsNil() {
-				pv := pref.ValueOf(conv.MessageType.New().ProtoReflect())
-				rv.Set(conv.GoValueOf(pv))
-			}
-			return conv.PBValueOf(rv).Message()
+		newMessage: func() pref.Message {
+			return conv.MessageType.New().ProtoReflect()
 		},
 	}
 }