From 8d30bbeede7d1e743d7d5c6cc87bd0169abce92a Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Thu, 16 May 2019 15:53:25 -0700 Subject: [PATCH] all: remove dependency on proto v1 This does not remove all dependencies, but all of the cases where it can now be implemented in terms of v2. Change-Id: Idc5b0273f0d35c284bf2141eb9cce998692ceb15 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/184878 Reviewed-by: Herbie Ong --- cmd/protoc-gen-go/annotation_test.go | 8 ++--- compiler/protogen/protogen.go | 2 +- encoding/protojson/decode_test.go | 3 +- encoding/prototext/decode_test.go | 3 +- encoding/prototext/other_test.go | 3 +- internal/filedesc/build_test.go | 2 +- proto/decode_test.go | 45 ++++++++++++++++------------ proto/encode_test.go | 5 ++-- proto/equal_test.go | 6 ++-- 9 files changed, 40 insertions(+), 37 deletions(-) diff --git a/cmd/protoc-gen-go/annotation_test.go b/cmd/protoc-gen-go/annotation_test.go index 233163d6..ea7f1364 100644 --- a/cmd/protoc-gen-go/annotation_test.go +++ b/cmd/protoc-gen-go/annotation_test.go @@ -9,9 +9,10 @@ import ( "io/ioutil" "testing" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/internal/fieldnum" "google.golang.org/protobuf/internal/scalar" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/descriptorpb" ) @@ -26,7 +27,7 @@ func TestAnnotations(t *testing.T) { t.Fatal(err) } gotInfo := &descriptorpb.GeneratedCodeInfo{} - if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil { + if err := prototext.Unmarshal(metaFile, gotInfo); err != nil { t.Fatalf("can't parse meta file: %v", err) } @@ -66,7 +67,6 @@ func TestAnnotations(t *testing.T) { }) } if !proto.Equal(gotInfo, wantInfo) { - t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v", - proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo)) + t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v", gotInfo, wantInfo) } } diff --git a/compiler/protogen/protogen.go b/compiler/protogen/protogen.go index 09264e65..0191e09b 100644 --- a/compiler/protogen/protogen.go +++ b/compiler/protogen/protogen.go @@ -216,7 +216,7 @@ func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) { // .proto source file specifying the full import path of the Go package // associated with this file. // - // option go_package = "github.com/golang/protobuf/ptypes/any"; + // option go_package = "google.golang.org/protobuf/types/known/anypb"; // // Build systems which want to exert full control over import paths may // specify M= flags. diff --git a/encoding/protojson/decode_test.go b/encoding/protojson/decode_test.go index 0de7d7f2..5797a8bd 100644 --- a/encoding/protojson/decode_test.go +++ b/encoding/protojson/decode_test.go @@ -8,7 +8,6 @@ import ( "math" "testing" - protoV1 "github.com/golang/protobuf/proto" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/testprotos/pb2" "google.golang.org/protobuf/encoding/testprotos/pb3" @@ -2483,7 +2482,7 @@ func TestUnmarshal(t *testing.T) { if err == nil && tt.wantErr { t.Error("Unmarshal() got nil error, want error\n\n") } - if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage.(protoV1.Message), tt.wantMessage.(protoV1.Message)) { + if tt.wantMessage != nil && !proto.Equal(tt.inputMessage, tt.wantMessage) { t.Errorf("Unmarshal()\n\n%v\n\n%v\n", tt.inputMessage, tt.wantMessage) } }) diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go index 7f0a4e86..799de2eb 100644 --- a/encoding/prototext/decode_test.go +++ b/encoding/prototext/decode_test.go @@ -8,7 +8,6 @@ import ( "math" "testing" - protoV1 "github.com/golang/protobuf/proto" "google.golang.org/protobuf/encoding/prototext" pimpl "google.golang.org/protobuf/internal/impl" "google.golang.org/protobuf/internal/scalar" @@ -1498,7 +1497,7 @@ type_url: "pb2.Nested" if err == nil && tt.wantErr { t.Error("Unmarshal() got nil error, want error\n\n") } - if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage.(protoV1.Message), tt.wantMessage.(protoV1.Message)) { + if tt.wantMessage != nil && !proto.Equal(tt.inputMessage, tt.wantMessage) { t.Errorf("Unmarshal()\n\n%v\n\n%v\n", tt.inputMessage, tt.wantMessage) } }) diff --git a/encoding/prototext/other_test.go b/encoding/prototext/other_test.go index ec1842f3..5b3376b6 100644 --- a/encoding/prototext/other_test.go +++ b/encoding/prototext/other_test.go @@ -7,7 +7,6 @@ package prototext_test import ( "testing" - protoV1 "github.com/golang/protobuf/proto" "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/internal/impl" pimpl "google.golang.org/protobuf/internal/impl" @@ -240,7 +239,7 @@ func TestRoundTrip(t *testing.T) { t.Errorf("Unmarshal() returned error: %v\n\n", err) } - if !protoV1.Equal(gotMessage, tt.message.(protoV1.Message)) { + if !proto.Equal(gotMessage, tt.message) { t.Errorf("Unmarshal()\n\n%v\n\n%v\n", gotMessage, tt.message) } }) diff --git a/internal/filedesc/build_test.go b/internal/filedesc/build_test.go index edeb56e3..3a4f5aeb 100644 --- a/internal/filedesc/build_test.go +++ b/internal/filedesc/build_test.go @@ -10,7 +10,7 @@ import ( "io/ioutil" "testing" - proto "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protodesc" "google.golang.org/protobuf/reflect/protoreflect" diff --git a/proto/decode_test.go b/proto/decode_test.go index b2b6227e..3b919aa9 100644 --- a/proto/decode_test.go +++ b/proto/decode_test.go @@ -135,22 +135,22 @@ var testProtos = []testProto{ OptionalNestedEnum: test3pb.TestAllTypes_BAR, }, build( &testpb.TestAllExtensions{}, - extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)), - extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)), - extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)), - extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)), - extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)), - extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)), - extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)), - extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)), - extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)), - extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)), - extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)), - extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)), - extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)), - extend(testpb.E_OptionalStringExtension, scalar.String("string")), + extend(testpb.E_OptionalInt32Extension, int32(1001)), + extend(testpb.E_OptionalInt64Extension, int64(1002)), + extend(testpb.E_OptionalUint32Extension, uint32(1003)), + extend(testpb.E_OptionalUint64Extension, uint64(1004)), + extend(testpb.E_OptionalSint32Extension, int32(1005)), + extend(testpb.E_OptionalSint64Extension, int64(1006)), + extend(testpb.E_OptionalFixed32Extension, uint32(1007)), + extend(testpb.E_OptionalFixed64Extension, uint64(1008)), + extend(testpb.E_OptionalSfixed32Extension, int32(1009)), + extend(testpb.E_OptionalSfixed64Extension, int64(1010)), + extend(testpb.E_OptionalFloatExtension, float32(1011.5)), + extend(testpb.E_OptionalDoubleExtension, float64(1012.5)), + extend(testpb.E_OptionalBoolExtension, bool(true)), + extend(testpb.E_OptionalStringExtension, string("string")), extend(testpb.E_OptionalBytesExtension, []byte("bytes")), - extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()), + extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR), )}, wire: pack.Message{ pack.Tag{1, pack.VarintType}, pack.Varint(1001), @@ -1370,14 +1370,21 @@ func unknown(raw pref.RawFields) buildOpt { } func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt { + // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T? + t := reflect.TypeOf(value) + if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 { + v := reflect.New(t) + v.Elem().Set(reflect.ValueOf(value)) + value = v.Interface() + } + return func(m proto.Message) { - if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil { - panic(err) - } + xt := desc.Type + m.ProtoReflect().Set(xt, xt.ValueOf(value)) } } func marshalText(m proto.Message) string { - b, _ := prototext.Marshal(m) + b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m) return string(b) } diff --git a/proto/encode_test.go b/proto/encode_test.go index aba6830c..cd4a7ef8 100644 --- a/proto/encode_test.go +++ b/proto/encode_test.go @@ -9,7 +9,6 @@ import ( "fmt" "testing" - protoV1 "github.com/golang/protobuf/proto" "github.com/google/go-cmp/cmp" "google.golang.org/protobuf/proto" @@ -38,7 +37,7 @@ func TestEncode(t *testing.T) { AllowPartial: test.partial, } if err := uopts.Unmarshal(wire, got); err != nil { - t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message))) + t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) return } @@ -47,7 +46,7 @@ func TestEncode(t *testing.T) { return } if !proto.Equal(got, want) { - t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message))) + t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) } }) } diff --git a/proto/equal_test.go b/proto/equal_test.go index 99f1720a..568312fd 100644 --- a/proto/equal_test.go +++ b/proto/equal_test.go @@ -393,16 +393,16 @@ var inequalities = []struct{ a, b proto.Message }{ // Extensions. { build(&testpb.TestAllExtensions{}, - extend(testpb.E_OptionalInt32Extension, scalar.Int32(1)), + extend(testpb.E_OptionalInt32Extension, int32(1)), ), build(&testpb.TestAllExtensions{}, - extend(testpb.E_OptionalInt32Extension, scalar.Int32(2)), + extend(testpb.E_OptionalInt32Extension, int32(2)), ), }, { &testpb.TestAllExtensions{}, build(&testpb.TestAllExtensions{}, - extend(testpb.E_OptionalInt32Extension, scalar.Int32(2)), + extend(testpb.E_OptionalInt32Extension, int32(2)), ), }, // Proto2 default values are not considered by Equal, so the following are still unequal.