From 86422af988555404d3339b7ff83105f4becf1131 Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Thu, 21 Aug 2025 22:10:07 +0800 Subject: [PATCH] Refactor to use reflect.TypeFor --- internal/parser.go | 2 +- pkg/api/handler.go | 2 +- pkg/api/handler_overview.go | 2 +- pkg/collector/hydratation/hydration.go | 2 +- pkg/redactor/redactor.go | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/parser.go b/internal/parser.go index 3fa85617e..b9e474130 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -47,7 +47,7 @@ func encodeRawValue(labels map[string]string, root string, rawValue interface{}) if tValue.Kind() == reflect.Map && tValue.Elem().Kind() == reflect.Interface { r := reflect.ValueOf(rawValue). - Convert(reflect.TypeOf((map[string]interface{})(nil))). + Convert(reflect.TypeFor[map[string]interface{}]()). Interface().(map[string]interface{}) for k, v := range r { diff --git a/pkg/api/handler.go b/pkg/api/handler.go index 922779e61..6c99ca12e 100644 --- a/pkg/api/handler.go +++ b/pkg/api/handler.go @@ -156,7 +156,7 @@ func extractType(element interface{}) string { for i := range v.NumField() { field := v.Field(i) - if field.Kind() == reflect.Map && field.Type().Elem() == reflect.TypeOf(dynamic.PluginConf{}) { + if field.Kind() == reflect.Map && field.Type().Elem() == reflect.TypeFor[dynamic.PluginConf]() { if keys := field.MapKeys(); len(keys) == 1 { return keys[0].String() } diff --git a/pkg/api/handler_overview.go b/pkg/api/handler_overview.go index 7b15d513e..9279370a6 100644 --- a/pkg/api/handler_overview.go +++ b/pkg/api/handler_overview.go @@ -232,7 +232,7 @@ func getProviders(conf static.Configuration) []string { if !field.IsNil() { providers = append(providers, v.Type().Field(i).Name) } - } else if field.Kind() == reflect.Map && field.Type().Elem() == reflect.TypeOf(static.PluginConf{}) { + } else if field.Kind() == reflect.Map && field.Type().Elem() == reflect.TypeFor[static.PluginConf]() { for _, value := range field.MapKeys() { providers = append(providers, "plugin-"+value.String()) } diff --git a/pkg/collector/hydratation/hydration.go b/pkg/collector/hydratation/hydration.go index c45cfcfbb..74009817a 100644 --- a/pkg/collector/hydratation/hydration.go +++ b/pkg/collector/hydratation/hydration.go @@ -55,7 +55,7 @@ func fill(field reflect.Value) error { setTyped(field, int32(defaultNumber)) case reflect.Int64: switch field.Type() { - case reflect.TypeOf(types.Duration(time.Second)): + case reflect.TypeFor[types.Duration](): setTyped(field, types.Duration(defaultNumber*time.Second)) default: setTyped(field, int64(defaultNumber)) diff --git a/pkg/redactor/redactor.go b/pkg/redactor/redactor.go index c28feaa1a..cbe4a6c5c 100644 --- a/pkg/redactor/redactor.go +++ b/pkg/redactor/redactor.go @@ -70,7 +70,7 @@ func doOnJSON(input string) string { } func doOnStruct(field reflect.Value, tag string, redactByDefault bool) error { - if field.Type().AssignableTo(reflect.TypeOf(dynamic.PluginConf{})) { + if field.Type().AssignableTo(reflect.TypeFor[dynamic.PluginConf]()) { resetPlugin(field) return nil } @@ -164,7 +164,7 @@ func reset(field reflect.Value, name string) error { } case reflect.String: if field.String() != "" { - if field.Type().AssignableTo(reflect.TypeOf(types.FileOrContent(""))) { + if field.Type().AssignableTo(reflect.TypeFor[types.FileOrContent]()) { field.Set(reflect.ValueOf(types.FileOrContent(maskShort))) } else { field.Set(reflect.ValueOf(maskShort))