accounts/abi, accounts/keystore: use reflect.TypeFor (#32323)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
cui 2025-08-11 20:24:55 +08:00 committed by GitHub
parent 2485d096f3
commit 92106a6b17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 27 deletions

View file

@ -53,7 +53,7 @@ func ConvertType(in interface{}, proto interface{}) interface{} {
// indirect recursively dereferences the value until it either gets the value
// or finds a big.Int
func indirect(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeOf(big.Int{}) {
if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeFor[big.Int]() {
return indirect(v.Elem())
}
return v
@ -65,32 +65,32 @@ func reflectIntType(unsigned bool, size int) reflect.Type {
if unsigned {
switch size {
case 8:
return reflect.TypeOf(uint8(0))
return reflect.TypeFor[uint8]()
case 16:
return reflect.TypeOf(uint16(0))
return reflect.TypeFor[uint16]()
case 32:
return reflect.TypeOf(uint32(0))
return reflect.TypeFor[uint32]()
case 64:
return reflect.TypeOf(uint64(0))
return reflect.TypeFor[uint64]()
}
}
switch size {
case 8:
return reflect.TypeOf(int8(0))
return reflect.TypeFor[int8]()
case 16:
return reflect.TypeOf(int16(0))
return reflect.TypeFor[int16]()
case 32:
return reflect.TypeOf(int32(0))
return reflect.TypeFor[int32]()
case 64:
return reflect.TypeOf(int64(0))
return reflect.TypeFor[int64]()
}
return reflect.TypeOf(&big.Int{})
return reflect.TypeFor[*big.Int]()
}
// mustArrayToByteSlice creates a new byte slice with the exact same size as value
// and copies the bytes in value to the new slice.
func mustArrayToByteSlice(value reflect.Value) reflect.Value {
slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
slice := reflect.ValueOf(make([]byte, value.Len()))
reflect.Copy(slice, value)
return slice
}
@ -104,7 +104,7 @@ func set(dst, src reflect.Value) error {
switch {
case dstType.Kind() == reflect.Interface && dst.Elem().IsValid() && (dst.Elem().Type().Kind() == reflect.Ptr || dst.Elem().CanSet()):
return set(dst.Elem(), src)
case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeOf(big.Int{}):
case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeFor[big.Int]():
return set(dst.Elem(), src)
case srcType.AssignableTo(dstType) && dst.CanSet():
dst.Set(src)

View file

@ -204,12 +204,12 @@ func TestConvertType(t *testing.T) {
var fields []reflect.StructField
fields = append(fields, reflect.StructField{
Name: "X",
Type: reflect.TypeOf(new(big.Int)),
Type: reflect.TypeFor[*big.Int](),
Tag: "json:\"" + "x" + "\"",
})
fields = append(fields, reflect.StructField{
Name: "Y",
Type: reflect.TypeOf(new(big.Int)),
Type: reflect.TypeFor[*big.Int](),
Tag: "json:\"" + "y" + "\"",
})
val := reflect.New(reflect.StructOf(fields))

View file

@ -238,9 +238,9 @@ func (t Type) GetType() reflect.Type {
case UintTy:
return reflectIntType(true, t.Size)
case BoolTy:
return reflect.TypeOf(false)
return reflect.TypeFor[bool]()
case StringTy:
return reflect.TypeOf("")
return reflect.TypeFor[string]()
case SliceTy:
return reflect.SliceOf(t.Elem.GetType())
case ArrayTy:
@ -248,19 +248,15 @@ func (t Type) GetType() reflect.Type {
case TupleTy:
return t.TupleType
case AddressTy:
return reflect.TypeOf(common.Address{})
return reflect.TypeFor[common.Address]()
case FixedBytesTy:
return reflect.ArrayOf(t.Size, reflect.TypeOf(byte(0)))
return reflect.ArrayOf(t.Size, reflect.TypeFor[byte]())
case BytesTy:
return reflect.SliceOf(reflect.TypeOf(byte(0)))
case HashTy:
// hashtype currently not used
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
case FixedPointTy:
// fixedpoint type currently not used
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
return reflect.TypeFor[[]byte]()
case HashTy, FixedPointTy: // currently not used
return reflect.TypeFor[[32]byte]()
case FunctionTy:
return reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
return reflect.TypeFor[[24]byte]()
default:
panic("Invalid type")
}

View file

@ -50,7 +50,7 @@ var (
)
// KeyStoreType is the reflect type of a keystore backend.
var KeyStoreType = reflect.TypeOf(&KeyStore{})
var KeyStoreType = reflect.TypeFor[*KeyStore]()
// KeyStoreScheme is the protocol scheme prefixing account and wallet URLs.
const KeyStoreScheme = "keystore"