Parcourir la source

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>
Joe Tsai il y a 6 ans
Parent
commit
cdb7773569

+ 2 - 2
encoding/bench_test.go

@@ -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)

+ 9 - 5
encoding/jsonpb/jsonpb.go

@@ -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

+ 1 - 1
encoding/protojson/bench_test.go

@@ -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)
 		}

+ 3 - 3
encoding/protojson/decode.go

@@ -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

+ 1 - 1
encoding/protojson/decode_test.go

@@ -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)
 			}

+ 3 - 3
encoding/prototext/decode.go

@@ -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()

+ 1 - 1
encoding/prototext/decode_test.go

@@ -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)
 			}

+ 1 - 1
encoding/prototext/other_test.go

@@ -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)
 			}

+ 9 - 5
encoding/textpb/textpb.go

@@ -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

+ 1 - 1
internal/cmd/conformance/main.go

@@ -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{