Skip to content

Commit a313950

Browse files
authored
refactor: using reflect.TypeFor (#13353)
1 parent 0e56e16 commit a313950

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

api/docgen-openrpc/openrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func OpenRPCSchemaTypeMapper(ty reflect.Type) *jsonschema.Type {
112112
ty = ty.Elem()
113113
}
114114

115-
if ty == reflect.TypeOf((*interface{})(nil)).Elem() {
115+
if ty == reflect.TypeFor[interface{}]() {
116116
return &jsonschema.Type{Type: "object", AdditionalProperties: []byte("true")}
117117
}
118118

api/docgen/docgen.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ import (
5252
)
5353

5454
var ExampleValues = map[reflect.Type]interface{}{
55-
reflect.TypeOf(api.MinerSubsystem(0)): api.MinerSubsystem(1),
56-
reflect.TypeOf(auth.Permission("")): auth.Permission("write"),
57-
reflect.TypeOf(""): "string value",
58-
reflect.TypeOf(uint64(42)): uint64(42),
59-
reflect.TypeOf(byte(7)): byte(7),
60-
reflect.TypeOf([]byte{}): []byte("byte array"),
55+
reflect.TypeFor[api.MinerSubsystem](): api.MinerSubsystem(1),
56+
reflect.TypeFor[auth.Permission](): auth.Permission("write"),
57+
reflect.TypeFor[string](): "string value",
58+
reflect.TypeFor[uint64](): uint64(42),
59+
reflect.TypeFor[byte](): byte(7),
60+
reflect.TypeFor[[]byte](): []byte("byte array"),
6161
}
6262

6363
func addExample(v interface{}) {
@@ -70,8 +70,8 @@ func init() {
7070
panic(err)
7171
}
7272

73-
ExampleValues[reflect.TypeOf(c)] = c
74-
ExampleValues[reflect.TypeOf(&c)] = &c
73+
ExampleValues[reflect.TypeFor[cid.Cid]()] = c
74+
ExampleValues[reflect.TypeFor[*cid.Cid]()] = &c
7575

7676
c2, err := cid.Decode("bafy2bzacebp3shtrn43k7g3unredz7fxn4gj533d3o43tqn2p2ipxxhrvchve")
7777
if err != nil {
@@ -80,15 +80,15 @@ func init() {
8080

8181
tsk := types.NewTipSetKey(c, c2)
8282

83-
ExampleValues[reflect.TypeOf(tsk)] = tsk
83+
ExampleValues[reflect.TypeFor[types.TipSetKey]()] = tsk
8484

8585
addr, err := address.NewIDAddress(1234)
8686
if err != nil {
8787
panic(err)
8888
}
8989

90-
ExampleValues[reflect.TypeOf(addr)] = addr
91-
ExampleValues[reflect.TypeOf(&addr)] = &addr
90+
ExampleValues[reflect.TypeFor[address.Address]()] = addr
91+
ExampleValues[reflect.TypeFor[*address.Address]()] = &addr
9292

9393
var ts types.TipSet
9494
err = json.Unmarshal(tipsetSampleJson, &ts)
@@ -143,8 +143,7 @@ func init() {
143143
addExample(&f3Cert)
144144

145145
block := blocks.Block(&blocks.BasicBlock{})
146-
ExampleValues[reflect.TypeOf(&block).Elem()] = block
147-
146+
ExampleValues[reflect.TypeFor[blocks.Block]()] = block
148147
addExample(bitfield.NewFromSet([]uint64{5}))
149148
addExample(abi.RegisteredSealProof_StackedDrg32GiBV1_1)
150149
addExample(abi.RegisteredPoStProof_StackedDrgWindow32GiBV1)
@@ -187,9 +186,7 @@ func init() {
187186
addExample(map[string]int{"name": 42})
188187
addExample(map[string]time.Time{"name": time.Unix(1615243938, 0).UTC()})
189188
addExample(abi.ActorID(1000))
190-
addExample(map[string]types.Actor{
191-
"t01236": ExampleValue("init", reflect.TypeOf(types.Actor{}), nil).(types.Actor),
192-
})
189+
addExample(map[string]types.Actor{"t01236": ExampleValue("init", reflect.TypeFor[types.Actor](), nil).(types.Actor)})
193190
addExample(types.IpldOpGet)
194191
addExample(&types.TraceIpld{
195192
Op: types.IpldOpGet,

chain/vm/invoker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func (ar *ActorRegistry) Register(av actorstypes.Version, pred ActorPredicate, v
129129
// Explicitly add send, it's special.
130130
methods[builtin.MethodSend] = MethodMeta{
131131
Name: "Send",
132-
Params: reflect.TypeOf(new(abi.EmptyValue)),
133-
Ret: reflect.TypeOf(new(abi.EmptyValue)),
132+
Params: reflect.TypeFor[*abi.EmptyValue](),
133+
Ret: reflect.TypeFor[*abi.EmptyValue](),
134134
}
135135

136136
// Iterate over exported methods. Some of these _may_ be nil and
@@ -191,7 +191,7 @@ type invokee interface {
191191
func (*ActorRegistry) transform(instance invokee) (nativeCode, error) {
192192
itype := reflect.TypeOf(instance)
193193
exports := instance.Exports()
194-
runtimeType := reflect.TypeOf((*vmr.Runtime)(nil)).Elem()
194+
runtimeType := reflect.TypeFor[vmr.Runtime]()
195195
for i, e := range exports {
196196
i := i
197197
m := e.Method
@@ -225,7 +225,7 @@ func (*ActorRegistry) transform(instance invokee) (nativeCode, error) {
225225
"cbg.CBORMarshaler")
226226
}
227227
o0 := t.Out(0)
228-
if !o0.Implements(reflect.TypeOf((*cbg.CBORMarshaler)(nil)).Elem()) {
228+
if !o0.Implements(reflect.TypeFor[cbg.CBORMarshaler]()) {
229229
return nil, newErr("output needs to implement cgb.CBORMarshaler")
230230
}
231231
}
@@ -236,7 +236,7 @@ func (*ActorRegistry) transform(instance invokee) (nativeCode, error) {
236236
continue
237237
}
238238
meth := reflect.ValueOf(m)
239-
code[id] = reflect.MakeFunc(reflect.TypeOf((invokeFunc)(nil)),
239+
code[id] = reflect.MakeFunc(reflect.TypeFor[invokeFunc](),
240240
func(in []reflect.Value) []reflect.Value {
241241
paramT := meth.Type().In(1).Elem()
242242
param := reflect.New(paramT)

cli/worker/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ Example invocation of lotus-bench as external executor:
320320

321321
// Check DC-environment variable
322322
sectorSizes := []string{"2KiB", "8MiB", "512MiB", "32GiB", "64GiB"}
323-
resourcesType := reflect.TypeOf(storiface.Resources{})
323+
resourcesType := reflect.TypeFor[storiface.Resources]()
324324

325325
for _, sectorSize := range sectorSizes {
326326
for i := 0; i < resourcesType.NumField(); i++ {

cmd/lotus-shed/state-stats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ func collectSnapshotJobStats(ctx context.Context, in job, dag format.NodeGetter,
827827
varType := v.Type().Field(i).Type
828828
varValue := v.Field(i).Interface()
829829

830-
if varType == reflect.TypeOf(cid.Cid{}) {
830+
if varType == reflect.TypeFor[cid.Cid]() {
831831
subjobs = append(subjobs, job{
832832
key: fmt.Sprintf("%s/%s", in.key, varName),
833833
c: varValue.(cid.Cid),
@@ -894,7 +894,7 @@ func collectStats(ctx context.Context, addr address.Address, actor *types.Actor,
894894
varType := v.Type().Field(i).Type
895895
varValue := v.Field(i).Interface()
896896

897-
if varType == reflect.TypeOf(cid.Cid{}) {
897+
if varType == reflect.TypeFor[cid.Cid]() {
898898
fields = append(fields, fieldItem{
899899
Name: varName,
900900
Cid: varValue.(cid.Cid),

0 commit comments

Comments
 (0)