Procházet zdrojové kódy

internal/protolegacy: add stub v1 proto package for testing purposes

The protolegacy package is a minimal version of the v1 proto package.
This allows us to use this stub version as the dependency for
internal/testprotos/legacy packages and avoid a dependency
on the real v1 proto package.

The implementation of most v1 functionality will panic if called.
This way, we know if we the v2 code depends on one of those
unimplemented functions.

Updates golang/protobuf#962

Change-Id: I20b4091706fd456e4b01ae0931cce30a872639b0
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/199297
Reviewed-by: Damien Neil <dneil@google.com>
Joe Tsai před 6 roky
rodič
revize
8e9d5f6e8a

+ 125 - 0
internal/protolegacy/proto.go

@@ -0,0 +1,125 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE.md file.
+
+// Package protolegacy is a stub version of the v1 proto package
+// to satisfy internal/testprotos/legacy dependencies.
+package protolegacy
+
+import (
+	"bytes"
+	"compress/gzip"
+	"errors"
+	"fmt"
+	"io/ioutil"
+
+	"google.golang.org/protobuf/reflect/protoreflect"
+	"google.golang.org/protobuf/reflect/protoregistry"
+	"google.golang.org/protobuf/runtime/protoiface"
+	"google.golang.org/protobuf/runtime/protoimpl"
+)
+
+const (
+	ProtoPackageIsVersion1 = true
+	ProtoPackageIsVersion2 = true
+	ProtoPackageIsVersion3 = true
+)
+
+const (
+	WireVarint     = 0
+	WireFixed32    = 5
+	WireFixed64    = 1
+	WireBytes      = 2
+	WireStartGroup = 3
+	WireEndGroup   = 4
+)
+
+type (
+	Message                = protoiface.MessageV1
+	ExtensionRange         = protoiface.ExtensionRangeV1
+	ExtensionDesc          = protoimpl.ExtensionInfo
+	Extension              = protoimpl.ExtensionFieldV1
+	XXX_InternalExtensions = protoimpl.ExtensionFields
+)
+
+func RegisterFile(s string, d []byte) {
+	// Decompress the descriptor.
+	zr, err := gzip.NewReader(bytes.NewReader(d))
+	if err != nil {
+		panic(fmt.Sprintf("proto: invalid compressed file descriptor: %v", err))
+	}
+	b, err := ioutil.ReadAll(zr)
+	if err != nil {
+		panic(fmt.Sprintf("proto: invalid compressed file descriptor: %v", err))
+	}
+
+	// Construct a protoreflect.FileDescriptor from the raw descriptor.
+	// Note that DescBuilder.Build automatically registers the constructed
+	// file descriptor with the v2 registry.
+	protoimpl.DescBuilder{RawDescriptor: b}.Build()
+}
+
+func RegisterType(m Message, s string) {
+	mt := protoimpl.X.LegacyMessageTypeOf(m, protoreflect.FullName(s))
+	if err := protoregistry.GlobalTypes.Register(mt); err != nil {
+		panic(err)
+	}
+}
+
+func RegisterMapType(interface{}, string) {
+	// Do nothing.
+}
+
+func RegisterEnum(string, map[int32]string, map[string]int32) {
+	// Do nothing.
+}
+
+func RegisterExtension(d *ExtensionDesc) {
+	if err := protoregistry.GlobalTypes.Register(d); err != nil {
+		panic(err)
+	}
+}
+
+var ErrInternalBadWireType = errors.New("not implemented")
+
+func Size(Message) int                { panic("not implemented") }
+func Marshal(Message) ([]byte, error) { panic("not implemented") }
+func Unmarshal([]byte, Message) error { panic("not implemented") }
+
+func SizeVarint(uint64) int             { panic("not implemented") }
+func EncodeVarint(uint64) []byte        { panic("not implemented") }
+func DecodeVarint([]byte) (uint64, int) { panic("not implemented") }
+
+func CompactTextString(Message) string                                  { panic("not implemented") }
+func EnumName(map[int32]string, int32) string                           { panic("not implemented") }
+func UnmarshalJSONEnum(map[string]int32, []byte, string) (int32, error) { panic("not implemented") }
+
+type Buffer struct{}
+
+func (*Buffer) DecodeFixed32() (uint64, error)      { panic("not implemented") }
+func (*Buffer) DecodeFixed64() (uint64, error)      { panic("not implemented") }
+func (*Buffer) DecodeGroup(Message) error           { panic("not implemented") }
+func (*Buffer) DecodeMessage(Message) error         { panic("not implemented") }
+func (*Buffer) DecodeRawBytes(bool) ([]byte, error) { panic("not implemented") }
+func (*Buffer) DecodeStringBytes() (string, error)  { panic("not implemented") }
+func (*Buffer) DecodeVarint() (uint64, error)       { panic("not implemented") }
+func (*Buffer) DecodeZigzag32() (uint64, error)     { panic("not implemented") }
+func (*Buffer) DecodeZigzag64() (uint64, error)     { panic("not implemented") }
+func (*Buffer) EncodeFixed32(uint64) error          { panic("not implemented") }
+func (*Buffer) EncodeFixed64(uint64) error          { panic("not implemented") }
+func (*Buffer) EncodeMessage(Message) error         { panic("not implemented") }
+func (*Buffer) EncodeRawBytes([]byte) error         { panic("not implemented") }
+func (*Buffer) EncodeStringBytes(string) error      { panic("not implemented") }
+func (*Buffer) EncodeVarint(uint64) error           { panic("not implemented") }
+func (*Buffer) EncodeZigzag32(uint64) error         { panic("not implemented") }
+func (*Buffer) EncodeZigzag64(uint64) error         { panic("not implemented") }
+func (*Buffer) Marshal(Message) error               { panic("not implemented") }
+func (*Buffer) Unmarshal(Message) error             { panic("not implemented") }
+
+type InternalMessageInfo struct{}
+
+func (*InternalMessageInfo) DiscardUnknown(Message)                        { panic("not implemented") }
+func (*InternalMessageInfo) Marshal([]byte, Message, bool) ([]byte, error) { panic("not implemented") }
+func (*InternalMessageInfo) Merge(Message, Message)                        { panic("not implemented") }
+func (*InternalMessageInfo) Size(Message) int                              { panic("not implemented") }
+func (*InternalMessageInfo) Unmarshal(Message, []byte) error               { panic("not implemented") }

