From 72fc3efa853e1ae4bab9f94cde797b11c834f4e6 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Wed, 27 Nov 2024 19:56:22 +0000 Subject: [PATCH] mk: use Go 1.22 reflect.TypeFor --- app/fetch/gql.go | 2 +- app/fileserver/gql.go | 6 +++--- app/fwdp/gql.go | 2 +- app/pdump/gql.go | 4 ++-- app/tg/gql.go | 16 ++++++++-------- app/tgconsumer/gql.go | 18 +++++++++--------- app/tgproducer/gql.go | 12 ++++++------ core/gqlserver/bind_test.go | 4 ++-- core/gqlserver/gqlserver.go | 2 +- core/gqlserver/interface.go | 2 +- core/nnduration/gql.go | 4 ++-- dpdk/ethdev/ethnetif/gql.go | 6 +++--- dpdk/ethdev/ethnetif/xdp-face-map.go | 2 +- iface/gql.go | 6 +++--- iface/locator.go | 3 +-- ndni/gql.go | 8 ++++---- 16 files changed, 48 insertions(+), 49 deletions(-) diff --git a/app/fetch/gql.go b/app/fetch/gql.go index aa2f7b8c..de46447f 100644 --- a/app/fetch/gql.go +++ b/app/fetch/gql.go @@ -31,7 +31,7 @@ func init() { Name: "FetcherConfigInput", Description: "Fetcher config.", Fields: gqlserver.BindInputFields[Config](gqlserver.FieldTypes{ - reflect.TypeOf(iface.PktQueueConfig{}): iface.GqlPktQueueInput, + reflect.TypeFor[iface.PktQueueConfig](): iface.GqlPktQueueInput, }), }) diff --git a/app/fileserver/gql.go b/app/fileserver/gql.go index 66fff146..c2e35c37 100644 --- a/app/fileserver/gql.go +++ b/app/fileserver/gql.go @@ -27,15 +27,15 @@ func init() { Name: "FileServerMountInput", Description: "File server mount definition.", Fields: gqlserver.BindInputFields[Mount](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): gqlserver.NonNullString, + reflect.TypeFor[ndn.Name](): gqlserver.NonNullString, }), }) GqlConfigInput = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "FileServerConfigInput", Description: "File server config.", Fields: gqlserver.BindInputFields[Config](gqlserver.FieldTypes{ - reflect.TypeOf(iface.PktQueueConfig{}): iface.GqlPktQueueInput, - reflect.TypeOf(Mount{}): GqlMountInput, + reflect.TypeFor[iface.PktQueueConfig](): iface.GqlPktQueueInput, + reflect.TypeFor[Mount](): GqlMountInput, }), }) GqlCountersType = graphql.NewObject(graphql.ObjectConfig{ diff --git a/app/fwdp/gql.go b/app/fwdp/gql.go index e22af66f..53c14f3d 100644 --- a/app/fwdp/gql.go +++ b/app/fwdp/gql.go @@ -233,7 +233,7 @@ func init() { GqlFwdCountersType = graphql.NewObject(graphql.ObjectConfig{ Name: "FwFwdCounters", Fields: gqlserver.BindFields[FwdCounters](gqlserver.FieldTypes{ - reflect.TypeOf(runningstat.Snapshot{}): runningstat.GqlSnapshotType, + reflect.TypeFor[runningstat.Snapshot](): runningstat.GqlSnapshotType, }), }) fwdCountersConfigTemplate := gqlserver.CountersConfig{ diff --git a/app/pdump/gql.go b/app/pdump/gql.go index 65ce39b7..6944e92e 100644 --- a/app/pdump/gql.go +++ b/app/pdump/gql.go @@ -43,14 +43,14 @@ func init() { Name: "PdumpNameFilterEntryInput", Description: "Packet dump name filter entry.", Fields: gqlserver.BindInputFields[NameFilterEntry](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): ndni.GqlNameType, + reflect.TypeFor[ndn.Name](): ndni.GqlNameType, }), }) GqlNameFilterEntryType = graphql.NewObject(graphql.ObjectConfig{ Name: "PdumpNameFilterEntry", Description: "Packet dump name filter entry.", Fields: gqlserver.BindFields[NameFilterEntry](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): ndni.GqlNameType, + reflect.TypeFor[ndn.Name](): ndni.GqlNameType, }), }) diff --git a/app/tg/gql.go b/app/tg/gql.go index 8415adf0..c12d9315 100644 --- a/app/tg/gql.go +++ b/app/tg/gql.go @@ -101,11 +101,11 @@ func init() { Name: "startTrafficGen", Description: "Create and start a traffic generator.", Args: gqlserver.BindArguments[Config](gqlserver.FieldTypes{ - reflect.TypeOf(iface.LocatorWrapper{}): gqlserver.JSON, - reflect.TypeOf(tgproducer.Config{}): tgproducer.GqlConfigInput, - reflect.TypeOf(fileserver.Config{}): fileserver.GqlConfigInput, - reflect.TypeOf(tgconsumer.Config{}): tgconsumer.GqlConfigInput, - reflect.TypeOf(fetch.Config{}): fetch.GqlConfigInput, + reflect.TypeFor[iface.LocatorWrapper](): gqlserver.JSON, + reflect.TypeFor[tgproducer.Config](): tgproducer.GqlConfigInput, + reflect.TypeFor[fileserver.Config](): fileserver.GqlConfigInput, + reflect.TypeFor[tgconsumer.Config](): tgconsumer.GqlConfigInput, + reflect.TypeFor[fetch.Config](): fetch.GqlConfigInput, }), Type: graphql.NewNonNull(GqlTrafficGenType.Object), Resolve: func(p graphql.ResolveParams) (any, error) { @@ -134,9 +134,9 @@ func init() { Name: "TgCounters", Description: "Traffic generator counters.", Fields: gqlserver.BindFields[Counters](gqlserver.FieldTypes{ - reflect.TypeOf(tgproducer.Counters{}): tgproducer.GqlCountersType, - reflect.TypeOf(fileserver.Counters{}): fileserver.GqlCountersType, - reflect.TypeOf(tgconsumer.Counters{}): tgconsumer.GqlCountersType, + reflect.TypeFor[tgproducer.Counters](): tgproducer.GqlCountersType, + reflect.TypeFor[fileserver.Counters](): fileserver.GqlCountersType, + reflect.TypeFor[tgconsumer.Counters](): tgconsumer.GqlCountersType, }), }) diff --git a/app/tgconsumer/gql.go b/app/tgconsumer/gql.go index 7a990f5b..880d3689 100644 --- a/app/tgconsumer/gql.go +++ b/app/tgconsumer/gql.go @@ -31,32 +31,32 @@ func init() { Name: "TgcPatternInput", Description: "Traffic generator consumer pattern definition.", Fields: gqlserver.BindInputFields[Pattern](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): gqlserver.NonNullString, - reflect.TypeOf(nnduration.Milliseconds(0)): nnduration.GqlMilliseconds, - reflect.TypeOf(ndni.DataGenConfig{}): ndni.GqlDataGenInput, + reflect.TypeFor[ndn.Name](): gqlserver.NonNullString, + reflect.TypeFor[nnduration.Milliseconds](): nnduration.GqlMilliseconds, + reflect.TypeFor[ndni.DataGenConfig](): ndni.GqlDataGenInput, }), }) GqlConfigInput = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "TgcConfigInput", Description: "Traffic generator consumer config.", Fields: gqlserver.BindInputFields[Config](gqlserver.FieldTypes{ - reflect.TypeOf(iface.PktQueueConfig{}): iface.GqlPktQueueInput, - reflect.TypeOf(nnduration.Nanoseconds(0)): nnduration.GqlNanoseconds, - reflect.TypeOf(Pattern{}): GqlPatternInput, + reflect.TypeFor[iface.PktQueueConfig](): iface.GqlPktQueueInput, + reflect.TypeFor[nnduration.Nanoseconds](): nnduration.GqlNanoseconds, + reflect.TypeFor[Pattern](): GqlPatternInput, }), }) GqlPatternCountersType = graphql.NewObject(graphql.ObjectConfig{ Name: "TgcPatternCounters", Fields: gqlserver.BindFields[PatternCounters](gqlserver.FieldTypes{ - reflect.TypeOf(runningstat.Snapshot{}): runningstat.GqlSnapshotType, + reflect.TypeFor[runningstat.Snapshot](): runningstat.GqlSnapshotType, }), }) GqlCountersType = graphql.NewObject(graphql.ObjectConfig{ Name: "TgcCounters", Fields: gqlserver.BindFields[Counters](gqlserver.FieldTypes{ - reflect.TypeOf(runningstat.Snapshot{}): runningstat.GqlSnapshotType, - reflect.TypeOf(PatternCounters{}): GqlPatternCountersType, + reflect.TypeFor[runningstat.Snapshot](): runningstat.GqlSnapshotType, + reflect.TypeFor[PatternCounters](): GqlPatternCountersType, }), }) diff --git a/app/tgproducer/gql.go b/app/tgproducer/gql.go index 3f7f9439..35a38bd5 100644 --- a/app/tgproducer/gql.go +++ b/app/tgproducer/gql.go @@ -29,23 +29,23 @@ func init() { Name: "TgpReplyInput", Description: "Traffic generator producer reply definition.", Fields: gqlserver.BindInputFields[Reply](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): gqlserver.NonNullString, + reflect.TypeFor[ndn.Name](): gqlserver.NonNullString, }), }) GqlPatternInput = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "TgpPatternInput", Description: "Traffic generator producer pattern definition.", Fields: gqlserver.BindInputFields[Pattern](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): gqlserver.NonNullString, - reflect.TypeOf(Reply{}): GqlReplyInput, + reflect.TypeFor[ndn.Name](): gqlserver.NonNullString, + reflect.TypeFor[Reply](): GqlReplyInput, }), }) GqlConfigInput = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "TgpConfigInput", Description: "Traffic generator producer config.", Fields: gqlserver.BindInputFields[Config](gqlserver.FieldTypes{ - reflect.TypeOf(iface.PktQueueConfig{}): iface.GqlPktQueueInput, - reflect.TypeOf(Pattern{}): GqlPatternInput, + reflect.TypeFor[iface.PktQueueConfig](): iface.GqlPktQueueInput, + reflect.TypeFor[Pattern](): GqlPatternInput, }), }) @@ -56,7 +56,7 @@ func init() { GqlCountersType = graphql.NewObject(graphql.ObjectConfig{ Name: "TgpCounters", Fields: gqlserver.BindFields[Counters](gqlserver.FieldTypes{ - reflect.TypeOf(PatternCounters{}): GqlPatternCountersType, + reflect.TypeFor[PatternCounters](): GqlPatternCountersType, }), }) diff --git a/core/gqlserver/bind_test.go b/core/gqlserver/bind_test.go index 17cc06d8..33354186 100644 --- a/core/gqlserver/bind_test.go +++ b/core/gqlserver/bind_test.go @@ -57,7 +57,7 @@ func TestBindFields(t *testing.T) { assert.Panics(func() { gqlserver.BindFields[bindTestC](nil) }) fC := gqlserver.BindFields[bindTestC](gqlserver.FieldTypes{ - reflect.TypeOf(bindTestB{}): gqlTypeB, + reflect.TypeFor[bindTestB](): gqlTypeB, }) assert.Len(fC, len(bindTypesC)) for fieldName, fieldType := range bindTypesC { @@ -104,7 +104,7 @@ func TestBindInputFields(t *testing.T) { assert.Panics(func() { gqlserver.BindInputFields[bindTestC](nil) }) iC := gqlserver.BindInputFields[bindTestC](gqlserver.FieldTypes{ - reflect.TypeOf(bindTestB{}): gqlTypeB, + reflect.TypeFor[bindTestB](): gqlTypeB, }) assert.Len(iC, len(bindTypesC)) for fieldName, fieldType := range bindTypesC { diff --git a/core/gqlserver/gqlserver.go b/core/gqlserver/gqlserver.go index fbb44bb9..2e87b50a 100644 --- a/core/gqlserver/gqlserver.go +++ b/core/gqlserver/gqlserver.go @@ -92,7 +92,7 @@ func init() { versionType := graphql.NewObject(graphql.ObjectConfig{ Name: "Version", Fields: BindFields[version.Version](FieldTypes{ - reflect.TypeOf(time.Time{}): graphql.DateTime, + reflect.TypeFor[time.Time](): graphql.DateTime, }), }) AddQuery(&graphql.Field{ diff --git a/core/gqlserver/interface.go b/core/gqlserver/interface.go index ece7314c..e555dc57 100644 --- a/core/gqlserver/interface.go +++ b/core/gqlserver/interface.go @@ -86,6 +86,6 @@ func NewInterface(ic graphql.InterfaceConfig) (it *Interface) { // ImplementsInterface records an object implementing an interface. // This also appends the object to Schema.Types to ensure that it appears in the schema. func ImplementsInterface[T any](ot *graphql.Object, it *Interface) { - it.types[reflect.TypeOf((*T)(nil)).Elem()] = ot + it.types[reflect.TypeFor[T]()] = ot Schema.Types = append(Schema.Types, ot) } diff --git a/core/nnduration/gql.go b/core/nnduration/gql.go index 8940ac52..330dd742 100644 --- a/core/nnduration/gql.go +++ b/core/nnduration/gql.go @@ -12,8 +12,8 @@ import ( // GraphQL types. var ( - GqlMilliseconds = makeGqlType(reflect.TypeOf(Milliseconds(0))) - GqlNanoseconds = makeGqlType(reflect.TypeOf(Nanoseconds(0))) + GqlMilliseconds = makeGqlType(reflect.TypeFor[Milliseconds]()) + GqlNanoseconds = makeGqlType(reflect.TypeFor[Nanoseconds]()) ) type durationer interface { diff --git a/dpdk/ethdev/ethnetif/gql.go b/dpdk/ethdev/ethnetif/gql.go index bba4140f..94102f0b 100644 --- a/dpdk/ethdev/ethnetif/gql.go +++ b/dpdk/ethdev/ethnetif/gql.go @@ -18,8 +18,8 @@ func init() { GqlDriverKindEnum = gqlserver.NewStringEnum("NetifDriverKind", "", DriverPCI, DriverXDP, DriverAfPacket) GqlConfigFieldTypes = gqlserver.FieldTypes{ - reflect.TypeOf(DriverPCI): GqlDriverKindEnum, - reflect.TypeOf(pciaddr.PCIAddress{}): graphql.String, - reflect.TypeOf(map[string]any{}): gqlserver.JSON, + reflect.TypeFor[DriverKind](): GqlDriverKindEnum, + reflect.TypeFor[pciaddr.PCIAddress](): graphql.String, + reflect.TypeFor[map[string]any](): gqlserver.JSON, } } diff --git a/dpdk/ethdev/ethnetif/xdp-face-map.go b/dpdk/ethdev/ethnetif/xdp-face-map.go index 4e539efd..f94c1e22 100644 --- a/dpdk/ethdev/ethnetif/xdp-face-map.go +++ b/dpdk/ethdev/ethnetif/xdp-face-map.go @@ -140,7 +140,7 @@ func XDPDeleteFaceMapEntry(dev ethdev.EthDev, key []byte) { // xdpHashMakeKey returns *[size]byte pointer as needed by gobpfld.AbstractMap.toKeyPtr(). func xdpHashMakeKey(size uint32, key []byte) (arrayPtr any) { - ptr := reflect.New(reflect.ArrayOf(int(size), reflect.TypeOf(byte(0)))) + ptr := reflect.New(reflect.ArrayOf(int(size), reflect.TypeFor[byte]())) copy(ptr.Elem().Slice(0, int(size)).Interface().([]byte), key) return ptr.Interface() } diff --git a/iface/gql.go b/iface/gql.go index 8fd9ea74..730e5682 100644 --- a/iface/gql.go +++ b/iface/gql.go @@ -34,7 +34,7 @@ func init() { Name: "FacePktQueueInput", Description: "Packet queue configuration.", Fields: gqlserver.BindInputFields[PktQueueConfig](gqlserver.FieldTypes{ - reflect.TypeOf(nnduration.Nanoseconds(0)): nnduration.GqlNanoseconds, + reflect.TypeFor[nnduration.Nanoseconds](): nnduration.GqlNanoseconds, }), }) @@ -137,8 +137,8 @@ func init() { GqlCountersType = graphql.NewObject(graphql.ObjectConfig{ Name: "FaceCounters", Fields: gqlserver.BindFields[Counters](gqlserver.FieldTypes{ - reflect.TypeOf(RxCounters{}): GqlRxCountersType, - reflect.TypeOf(TxCounters{}): GqlTxCountersType, + reflect.TypeFor[RxCounters](): GqlRxCountersType, + reflect.TypeFor[TxCounters](): GqlTxCountersType, }), }) gqlserver.AddCounters(&gqlserver.CountersConfig{ diff --git a/iface/locator.go b/iface/locator.go index af1b064f..efa29c23 100644 --- a/iface/locator.go +++ b/iface/locator.go @@ -32,8 +32,7 @@ var locatorTypes = map[string]reflect.Type{} // RegisterLocatorScheme registers Locator schemes. func RegisterLocatorScheme[T Locator](schemes ...string) { - var loc T - typ := reflect.TypeOf(loc) + typ := reflect.TypeFor[T]() if typ.Kind() != reflect.Struct { logger.Panic("Locator must be a struct", zap.Stringer("type", typ)) } diff --git a/ndni/gql.go b/ndni/gql.go index 42c688b4..2acc06f4 100644 --- a/ndni/gql.go +++ b/ndni/gql.go @@ -50,8 +50,8 @@ func init() { }) GqlInterestTemplateFieldTypes = gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): gqlserver.NonNullString, - reflect.TypeOf(nnduration.Milliseconds(0)): nnduration.GqlMilliseconds, + reflect.TypeFor[ndn.Name](): gqlserver.NonNullString, + reflect.TypeFor[nnduration.Milliseconds](): nnduration.GqlMilliseconds, } GqlInterestTemplateInput = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "InterestTemplateInput", @@ -63,8 +63,8 @@ func init() { Name: "DataGenInput", Description: "Data generator template.", Fields: gqlserver.BindInputFields[DataGenConfig](gqlserver.FieldTypes{ - reflect.TypeOf(ndn.Name{}): gqlserver.NonNullString, - reflect.TypeOf(nnduration.Milliseconds(0)): nnduration.GqlMilliseconds, + reflect.TypeFor[ndn.Name](): gqlserver.NonNullString, + reflect.TypeFor[nnduration.Milliseconds](): nnduration.GqlMilliseconds, }), }) }