mirror of
https://github.com/golang/go
synced 2024-11-17 17:54:48 -07:00
encoding/gob: use reflect.TypeFor for known types
This avoids several mildly confusing Elem calls. For #60088 Change-Id: If7b83d2ab10537c7e886a035b43cb272130c1669 Reviewed-on: https://go-review.googlesource.com/c/go/+/514455 Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
9138743679
commit
d4b46b0956
@ -1197,7 +1197,7 @@ func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePt
|
||||
// emptyStruct is the type we compile into when ignoring a struct value.
|
||||
type emptyStruct struct{}
|
||||
|
||||
var emptyStructType = reflect.TypeOf((*emptyStruct)(nil)).Elem()
|
||||
var emptyStructType = reflect.TypeFor[emptyStruct]()
|
||||
|
||||
// getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
|
||||
func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {
|
||||
|
@ -806,7 +806,7 @@ func TestIgnoreDepthLimit(t *testing.T) {
|
||||
defer func() { maxIgnoreNestingDepth = oldNestingDepth }()
|
||||
b := new(bytes.Buffer)
|
||||
enc := NewEncoder(b)
|
||||
typ := reflect.TypeOf(int(0))
|
||||
typ := reflect.TypeFor[int]()
|
||||
nested := reflect.ArrayOf(1, typ)
|
||||
for i := 0; i < 100; i++ {
|
||||
nested = reflect.ArrayOf(1, nested)
|
||||
|
@ -103,14 +103,14 @@ func validUserType(rt reflect.Type) (*userTypeInfo, error) {
|
||||
}
|
||||
|
||||
var (
|
||||
gobEncoderInterfaceType = reflect.TypeOf((*GobEncoder)(nil)).Elem()
|
||||
gobDecoderInterfaceType = reflect.TypeOf((*GobDecoder)(nil)).Elem()
|
||||
binaryMarshalerInterfaceType = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
|
||||
binaryUnmarshalerInterfaceType = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
|
||||
textMarshalerInterfaceType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
|
||||
textUnmarshalerInterfaceType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
||||
gobEncoderInterfaceType = reflect.TypeFor[GobEncoder]()
|
||||
gobDecoderInterfaceType = reflect.TypeFor[GobDecoder]()
|
||||
binaryMarshalerInterfaceType = reflect.TypeFor[encoding.BinaryMarshaler]()
|
||||
binaryUnmarshalerInterfaceType = reflect.TypeFor[encoding.BinaryUnmarshaler]()
|
||||
textMarshalerInterfaceType = reflect.TypeFor[encoding.TextMarshaler]()
|
||||
textUnmarshalerInterfaceType = reflect.TypeFor[encoding.TextUnmarshaler]()
|
||||
|
||||
wireTypeType = reflect.TypeOf((*wireType)(nil)).Elem()
|
||||
wireTypeType = reflect.TypeFor[wireType]()
|
||||
)
|
||||
|
||||
// implementsInterface reports whether the type implements the
|
||||
@ -270,12 +270,12 @@ var wireTypeUserInfo *userTypeInfo // userTypeInfo of wireType
|
||||
func init() {
|
||||
// Some magic numbers to make sure there are no surprises.
|
||||
checkId(16, tWireType)
|
||||
checkId(17, mustGetTypeInfo(reflect.TypeOf((*arrayType)(nil)).Elem()).id)
|
||||
checkId(18, mustGetTypeInfo(reflect.TypeOf((*CommonType)(nil)).Elem()).id)
|
||||
checkId(19, mustGetTypeInfo(reflect.TypeOf((*sliceType)(nil)).Elem()).id)
|
||||
checkId(20, mustGetTypeInfo(reflect.TypeOf((*structType)(nil)).Elem()).id)
|
||||
checkId(21, mustGetTypeInfo(reflect.TypeOf((*fieldType)(nil)).Elem()).id)
|
||||
checkId(23, mustGetTypeInfo(reflect.TypeOf((*mapType)(nil)).Elem()).id)
|
||||
checkId(17, mustGetTypeInfo(reflect.TypeFor[arrayType]()).id)
|
||||
checkId(18, mustGetTypeInfo(reflect.TypeFor[CommonType]()).id)
|
||||
checkId(19, mustGetTypeInfo(reflect.TypeFor[sliceType]()).id)
|
||||
checkId(20, mustGetTypeInfo(reflect.TypeFor[structType]()).id)
|
||||
checkId(21, mustGetTypeInfo(reflect.TypeFor[fieldType]()).id)
|
||||
checkId(23, mustGetTypeInfo(reflect.TypeFor[mapType]()).id)
|
||||
|
||||
copy(builtinIdToTypeSlice[:], idToType)
|
||||
|
||||
|
@ -49,15 +49,15 @@ func TestBasic(t *testing.T) {
|
||||
|
||||
// Reregister some basic types to check registration is idempotent.
|
||||
func TestReregistration(t *testing.T) {
|
||||
newtyp := getTypeUnlocked("int", reflect.TypeOf(int(0)))
|
||||
newtyp := getTypeUnlocked("int", reflect.TypeFor[int]())
|
||||
if newtyp != tInt.gobType() {
|
||||
t.Errorf("reregistration of %s got new type", newtyp.string())
|
||||
}
|
||||
newtyp = getTypeUnlocked("uint", reflect.TypeOf(uint(0)))
|
||||
newtyp = getTypeUnlocked("uint", reflect.TypeFor[uint]())
|
||||
if newtyp != tUint.gobType() {
|
||||
t.Errorf("reregistration of %s got new type", newtyp.string())
|
||||
}
|
||||
newtyp = getTypeUnlocked("string", reflect.TypeOf("hello"))
|
||||
newtyp = getTypeUnlocked("string", reflect.TypeFor[string]())
|
||||
if newtyp != tString.gobType() {
|
||||
t.Errorf("reregistration of %s got new type", newtyp.string())
|
||||
}
|
||||
@ -145,7 +145,7 @@ type Foo struct {
|
||||
}
|
||||
|
||||
func TestStructType(t *testing.T) {
|
||||
sstruct := getTypeUnlocked("Foo", reflect.TypeOf(Foo{}))
|
||||
sstruct := getTypeUnlocked("Foo", reflect.TypeFor[Foo]())
|
||||
str := sstruct.string()
|
||||
// If we can print it correctly, we built it correctly.
|
||||
expected := "Foo = struct { A int; B int; C string; D bytes; E float; F float; G Bar = struct { X string; }; H Bar; I Foo; }"
|
||||
|
Loading…
Reference in New Issue
Block a user