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>
diff --git a/encoding/textpb/decode.go b/encoding/textpb/decode.go
index a621429..e545911 100644
--- a/encoding/textpb/decode.go
+++ b/encoding/textpb/decode.go
@@ -8,12 +8,10 @@
"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 @@
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
}
diff --git a/encoding/textpb/decode_test.go b/encoding/textpb/decode_test.go
index bbf99bf..3737f91 100644
--- a/encoding/textpb/decode_test.go
+++ b/encoding/textpb/decode_test.go
@@ -1327,8 +1327,7 @@
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)
}
diff --git a/encoding/textpb/encode_test.go b/encoding/textpb/encode_test.go
index 3430fc5..6a6cd7a 100644
--- a/encoding/textpb/encode_test.go
+++ b/encoding/textpb/encode_test.go
@@ -1029,8 +1029,7 @@
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 @@
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)
}
diff --git a/encoding/textpb/other_test.go b/encoding/textpb/other_test.go
index e20e005..d9118a7 100644
--- a/encoding/textpb/other_test.go
+++ b/encoding/textpb/other_test.go
@@ -151,8 +151,7 @@
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 @@
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 @@
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 @@
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)
}