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>
This commit is contained in:
Herbie Ong 2019-03-15 18:22:36 -07:00
parent c96a79da29
commit 4d1c3be76f
4 changed files with 8 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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