encoding: switch ordering of Unmarshal arguments

While it is general convention that the receiver being mutated
is the first argument, the standard library specifically goes against
this convention when it comes to the Unmarshal function.

Switch the ordering of the Unmarshal function to match the Go stdlib.

Change-Id: I893346680233ef9fec7104415a54a0a7ae353378
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177258
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2019-05-14 16:05:06 -07:00 committed by Joe Tsai
parent 5c5b531562
commit cdb7773569
10 changed files with 31 additions and 23 deletions

View File

@ -166,7 +166,7 @@ func BenchmarkTextDecode(b *testing.B) {
if *benchV1 {
err = protoV1.UnmarshalText(string(in), m)
} else {
err = prototext.Unmarshal(m, in)
err = prototext.Unmarshal(in, m)
}
if err != nil {
b.Fatal(err)
@ -203,7 +203,7 @@ func BenchmarkJSONDecode(b *testing.B) {
if *benchV1 {
err = jsonpbV1.UnmarshalString(string(out), m)
} else {
err = protojson.Unmarshal(m, out)
err = protojson.Unmarshal(out, m)
}
if err != nil {
b.Fatal(err)

View File

@ -1,13 +1,17 @@
// Package jsonpb is deprecated.
package jsonpb
import "google.golang.org/protobuf/encoding/protojson"
var (
Marshal = protojson.Marshal
Unmarshal = protojson.Unmarshal
import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
var Marshal = protojson.Marshal
func Unmarshal(m proto.Message, b []byte) error {
return protojson.Unmarshal(b, m)
}
type (
MarshalOptions = protojson.MarshalOptions
UnmarshalOptions = protojson.UnmarshalOptions

View File

@ -15,7 +15,7 @@ func BenchmarkUnmarshal_Duration(b *testing.B) {
input := []byte(`"-123456789.123456789s"`)
for i := 0; i < b.N; i++ {
err := protojson.Unmarshal(&knownpb.Duration{}, input)
err := protojson.Unmarshal(input, &knownpb.Duration{})
if err != nil {
b.Fatal(err)
}

View File

@ -21,8 +21,8 @@ import (
)
// Unmarshal reads the given []byte into the given proto.Message.
func Unmarshal(m proto.Message, b []byte) error {
return UnmarshalOptions{}.Unmarshal(m, b)
func Unmarshal(b []byte, m proto.Message) error {
return UnmarshalOptions{}.Unmarshal(b, m)
}
// UnmarshalOptions is a configurable JSON format parser.
@ -48,7 +48,7 @@ type UnmarshalOptions struct {
// options in UnmarshalOptions object. It will clear the message first before
// setting the fields. If it returns an error, the given message may be
// partially set.
func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
mr := m.ProtoReflect()
// TODO: Determine if we would like to have an option for merging or only
// have merging behavior. We should at least be consistent with textproto

View File

@ -2596,7 +2596,7 @@ func TestUnmarshal(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
err := tt.umo.Unmarshal(tt.inputMessage, []byte(tt.inputText))
err := tt.umo.Unmarshal([]byte(tt.inputText), tt.inputMessage)
if err != nil && !tt.wantErr {
t.Errorf("Unmarshal() returned error: %v\n\n", err)
}

View File

@ -20,8 +20,8 @@ import (
)
// Unmarshal reads the given []byte into the given proto.Message.
func Unmarshal(m proto.Message, b []byte) error {
return UnmarshalOptions{}.Unmarshal(m, b)
func Unmarshal(b []byte, m proto.Message) error {
return UnmarshalOptions{}.Unmarshal(b, m)
}
// UnmarshalOptions is a configurable textproto format unmarshaler.
@ -41,7 +41,7 @@ type UnmarshalOptions struct {
// Unmarshal reads the given []byte and populates the given proto.Message using options in
// UnmarshalOptions object.
func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
var nerr errors.NonFatal
mr := m.ProtoReflect()

View File

@ -1523,7 +1523,7 @@ type_url: "pb2.Nested"
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
err := tt.umo.Unmarshal(tt.inputMessage, []byte(tt.inputText))
err := tt.umo.Unmarshal([]byte(tt.inputText), tt.inputMessage)
if err != nil && !tt.wantErr {
t.Errorf("Unmarshal() returned error: %v\n\n", err)
}

View File

@ -226,7 +226,7 @@ func TestRoundTrip(t *testing.T) {
}
gotMessage := new(pb2.KnownTypes)
err = prototext.UnmarshalOptions{Resolver: tt.resolver}.Unmarshal(gotMessage, b)
err = prototext.UnmarshalOptions{Resolver: tt.resolver}.Unmarshal(b, gotMessage)
if err != nil {
t.Errorf("Unmarshal() returned error: %v\n\n", err)
}

View File

@ -1,13 +1,17 @@
// Package textpb is deprecated.
package textpb
import "google.golang.org/protobuf/encoding/prototext"
var (
Marshal = prototext.Marshal
Unmarshal = prototext.Unmarshal
import (
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
var Marshal = prototext.Marshal
func Unmarshal(m proto.Message, b []byte) error {
return prototext.Unmarshal(b, m)
}
type (
MarshalOptions = prototext.MarshalOptions
UnmarshalOptions = prototext.UnmarshalOptions

View File

@ -71,7 +71,7 @@ func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
case *pb.ConformanceRequest_JsonPayload:
err = protojson.UnmarshalOptions{
DiscardUnknown: req.TestCategory == pb.TestCategory_JSON_IGNORE_UNKNOWN_PARSING_TEST,
}.Unmarshal(msg, []byte(p.JsonPayload))
}.Unmarshal([]byte(p.JsonPayload), msg)
default:
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_RuntimeError{