Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Merge to prototext.UnmarshalOptions #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion encoding/prototext/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func Unmarshal(b []byte, m proto.Message) error {
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 error if there are any missing required fields.
Expand Down Expand Up @@ -62,7 +67,9 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
// For profiling purposes, avoid changing the name of this function or
// introducing other code paths for unmarshal that do not go through this.
func (o UnmarshalOptions) unmarshal(b []byte, m proto.Message) error {
proto.Reset(m)
if !o.Merge {
proto.Reset(m)
}

if o.Resolver == nil {
o.Resolver = protoregistry.GlobalTypes
Expand Down
20 changes: 20 additions & 0 deletions encoding/prototext/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,26 @@ str_to_nested: {
inputMessage: &pb2.Nests{},
inputText: "reserved_field: 'ignore this'",
wantMessage: &pb2.Nests{},
}, {
desc: "reset message when merge is not set",
inputMessage: &pb3.Scalars{
SBool: true,
},
inputText: `s_string: "abc"`,
wantMessage: &pb3.Scalars{
SString: "abc",
},
}, {
desc: "message not reset when merge is enabled",
umo: prototext.UnmarshalOptions{Merge: true},
inputMessage: &pb3.Scalars{
SBool: true,
},
inputText: `s_string: "abc"`,
wantMessage: &pb3.Scalars{
SBool: true,
SString: "abc",
},
}, {
desc: "extensions of non-repeated fields",
inputMessage: &pb2.Extensions{},
Expand Down