all: use reflect.TypeFor now available in Go 1.22 (#11078)

Updates #cleanup

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2024-02-08 17:34:22 -08:00
committed by GitHub
parent efddad7d7d
commit 94a4f701c2
18 changed files with 31 additions and 38 deletions

View File

@@ -24,11 +24,6 @@ import (
"reflect"
)
// TODO(https://go.dev/issue/60088): Use reflect.TypeFor instead.
func reflectTypeFor[T any]() reflect.Type {
return reflect.TypeOf((*T)(nil)).Elem()
}
// Key is a generic key type associated with a specific value type.
//
// A zero Key is valid where the Value type itself is used as the context key.
@@ -65,7 +60,7 @@ func New[Value any](name string, defaultValue Value) Key[Value] {
// since newly allocated pointers are globally unique within a process.
key := Key[Value]{name: new(stringer[string])}
if name == "" {
name = reflectTypeFor[Value]().String()
name = reflect.TypeFor[Value]().String()
}
key.name.v = name
if v := reflect.ValueOf(defaultValue); v.IsValid() && !v.IsZero() {
@@ -78,7 +73,7 @@ func New[Value any](name string, defaultValue Value) Key[Value] {
func (key Key[Value]) contextKey() any {
if key.name == nil {
// Use the reflect.Type of the Value (implies key not created by New).
return reflectTypeFor[Value]()
return reflect.TypeFor[Value]()
} else {
// Use the name pointer directly (implies key created by New).
return key.name
@@ -119,7 +114,7 @@ func (key Key[Value]) Has(ctx context.Context) (ok bool) {
// String returns the name of the key.
func (key Key[Value]) String() string {
if key.name == nil {
return reflectTypeFor[Value]().String()
return reflect.TypeFor[Value]().String()
}
return key.name.String()
}

View File

@@ -248,7 +248,7 @@ func Hash[T any](v *T) Sum {
// Always treat the Hash input as if it were an interface by including
// a hash of the type. This ensures that hashing of two different types
// but with the same value structure produces different hashes.
t := reflect.TypeOf(v).Elem()
t := reflect.TypeFor[T]()
h.hashType(t)
if v == nil {
h.HashUint8(0) // indicates nil
@@ -300,8 +300,7 @@ func ExcludeFields[T any](fields ...string) Option {
}
func newFieldFilter[T any](include bool, fields []string) Option {
var zero T
t := reflect.TypeOf(&zero).Elem()
t := reflect.TypeFor[T]()
fieldSet := set.Set[string]{}
for _, f := range fields {
if _, ok := t.FieldByName(f); !ok {
@@ -321,12 +320,11 @@ func newFieldFilter[T any](include bool, fields []string) Option {
// be removed in the future, along with documentation about their precedence
// when combined.
func HasherForType[T any](opts ...Option) func(*T) Sum {
var v *T
seedOnce.Do(initSeed)
if len(opts) > 1 {
panic("HasherForType only accepts one optional argument") // for now
}
t := reflect.TypeOf(v).Elem()
t := reflect.TypeFor[T]()
var hash typeHasherFunc
for _, o := range opts {
switch o := o.(type) {

View File

@@ -823,7 +823,7 @@ func TestHashMapAcyclic(t *testing.T) {
hb := &hashBuffer{Hash: sha256.New()}
hash := lookupTypeHasher(reflect.TypeOf(m))
hash := lookupTypeHasher(reflect.TypeFor[map[int]string]())
for i := 0; i < 20; i++ {
va := reflect.ValueOf(&m).Elem()
hb.Reset()

View File

@@ -10,9 +10,9 @@ import (
)
var (
timeTimeType = reflect.TypeOf((*time.Time)(nil)).Elem()
netipAddrType = reflect.TypeOf((*netip.Addr)(nil)).Elem()
selfHasherType = reflect.TypeOf((*SelfHasher)(nil)).Elem()
timeTimeType = reflect.TypeFor[time.Time]()
netipAddrType = reflect.TypeFor[netip.Addr]()
selfHasherType = reflect.TypeFor[SelfHasher]()
)
// typeIsSpecialized reports whether this type has specialized hashing.