goprotobuf: Make Unmarshal reset the message before populating.
R=r
CC=golang-dev
http://codereview.appspot.com/6351110
diff --git a/proto/decode.go b/proto/decode.go
index 215df16..c5b1ad7 100644
--- a/proto/decode.go
+++ b/proto/decode.go
@@ -294,15 +294,11 @@
// decoded result in pb. If the struct underlying pb does not match
// the data in buf, the results can be unpredictable.
//
-// NOTE: As of 2012-06-29, Unmarshal does not and has never reset
-// the protocol message before unmarshaling, so reusing a message
-// for multiple unmarshals appends to repeated fields and so on.
-// We plan to change this, so that Unmarshal can be relied upon to
-// call pb.Reset before starting, as most callers expect.
-// Code that intends the appending behavior must switch to UnmarshalAppend.
+// Unmarshal resets pb before starting to unmarshal, so any
+// existing data in pb is always removed. Use UnmarshalAppend
+// to preserve and append to existing data.
func Unmarshal(buf []byte, pb Message) error {
- // TODO: Remove NOTE above and uncomment next line.
- // pb.Reset()
+ pb.Reset()
return UnmarshalAppend(buf, pb)
}
@@ -310,10 +306,8 @@
// writes the decoded result to pb. If the struct underlying pb does not match
// the data in buf, the results can be unpredictable.
//
-// NOTE: As of 2012-06-29, Unmarshal and UnmarshalAppend are the same,
-// but we plan to change this, so that Unmarshal resets pb before unmarshaling,
-// while UnmarshalAppend does not. (Right now both do not.)
-// Use UnmarshalAppend if you want to append to pb instead of replacing it.
+// UnmarshalAppend preserves and appends to existing data in pb.
+// Most code should use Unmarshal instead.
func UnmarshalAppend(buf []byte, pb Message) error {
// If the object can unmarshal itself, let it.
if u, ok := pb.(Unmarshaler); ok {