Skip to content

Commit 92106a6

Browse files
cuiweixiefjl
andauthored
accounts/abi, accounts/keystore: use reflect.TypeFor (#32323)
Co-authored-by: Felix Lange <[email protected]>
1 parent 2485d09 commit 92106a6

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

accounts/abi/reflect.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func ConvertType(in interface{}, proto interface{}) interface{} {
5353
// indirect recursively dereferences the value until it either gets the value
5454
// or finds a big.Int
5555
func indirect(v reflect.Value) reflect.Value {
56-
if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeOf(big.Int{}) {
56+
if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeFor[big.Int]() {
5757
return indirect(v.Elem())
5858
}
5959
return v
@@ -65,32 +65,32 @@ func reflectIntType(unsigned bool, size int) reflect.Type {
6565
if unsigned {
6666
switch size {
6767
case 8:
68-
return reflect.TypeOf(uint8(0))
68+
return reflect.TypeFor[uint8]()
6969
case 16:
70-
return reflect.TypeOf(uint16(0))
70+
return reflect.TypeFor[uint16]()
7171
case 32:
72-
return reflect.TypeOf(uint32(0))
72+
return reflect.TypeFor[uint32]()
7373
case 64:
74-
return reflect.TypeOf(uint64(0))
74+
return reflect.TypeFor[uint64]()
7575
}
7676
}
7777
switch size {
7878
case 8:
79-
return reflect.TypeOf(int8(0))
79+
return reflect.TypeFor[int8]()
8080
case 16:
81-
return reflect.TypeOf(int16(0))
81+
return reflect.TypeFor[int16]()
8282
case 32:
83-
return reflect.TypeOf(int32(0))
83+
return reflect.TypeFor[int32]()
8484
case 64:
85-
return reflect.TypeOf(int64(0))
85+
return reflect.TypeFor[int64]()
8686
}
87-
return reflect.TypeOf(&big.Int{})
87+
return reflect.TypeFor[*big.Int]()
8888
}
8989

9090
// mustArrayToByteSlice creates a new byte slice with the exact same size as value
9191
// and copies the bytes in value to the new slice.
9292
func mustArrayToByteSlice(value reflect.Value) reflect.Value {
93-
slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
93+
slice := reflect.ValueOf(make([]byte, value.Len()))
9494
reflect.Copy(slice, value)
9595
return slice
9696
}
@@ -104,7 +104,7 @@ func set(dst, src reflect.Value) error {
104104
switch {
105105
case dstType.Kind() == reflect.Interface && dst.Elem().IsValid() && (dst.Elem().Type().Kind() == reflect.Ptr || dst.Elem().CanSet()):
106106
return set(dst.Elem(), src)
107-
case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeOf(big.Int{}):
107+
case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeFor[big.Int]():
108108
return set(dst.Elem(), src)
109109
case srcType.AssignableTo(dstType) && dst.CanSet():
110110
dst.Set(src)

accounts/abi/reflect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ func TestConvertType(t *testing.T) {
204204
var fields []reflect.StructField
205205
fields = append(fields, reflect.StructField{
206206
Name: "X",
207-
Type: reflect.TypeOf(new(big.Int)),
207+
Type: reflect.TypeFor[*big.Int](),
208208
Tag: "json:\"" + "x" + "\"",
209209
})
210210
fields = append(fields, reflect.StructField{
211211
Name: "Y",
212-
Type: reflect.TypeOf(new(big.Int)),
212+
Type: reflect.TypeFor[*big.Int](),
213213
Tag: "json:\"" + "y" + "\"",
214214
})
215215
val := reflect.New(reflect.StructOf(fields))

accounts/abi/type.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,29 +238,25 @@ func (t Type) GetType() reflect.Type {
238238
case UintTy:
239239
return reflectIntType(true, t.Size)
240240
case BoolTy:
241-
return reflect.TypeOf(false)
241+
return reflect.TypeFor[bool]()
242242
case StringTy:
243-
return reflect.TypeOf("")
243+
return reflect.TypeFor[string]()
244244
case SliceTy:
245245
return reflect.SliceOf(t.Elem.GetType())
246246
case ArrayTy:
247247
return reflect.ArrayOf(t.Size, t.Elem.GetType())
248248
case TupleTy:
249249
return t.TupleType
250250
case AddressTy:
251-
return reflect.TypeOf(common.Address{})
251+
return reflect.TypeFor[common.Address]()
252252
case FixedBytesTy:
253-
return reflect.ArrayOf(t.Size, reflect.TypeOf(byte(0)))
253+
return reflect.ArrayOf(t.Size, reflect.TypeFor[byte]())
254254
case BytesTy:
255-
return reflect.SliceOf(reflect.TypeOf(byte(0)))
256-
case HashTy:
257-
// hashtype currently not used
258-
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
259-
case FixedPointTy:
260-
// fixedpoint type currently not used
261-
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
255+
return reflect.TypeFor[[]byte]()
256+
case HashTy, FixedPointTy: // currently not used
257+
return reflect.TypeFor[[32]byte]()
262258
case FunctionTy:
263-
return reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
259+
return reflect.TypeFor[[24]byte]()
264260
default:
265261
panic("Invalid type")
266262
}

accounts/keystore/keystore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var (
5050
)
5151

5252
// KeyStoreType is the reflect type of a keystore backend.
53-
var KeyStoreType = reflect.TypeOf(&KeyStore{})
53+
var KeyStoreType = reflect.TypeFor[*KeyStore]()
5454

5555
// KeyStoreScheme is the protocol scheme prefixing account and wallet URLs.
5656
const KeyStoreScheme = "keystore"

0 commit comments

Comments
 (0)