proto: reset message by default in Unmarshal
We change Unmarshal to reset a message by default.
* We add a Merge option to UnmarshalOptions for explicit merging.
* We speed up Reset by checking for the Reset method.
* Remove TODOs in prototext and protojson about reset behavior.
Fixes golang/protobuf#890
Change-Id: Ibd8963c741053f564acf061fbdb846699942109c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/195457
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/proto/decode.go b/proto/decode.go
index 8974139..ab62ff3 100644
--- a/proto/decode.go
+++ b/proto/decode.go
@@ -21,6 +21,11 @@
type UnmarshalOptions struct {
pragma.NoUnkeyedLiterals
+ // Merge merges the input into the destination message.
+ // The default behavior is to always reset the message before unmarshaling,
+ // unless Merge is specified.
+ Merge bool
+
// AllowPartial accepts input for messages that will result in missing
// required fields. If AllowPartial is false (the default), Unmarshal will
// return an error if there are any missing required fields.
@@ -49,7 +54,9 @@
o.Resolver = protoregistry.GlobalTypes
}
- // TODO: Reset m?
+ if !o.Merge {
+ Reset(m)
+ }
err := o.unmarshalMessage(b, m.ProtoReflect())
if err != nil {
return err