فهرست منبع

encoding/textpb: reduce dependency on proto v1 package

Substitute protoV1.Marshal calls to use proto v2.

A few tests still need to use v1 due to missing required field checks in
v2.

Change-Id: I97ee208b81afc85c81142e70c1d7bdc47db76ce4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/167774
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Herbie Ong 7 سال پیش
والد
کامیت
4d1c3be76f
4فایلهای تغییر یافته به همراه8 افزوده شده و 24 حذف شده
  1. 1 10
      encoding/textpb/decode.go
  2. 1 2
      encoding/textpb/decode_test.go
  3. 2 4
      encoding/textpb/encode_test.go
  4. 4 8
      encoding/textpb/other_test.go

+ 1 - 10
encoding/textpb/decode.go

@@ -8,12 +8,10 @@ import (
 	"fmt"
 	"strings"
 
-	protoV1 "github.com/golang/protobuf/proto"
 	"github.com/golang/protobuf/v2/internal/encoding/text"
 	"github.com/golang/protobuf/v2/internal/errors"
 	"github.com/golang/protobuf/v2/internal/pragma"
 	"github.com/golang/protobuf/v2/internal/set"
-	pvalue "github.com/golang/protobuf/v2/internal/value"
 	"github.com/golang/protobuf/v2/proto"
 	pref "github.com/golang/protobuf/v2/reflect/protoreflect"
 	"github.com/golang/protobuf/v2/reflect/protoregistry"
@@ -488,14 +486,7 @@ func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, knownFields pref.Kn
 		return err
 	}
 	// Serialize the embedded message and assign the resulting bytes to the value field.
-	// TODO: Switch to V2 marshal and enable deterministic option when ready.
-	var mv1 protoV1.Message
-	if mtmp, ok := m.(pvalue.Unwrapper); ok {
-		mv1 = mtmp.ProtoUnwrap().(protoV1.Message)
-	} else {
-		mv1 = m.Interface().(protoV1.Message)
-	}
-	b, err := protoV1.Marshal(mv1)
+	b, err := proto.Marshal(m.Interface())
 	if !nerr.Merge(err) {
 		return err
 	}

+ 1 - 2
encoding/textpb/decode_test.go

@@ -1327,8 +1327,7 @@ value: "some bytes"
 					OptString: scalar.String("inception"),
 				},
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b, err := protoV1.Marshal(m)
+			b, err := proto.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}

+ 2 - 4
encoding/textpb/encode_test.go

@@ -1029,8 +1029,7 @@ opt_int32: 42
 					OptString: scalar.String("inception"),
 				},
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b, err := protoV1.Marshal(m)
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
@@ -1054,8 +1053,7 @@ value: "\n\x13embedded inside Any\x12\x0b\n\tinception"
 					OptString: scalar.String("inception"),
 				},
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b, err := protoV1.Marshal(m)
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}

+ 4 - 8
encoding/textpb/other_test.go

@@ -151,8 +151,7 @@ func TestRoundTrip(t *testing.T) {
 					OptString: scalar.String("inception"),
 				},
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b, err := protoV1.Marshal(m)
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
@@ -173,8 +172,7 @@ func TestRoundTrip(t *testing.T) {
 					OptString: scalar.String("inception"),
 				},
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b, err := protoV1.Marshal(m)
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
@@ -196,8 +194,7 @@ func TestRoundTrip(t *testing.T) {
 			m1 := &pb2.Nested{
 				OptString: scalar.String("message inside Any of another Any field"),
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b1, err := protoV1.Marshal(m1)
+			b1, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}
@@ -205,8 +202,7 @@ func TestRoundTrip(t *testing.T) {
 				TypeUrl: "pb2.Nested",
 				Value:   b1,
 			}
-			// TODO: Switch to V2 marshal when ready.
-			b2, err := protoV1.Marshal(m2)
+			b2, err := proto.MarshalOptions{Deterministic: true}.Marshal(m2)
 			if err != nil {
 				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
 			}