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

View file

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

View file

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

View file

@ -50,7 +50,7 @@ var (
) )
// KeyStoreType is the reflect type of a keystore backend. // 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. // KeyStoreScheme is the protocol scheme prefixing account and wallet URLs.
const KeyStoreScheme = "keystore" const KeyStoreScheme = "keystore"