+ 4 - 0
internal/testprotos/legacy/legacy.proto

@@ -15,6 +15,10 @@ option go_package = "google.golang.org/protobuf/internal/testprotos/legacy";
 //
 // The specific version of protoc-gen-go used is encoded in the file path:
 //	$NAME.$VERSION-$COMMIT_DATE-$COMMIT_HASH/test.proto
+//
+// To avoid a dependency on the v1 module, we perform the following
+// import path replacement to all generated .pb.go files:
+//	"github.com/golang/protobuf/proto" => "google.golang.org/protobuf/internal/protolegacy"
 
 // The oldest supported version of protoc-gen-go is 2fc053c5,
 // which finished adding descriptor methods to all protobuf types.

+ 1 - 1
internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5/test.pb.go

@@ -14,7 +14,7 @@ It has these top-level messages:
 */
 package github_com_golang_protobuf_v2_internal_testprotos_legacy_proto2_v0_0_0_20160225_2fc053c5
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto2.v0.0.0-20160519-a4ab9ec5/test.pb.go

@@ -14,7 +14,7 @@ It has these top-level messages:
 */
 package proto2_v0_0_0_20160519_a4ab9ec5
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152/test.pb.go

@@ -13,7 +13,7 @@ It has these top-level messages:
 */
 package proto2_v1_0_0_20180125_92554152
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto2.v1.1.0-20180430-b4deda09/test.pb.go

@@ -3,7 +3,7 @@
 
 package proto2_v1_1_0_20180430_b4deda09 // import "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.1.0-20180430-b4deda09"
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto2.v1.2.0-20180814-aa810b61/test.pb.go

@@ -3,7 +3,7 @@
 
 package proto2_v1_2_0_20180814_aa810b61 // import "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.2.0-20180814-aa810b61"
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto2.v1.2.1-20181126-8d0c54c1/test.pb.go

@@ -5,7 +5,7 @@ package proto2_v1_2_1_20181126_8d0c54c1
 
 import (
 	fmt "fmt"
-	proto "github.com/golang/protobuf/proto"
+	proto "google.golang.org/protobuf/internal/protolegacy"
 	math "math"
 )
 

+ 1 - 1
internal/testprotos/legacy/proto3.v0.0.0-20160225-2fc053c5/test.pb.go

@@ -14,7 +14,7 @@ It has these top-level messages:
 */
 package github_com_golang_protobuf_v2_internal_testprotos_legacy_proto3_v0_0_0_20160225_2fc053c5
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto3.v0.0.0-20160519-a4ab9ec5/test.pb.go

@@ -14,7 +14,7 @@ It has these top-level messages:
 */
 package proto3_v0_0_0_20160519_a4ab9ec5
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto3.v1.0.0-20180125-92554152/test.pb.go

@@ -13,7 +13,7 @@ It has these top-level messages:
 */
 package proto3_v1_0_0_20180125_92554152
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto3.v1.1.0-20180430-b4deda09/test.pb.go

@@ -3,7 +3,7 @@
 
 package proto3_v1_1_0_20180430_b4deda09 // import "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v1.1.0-20180430-b4deda09"
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto3.v1.2.0-20180814-aa810b61/test.pb.go

@@ -3,7 +3,7 @@
 
 package proto3_v1_2_0_20180814_aa810b61 // import "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v1.2.0-20180814-aa810b61"
 
-import proto "github.com/golang/protobuf/proto"
+import proto "google.golang.org/protobuf/internal/protolegacy"
 import fmt "fmt"
 import math "math"
 

+ 1 - 1
internal/testprotos/legacy/proto3.v1.2.1-20181126-8d0c54c1/test.pb.go

@@ -5,7 +5,7 @@ package proto3_v1_2_1_20181126_8d0c54c1
 
 import (
 	fmt "fmt"
-	proto "github.com/golang/protobuf/proto"
+	proto "google.golang.org/protobuf/internal/protolegacy"
 	math "math"
 )
 

+ 2 - 4
proto/extension_test.go

@@ -31,10 +31,8 @@ func TestExtensionFuncs(t *testing.T) {
 			value:       int32(1),
 		},
 		{
-			message: &testpb.TestAllExtensions{},
-			ext:     testpb.E_RepeatedStringExtension,
-			// TODO: Represent repeated extension fields as []T.
-			// https://github.com/golang/protobuf/issues/901
+			message:     &testpb.TestAllExtensions{},
+			ext:         testpb.E_RepeatedStringExtension,
 			wantDefault: ([]string)(nil),
 			value:       []string{"a", "b", "c"},
 		},