Skip to content

Commit

Permalink
mk: use Go 1.22 reflect.TypeFor
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Nov 27, 2024
1 parent f8bc12f commit 72fc3ef
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/fetch/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
})

Expand Down
6 changes: 3 additions & 3 deletions app/fileserver/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion app/fwdp/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions app/pdump/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
})

Expand Down
16 changes: 8 additions & 8 deletions app/tg/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
}),
})

Expand Down
18 changes: 9 additions & 9 deletions app/tgconsumer/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
})

Expand Down
12 changes: 6 additions & 6 deletions app/tgproducer/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
})

Expand All @@ -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,
}),
})

Expand Down
4 changes: 2 additions & 2 deletions core/gqlserver/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/gqlserver/gqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion core/gqlserver/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions core/nnduration/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions dpdk/ethdev/ethnetif/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
2 changes: 1 addition & 1 deletion dpdk/ethdev/ethnetif/xdp-face-map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
6 changes: 3 additions & 3 deletions iface/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
})

Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions iface/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
8 changes: 4 additions & 4 deletions ndni/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
}),
})
}

0 comments on commit 72fc3ef

Please sign in to comment.