From 722b18843d9ca96cf9d9a968c6ef88563bba0ded Mon Sep 17 00:00:00 2001 From: Sergio Salvatore Date: Tue, 6 Jan 2026 17:30:11 -0500 Subject: [PATCH] Modernize 20260106 Run `go modernize` on the whole codebase and get rid of some other cruft. --- decoders/yaml/yaml_test.go | 9 +- dials.go | 5 +- dials_test.go | 33 +++---- ez/ez_test.go | 18 ++-- helper/reflect_test.go | 6 +- parse/complex.go | 1 - parse/complex_go115.go | 1 - parse/complex_test.go | 1 - parse/map_test.go | 2 +- parse/number.go | 2 +- parse/parse_string.go | 2 +- ptrify/ptrify.go | 2 +- sources/file/file_test.go | 44 +++------ sources/flag/flag.go | 62 ++++++------- sources/flag/flag_test.go | 3 +- sources/flag/flaghelper/strings.go | 2 +- sources/pflag/pflag.go | 60 ++++++------- sources/pflag/pflag_test.go | 3 +- sourcewrap/blank_test.go | 16 +--- sourcewrap/transforming_source_test.go | 4 +- transform/alias_mangler_test.go | 4 +- transform/anonymous_flatten_mangler_test.go | 4 +- transform/flatten_mangler.go | 6 +- transform/flatten_mangler_test.go | 8 +- transform/set_slice_mangler_test.go | 2 +- transform/single_type_substitution_mangler.go | 4 +- .../single_type_substitution_mangler_test.go | 24 ++--- transform/string_casting_mangler.go | 2 +- transform/string_casting_mangler_test.go | 90 +++++++++---------- transform/text_unmarshaler_mangler.go | 2 +- transform/text_unmarshaler_mangler_test.go | 10 +-- transform/transformer_test.go | 4 +- 32 files changed, 190 insertions(+), 246 deletions(-) diff --git a/decoders/yaml/yaml_test.go b/decoders/yaml/yaml_test.go index c3b58ad..943ebf9 100644 --- a/decoders/yaml/yaml_test.go +++ b/decoders/yaml/yaml_test.go @@ -70,8 +70,7 @@ func TestShallowlyNestedYAML(t *testing.T) { }` myConfig := &testConfig{} - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config( ctx, myConfig, @@ -127,8 +126,7 @@ func TestEmbedded(t *testing.T) { }` myConfig := &testConfig{} - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config( ctx, myConfig, @@ -235,8 +233,7 @@ func TestAnonymousNestedYAML(t *testing.T) { }` myConfig := &testConfig{} - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config( ctx, myConfig, diff --git a/dials.go b/dials.go index fabc8e0..8581f05 100644 --- a/dials.go +++ b/dials.go @@ -106,7 +106,7 @@ func (p Params[T]) Config(ctx context.Context, t *T, sources ...Source) (*Dials[ watcherChan := make(chan watchStatusUpdate) computed := make([]sourceValue, len(sources)) - typeOfT := reflect.TypeOf(t) + typeOfT := reflect.TypeFor[*T]() if typeOfT.Kind() != reflect.Pointer { return nil, fmt.Errorf("config type %T is not a pointer", t) } @@ -533,7 +533,6 @@ func (d *Dials[T]) updateSourceValue( } func (d *Dials[T]) markSourceDone( - ctx context.Context, sourceValues []sourceValue, watchTab *watcherDone, ) bool { @@ -729,7 +728,7 @@ func (d *Dials[T]) monitor( }) } case *watcherDone: - if !d.markSourceDone(ctx, sourceValues, v) { + if !d.markSourceDone(sourceValues, v) { // if there are no watching sources, just exit. d.sourceVals.Store(&sourceValues) return diff --git a/dials_test.go b/dials_test.go index 88f5f7c..b1cfbc8 100644 --- a/dials_test.go +++ b/dials_test.go @@ -86,8 +86,7 @@ func TestConfigWithoutVerifier(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}} d, err := Config(ctx, &base, &fakeSource{outVal: emptyConf}, &w) @@ -150,8 +149,7 @@ func TestConfigWithFailVerifier(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}} _, err := Config(ctx, &base, &fakeSource{outVal: emptyConf}, &w) @@ -184,8 +182,7 @@ func TestConfigWithSkippedInitialVerify(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}} _, err := Params[testConfig]{ @@ -218,8 +215,7 @@ func TestConfigWithDelayInitialVerifyFailNoWatch(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s := fakeSource{outVal: foozleConfig} d, err := Params[testConfig]{ @@ -257,8 +253,7 @@ func TestConfigWithDelayInitialVerifyFailWatchNoGlobalCBSuppress(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() globalCfgCh := make(chan *configurableVerifier, 1) w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}} @@ -313,8 +308,7 @@ func TestConfigWithDelayInitialVerifyFailWatchlobalCBSuppress(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() errCBCh := make(chan error, 1) expglobalCBCall := make(chan struct{}) @@ -426,8 +420,7 @@ func TestConfigWithSuccessVerifier(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}} d, err := Config(ctx, &base, &fakeSource{outVal: emptyConf}, &w) @@ -502,8 +495,7 @@ func TestConfigWithConfigureVerifier(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() errCh := make(chan error, 1) params := Params[configurableVerifier]{ @@ -588,8 +580,7 @@ func TestWatcherWithDoneAndErrorCallback(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() reportedErrCh := make(chan error) p := Params[testConfig]{ @@ -673,8 +664,7 @@ func TestConfigWithNewConfigCallbacks(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() oldConf := make(chan *testConfig, 1) newConf := make(chan *testConfig) @@ -792,8 +782,7 @@ func TestConfigWithNewConfigCallbacksSaturate(t *testing.T) { } // setup a cancelable context so the monitor goroutine gets shutdown. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() // give oldConf a large capacity (we don't want to block on both) oldConf := make(chan *testConfig, 128) diff --git a/ez/ez_test.go b/ez/ez_test.go index aa92f9c..b5ab3a0 100644 --- a/ez/ez_test.go +++ b/ez/ez_test.go @@ -33,8 +33,7 @@ func (c *config) ConfigPath() (string, bool) { // TestYAMLConfigEnvFlag cannot run concurrently with other tests because of // environment manipulation. func TestYAMLConfigEnvFlagWithValidConfig(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() t.Setenv("CONFIGPATH", "../testhelper/testconfig.yaml") @@ -58,8 +57,7 @@ func TestYAMLConfigEnvFlagWithValidConfig(t *testing.T) { } func TestYAMLConfigEnvFlagWithValidConfigAndAlias(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() t.Setenv("ALTCONFIGPATH", "../testhelper/testconfig.yaml") @@ -94,8 +92,7 @@ func (bc *beatlesConfig) ConfigPath() (string, bool) { } func TestYAMLConfigEnvFlagWithFileKeyNaming(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() c := &beatlesConfig{YAMLPath: "../testhelper/testconfig.yaml"} view, dialsErr := YAMLConfigEnvFlag(ctx, c, Params[beatlesConfig]{ @@ -147,8 +144,7 @@ func (c *validatingConfig) Verify() error { } func TestYAMLConfigEnvFlagWithValidatingConfig(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() tmpFile, tmpErr := os.CreateTemp(t.TempDir(), "*") require.NoError(t, tmpErr) @@ -164,8 +160,7 @@ func TestYAMLConfigEnvFlagWithValidatingConfig(t *testing.T) { } func TestYAMLConfigEnvFlagWithValidatingConfigInitiallyValid(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() tmpDir := t.TempDir() path := filepath.Join(tmpDir, "fim1.yaml") @@ -204,8 +199,7 @@ func TestYAMLConfigEnvFlagWithValidatingConfigInitiallyValid(t *testing.T) { } func TestJSONConfigEnvFlagWithNewConfigCallback(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() tmpDir := t.TempDir() path := filepath.Join(tmpDir, "fim1.json") diff --git a/helper/reflect_test.go b/helper/reflect_test.go index 89cfbe5..688cdfc 100644 --- a/helper/reflect_test.go +++ b/helper/reflect_test.go @@ -10,7 +10,7 @@ import ( ) func TestOnImplements(t *testing.T) { - textUnmarshalerType := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() + textUnmarshalerType := reflect.TypeFor[encoding.TextUnmarshaler]() for testName, itbl := range map[string]struct { seed func() any @@ -25,7 +25,7 @@ func TestOnImplements(t *testing.T) { assert.Equal(t, "10.1.1.1", inputIP.String()) newType := v.Type() - assert.Equal(t, reflect.PointerTo(reflect.TypeOf(net.IP{})), newType) + assert.Equal(t, reflect.PointerTo(reflect.TypeFor[net.IP]()), newType) outputIP, ok := v.Interface().(*net.IP) assert.True(t, ok) @@ -51,7 +51,7 @@ func TestOnImplements(t *testing.T) { assert.Equal(t, "10.1.1.1", inputIP.String()) newType := v.Type() - assert.Equal(t, reflect.PointerTo(reflect.TypeOf(net.IP{})), newType) + assert.Equal(t, reflect.PointerTo(reflect.TypeFor[net.IP]()), newType) outputIP, ok := v.Interface().(*net.IP) assert.True(t, ok) diff --git a/parse/complex.go b/parse/complex.go index 3c60b9e..2939d85 100644 --- a/parse/complex.go +++ b/parse/complex.go @@ -1,5 +1,4 @@ //go:build !go1.15 -// +build !go1.15 package parse diff --git a/parse/complex_go115.go b/parse/complex_go115.go index 1bee31e..7b695d2 100644 --- a/parse/complex_go115.go +++ b/parse/complex_go115.go @@ -1,5 +1,4 @@ //go:build go1.15 -// +build go1.15 package parse diff --git a/parse/complex_test.go b/parse/complex_test.go index a81c35d..2092b00 100644 --- a/parse/complex_test.go +++ b/parse/complex_test.go @@ -1,5 +1,4 @@ //go:build !go1.15 -// +build !go1.15 package parse diff --git a/parse/map_test.go b/parse/map_test.go index c8a951c..3de636e 100644 --- a/parse/map_test.go +++ b/parse/map_test.go @@ -113,7 +113,7 @@ func TestParseMapForStringStringMaps(t *testing.T) { } { tbl := itbl t.Run(tbl.name, func(t *testing.T) { - ss, err := Map(tbl.input, reflect.TypeOf(map[string]string{})) + ss, err := Map(tbl.input, reflect.TypeFor[map[string]string]()) if tbl.expectedErr != nil { assert.EqualError(t, err, tbl.expectedErr.Error()) return diff --git a/parse/number.go b/parse/number.go index cfe5910..a3c681d 100644 --- a/parse/number.go +++ b/parse/number.go @@ -7,7 +7,7 @@ import ( "time" ) -var durationType = reflect.TypeOf(time.Duration(0)) +var durationType = reflect.TypeFor[time.Duration]() func parseNumber(strVal string, numberType reflect.Type) (reflect.Value, error) { var castVal reflect.Value diff --git a/parse/parse_string.go b/parse/parse_string.go index a40a61f..84fff8d 100644 --- a/parse/parse_string.go +++ b/parse/parse_string.go @@ -44,7 +44,7 @@ func String(str string, t reflect.Type) (reflect.Value, error) { case reflect.Map: switch t { - case reflect.TypeOf(map[string][]string{}): + case reflect.TypeFor[map[string][]string](): converted, err := StringStringSliceMap(str) if err != nil { return reflect.Value{}, err diff --git a/ptrify/ptrify.go b/ptrify/ptrify.go index 1041c08..a2069cd 100644 --- a/ptrify/ptrify.go +++ b/ptrify/ptrify.go @@ -11,7 +11,7 @@ import ( // Note: this looks weird because it is, you need to call TypeOf on a nil // pointer here then take the element type, otherwise you get a nil type and // that's not useful (it actually generates a panic when it's used further down). -var textUnmarshaler = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() +var textUnmarshaler = reflect.TypeFor[encoding.TextUnmarshaler]() // Pointerify takes a type and returns another type with all its members // set to pointers of their respective types diff --git a/sources/file/file_test.go b/sources/file/file_test.go index b572d71..1f90e9d 100644 --- a/sources/file/file_test.go +++ b/sources/file/file_test.go @@ -37,39 +37,28 @@ type config struct { NumBeatles int } -func tmpDir(t testing.TB) string { - t.Helper() - dir, dirErr := os.MkdirTemp("", "dials_file") - require.NoError(t, dirErr, "failed to create temporary directory") - return dir -} - func TestWatchingFile(t *testing.T) { t.Parallel() - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() firstConfig := writeTestConfig(t, dir, `{ "secretOfLife": 42, "numBeatles": 4 }`) - defer os.Remove(firstConfig) secondConfig := writeTestConfig(t, dir, `{ "secretOfLife": 47, "numBeatles": 4 }`) - defer os.Remove(secondConfig) myConfig := &config{} watchingFile, watchingErr := NewWatchingSource(firstConfig, &json.Decoder{}, WithLogger(&testStdLogger{t})) require.NoError(t, watchingErr, "construction failure") - defer watchingFile.WG.Wait() + t.Cleanup(watchingFile.WG.Wait) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config(ctx, myConfig, watchingFile) assert.NoError(t, err) @@ -100,20 +89,17 @@ func TestWatchingFileWithRelativePathAndChdir(t *testing.T) { defer os.Chdir(initWD) t.Parallel() - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() firstConfig := writeTestConfig(t, dir, `{ "secretOfLife": 42, "numBeatles": 4 }`) - defer os.Remove(firstConfig) secondConfig := writeTestConfig(t, dir, `{ "secretOfLife": 47, "numBeatles": 4 }`) - defer os.Remove(secondConfig) myConfig := &config{} @@ -124,10 +110,9 @@ func TestWatchingFileWithRelativePathAndChdir(t *testing.T) { watchingFile, watchingErr := NewWatchingSource(relFname, &json.Decoder{}, WithLogger(&testStdLogger{t})) require.NoError(t, watchingErr, "construction failure") - defer watchingFile.WG.Wait() + t.Cleanup(watchingFile.WG.Wait) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config(ctx, myConfig, watchingFile) assert.NoError(t, err) @@ -159,20 +144,18 @@ func TestWatchingFileWithRelativePathAndChdir(t *testing.T) { func TestWatchingFileWithRemove(t *testing.T) { t.Parallel() - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() firstConfig := writeTestConfig(t, dir, `{ "secretOfLife": 42, "numBeatles": 4 }`) - defer os.Remove(firstConfig) myConfig := &config{} watchingFile, watchingErr := NewWatchingSource(firstConfig, &json.Decoder{}, WithLogger(&testStdLogger{t})) require.NoError(t, watchingErr, "construction failure") - defer watchingFile.WG.Wait() + t.Cleanup(watchingFile.WG.Wait) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -212,20 +195,18 @@ func TestWatchingFileWithRemove(t *testing.T) { func TestWatchingFileWithTrickle(t *testing.T) { t.Parallel() - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() firstConfig := writeTestConfig(t, dir, `{ "secretOfLife": 42, "numBeatles": 4 }`) - defer os.Remove(firstConfig) myConfig := &config{} watchingFile, watchingErr := NewWatchingSource(firstConfig, &json.Decoder{}, WithLogger(&testStdLogger{t})) require.NoError(t, watchingErr, "construction failure") - defer watchingFile.WG.Wait() + t.Cleanup(watchingFile.WG.Wait) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -324,10 +305,9 @@ func TestWatchingFileWithK8SEmulatedAtomicWriter(t *testing.T) { watchingFile, watchingErr := NewWatchingSource(configPath, &json.Decoder{}, WithLogger(&testStdLogger{t})) require.NoError(t, watchingErr, "construction failure") - defer watchingFile.WG.Wait() + t.Cleanup(watchingFile.WG.Wait) - ctx, outerCancel := context.WithCancel(context.Background()) - defer outerCancel() + ctx := t.Context() d, err := dials.Config(ctx, myConfig, watchingFile) assert.NoError(t, err) diff --git a/sources/flag/flag.go b/sources/flag/flag.go index e40081f..a70a4ab 100644 --- a/sources/flag/flag.go +++ b/sources/flag/flag.go @@ -18,37 +18,37 @@ import ( ) var ( - timeTime = reflect.TypeOf(time.Time{}) - timeDuration = reflect.TypeOf(time.Nanosecond) - flagReflectType = reflect.TypeOf((*flag.Value)(nil)).Elem() - stringSlice = reflect.SliceOf(reflect.TypeOf("")) - mapStringStringSlice = reflect.MapOf(reflect.TypeOf(""), stringSlice) - mapStringString = reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf("")) - stringSet = reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(struct{}{})) - textMReflectType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() - - stringType = reflect.TypeOf("") - - boolType = reflect.TypeOf(false) - - float32Type = reflect.TypeOf(float32(0)) - float64Type = reflect.TypeOf(float64(0)) - - intType = reflect.TypeOf(int(0)) - int8Type = reflect.TypeOf(int8(0)) - int16Type = reflect.TypeOf(int16(0)) - int32Type = reflect.TypeOf(int32(0)) - int64Type = reflect.TypeOf(int64(0)) - - uintType = reflect.TypeOf(uint(0)) - uint8Type = reflect.TypeOf(uint8(0)) - uint16Type = reflect.TypeOf(uint16(0)) - uint32Type = reflect.TypeOf(uint32(0)) - uint64Type = reflect.TypeOf(uint64(0)) - uintptrType = reflect.TypeOf(uintptr(0)) - - complex64Type = reflect.TypeOf((*complex64)(nil)) - complex128Type = reflect.TypeOf((*complex128)(nil)) + timeTime = reflect.TypeFor[time.Time]() + timeDuration = reflect.TypeFor[time.Duration]() + flagReflectType = reflect.TypeFor[flag.Value]() + stringSlice = reflect.SliceOf(reflect.TypeFor[string]()) + mapStringStringSlice = reflect.MapOf(reflect.TypeFor[string](), stringSlice) + mapStringString = reflect.MapOf(reflect.TypeFor[string](), reflect.TypeFor[string]()) + stringSet = reflect.MapOf(reflect.TypeFor[string](), reflect.TypeOf(struct{}{})) + textMReflectType = reflect.TypeFor[encoding.TextUnmarshaler]() + + stringType = reflect.TypeFor[string]() + + boolType = reflect.TypeFor[bool]() + + float32Type = reflect.TypeFor[float32]() + float64Type = reflect.TypeFor[float64]() + + intType = reflect.TypeFor[int]() + int8Type = reflect.TypeFor[int8]() + int16Type = reflect.TypeFor[int16]() + int32Type = reflect.TypeFor[int32]() + int64Type = reflect.TypeFor[int64]() + + uintType = reflect.TypeFor[uint]() + uint8Type = reflect.TypeFor[uint8]() + uint16Type = reflect.TypeFor[uint16]() + uint32Type = reflect.TypeFor[uint32]() + uint64Type = reflect.TypeFor[uint64]() + uintptrType = reflect.TypeFor[uintptr]() + + complex64Type = reflect.TypeFor[*complex64]() + complex128Type = reflect.TypeFor[*complex128]() intSliceType = reflect.SliceOf(intType) int8SliceType = reflect.SliceOf(int8Type) diff --git a/sources/flag/flag_test.go b/sources/flag/flag_test.go index 67ba46b..35f53a2 100644 --- a/sources/flag/flag_test.go +++ b/sources/flag/flag_test.go @@ -1059,8 +1059,7 @@ func TestMust(t *testing.T) { fs := Must(NewSetWithArgs(DefaultFlagNameConfig(), &Config{}, []string{"-world", "-hello=foobar"})) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config(ctx, &Config{}, fs) if err != nil { diff --git a/sources/flag/flaghelper/strings.go b/sources/flag/flaghelper/strings.go index 0bdec99..bcf32d1 100644 --- a/sources/flag/flaghelper/strings.go +++ b/sources/flag/flaghelper/strings.go @@ -206,7 +206,7 @@ func NewMapStringStringFlag(m *map[string]string) *MapStringStringFlag { // Set implement pflag.Value and flag.Value func (v *MapStringStringFlag) Set(s string) error { - parsed, err := parse.Map(s, reflect.TypeOf(map[string]string{})) + parsed, err := parse.Map(s, reflect.TypeFor[map[string]string]()) if err != nil { return err } diff --git a/sources/pflag/pflag.go b/sources/pflag/pflag.go index 634b9b5..873e6bd 100644 --- a/sources/pflag/pflag.go +++ b/sources/pflag/pflag.go @@ -21,36 +21,36 @@ import ( var ( // the following types are unsupported by the pflag package but are supported // in dials pflag package. We check for these types so we can handle them appropriately - pflagReflectType = reflect.TypeOf((*pflag.Value)(nil)).Elem() - textMReflectType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() - timeDuration = reflect.TypeOf(time.Nanosecond) - stringSlice = reflect.SliceOf(reflect.TypeOf("")) - mapStringStringSlice = reflect.MapOf(reflect.TypeOf(""), stringSlice) - mapStringString = reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf("")) - stringSet = reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(struct{}{})) - - stringType = reflect.TypeOf("") - - boolType = reflect.TypeOf(false) - - float32Type = reflect.TypeOf(float32(0)) - float64Type = reflect.TypeOf(float64(0)) - - intType = reflect.TypeOf(int(0)) - int8Type = reflect.TypeOf(int8(0)) - int16Type = reflect.TypeOf(int16(0)) - int32Type = reflect.TypeOf(int32(0)) - int64Type = reflect.TypeOf(int64(0)) - - uintType = reflect.TypeOf(uint(0)) - uint8Type = reflect.TypeOf(uint8(0)) - uint16Type = reflect.TypeOf(uint16(0)) - uint32Type = reflect.TypeOf(uint32(0)) - uint64Type = reflect.TypeOf(uint64(0)) - uintptrType = reflect.TypeOf(uintptr(0)) - - complex64Type = reflect.TypeOf((*complex64)(nil)) - complex128Type = reflect.TypeOf((*complex128)(nil)) + pflagReflectType = reflect.TypeFor[pflag.Value]() + textMReflectType = reflect.TypeFor[encoding.TextUnmarshaler]() + timeDuration = reflect.TypeFor[time.Duration]() + stringSlice = reflect.SliceOf(reflect.TypeFor[string]()) + mapStringStringSlice = reflect.MapOf(reflect.TypeFor[string](), stringSlice) + mapStringString = reflect.MapOf(reflect.TypeFor[string](), reflect.TypeFor[string]()) + stringSet = reflect.MapOf(reflect.TypeFor[string](), reflect.TypeOf(struct{}{})) + + stringType = reflect.TypeFor[string]() + + boolType = reflect.TypeFor[bool]() + + float32Type = reflect.TypeFor[float32]() + float64Type = reflect.TypeFor[float64]() + + intType = reflect.TypeFor[int]() + int8Type = reflect.TypeFor[int8]() + int16Type = reflect.TypeFor[int16]() + int32Type = reflect.TypeFor[int32]() + int64Type = reflect.TypeFor[int64]() + + uintType = reflect.TypeFor[uint]() + uint8Type = reflect.TypeFor[uint8]() + uint16Type = reflect.TypeFor[uint16]() + uint32Type = reflect.TypeFor[uint32]() + uint64Type = reflect.TypeFor[uint64]() + uintptrType = reflect.TypeFor[uintptr]() + + complex64Type = reflect.TypeFor[*complex64]() + complex128Type = reflect.TypeFor[*complex128]() intSliceType = reflect.SliceOf(intType) int8SliceType = reflect.SliceOf(int8Type) diff --git a/sources/pflag/pflag_test.go b/sources/pflag/pflag_test.go index 9e5d22b..ee74071 100644 --- a/sources/pflag/pflag_test.go +++ b/sources/pflag/pflag_test.go @@ -893,8 +893,7 @@ func TestMust(t *testing.T) { fs := Must(NewSetWithArgs(DefaultFlagNameConfig(), &Config{}, []string{"--world", "--hello=foobar"})) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d, err := dials.Config(ctx, &Config{}, fs) if err != nil { diff --git a/sourcewrap/blank_test.go b/sourcewrap/blank_test.go index 9f85814..c7a9006 100644 --- a/sourcewrap/blank_test.go +++ b/sourcewrap/blank_test.go @@ -79,9 +79,7 @@ func (t *trivalErroringWatchingSource) Value(_ context.Context, typ *dials.Type) } func TestBlankSource(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - - defer cancel() + ctx := t.Context() b := Blank{} type basicConf struct { A int @@ -120,9 +118,7 @@ func TestBlankSource(t *testing.T) { } func TestBlankSourceError(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - - defer cancel() + ctx := t.Context() b := Blank{} type basicConf struct { A int @@ -169,9 +165,7 @@ func TestBlankSourceError(t *testing.T) { } func TestBlankSourceWatcher(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - - defer cancel() + ctx := t.Context() b := Blank{} type basicConf struct { A int @@ -228,9 +222,7 @@ func TestBlankSourceWatcher(t *testing.T) { } func TestBlankSourceErrorWatcher(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - - defer cancel() + ctx := t.Context() b := Blank{} type basicConf struct { A int diff --git a/sourcewrap/transforming_source_test.go b/sourcewrap/transforming_source_test.go index bbbd3ac..d2e6979 100644 --- a/sourcewrap/transforming_source_test.go +++ b/sourcewrap/transforming_source_test.go @@ -1,7 +1,6 @@ package sourcewrap import ( - "context" "encoding/json" "fmt" "io" @@ -35,8 +34,7 @@ func (tjd *trivialJSONDecoder) Decode(r io.Reader, dt *dials.Type) (reflect.Valu } func TestTransformingDecoder(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() type conf struct { Set map[string]struct{} diff --git a/transform/alias_mangler_test.go b/transform/alias_mangler_test.go index 5da29cc..be0da48 100644 --- a/transform/alias_mangler_test.go +++ b/transform/alias_mangler_test.go @@ -41,7 +41,7 @@ func TestAliasManglerMangle(t *testing.T) { t.Run(testName, func(t *testing.T) { sf := reflect.StructField{ Name: "Foo", - Type: reflect.TypeOf(""), + Type: reflect.TypeFor[string](), Tag: reflect.StructTag(tbl.tag), } @@ -77,7 +77,7 @@ func TestAliasManglerMangle(t *testing.T) { func TestAliasManglerUnmangle(t *testing.T) { sf := reflect.StructField{ Name: "Foo", - Type: reflect.TypeOf(""), + Type: reflect.TypeFor[string](), } num := 42 diff --git a/transform/anonymous_flatten_mangler_test.go b/transform/anonymous_flatten_mangler_test.go index d1015c6..8ec2dbd 100644 --- a/transform/anonymous_flatten_mangler_test.go +++ b/transform/anonymous_flatten_mangler_test.go @@ -29,7 +29,7 @@ func TestTopLevelAnonymousFlatten(t *testing.T) { World: "hello world", }, } - typeOfC := reflect.TypeOf(c) + typeOfC := reflect.TypeFor[*Config]() tVal := reflect.ValueOf(c) typeInstance := ptrify.Pointerify(typeOfC.Elem(), tVal.Elem()) @@ -112,7 +112,7 @@ func TestTopLevelAnonymousFlattenWithNils(t *testing.T) { World: "hello world", }, } - typeOfC := reflect.TypeOf(c) + typeOfC := reflect.TypeFor[*Config]() tVal := reflect.ValueOf(c) typeInstance := ptrify.Pointerify(typeOfC.Elem(), tVal.Elem()) diff --git a/transform/flatten_mangler.go b/transform/flatten_mangler.go index 0e27ab1..5b72a41 100644 --- a/transform/flatten_mangler.go +++ b/transform/flatten_mangler.go @@ -20,7 +20,7 @@ const ( ) // textMReflectType is a reflect.Type of TextUnmarshaler -var textMReflectType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() +var textMReflectType = reflect.TypeFor[encoding.TextUnmarshaler]() // FlattenMangler implements the Mangler interface type FlattenMangler struct { @@ -369,8 +369,8 @@ func GetField(sf reflect.StructField, v reflect.Value) reflect.Value { if fieldPath == "" { panic(fmt.Errorf("dialsfieldpath tag not set for field %s", sf.Name)) } - fields := strings.Split(fieldPath, ",") - for _, fname := range fields { + fields := strings.SplitSeq(fieldPath, ",") + for fname := range fields { v = stripPtrs(v) // if the struct isn't populated, return the zero value if !v.IsValid() { diff --git a/transform/flatten_mangler_test.go b/transform/flatten_mangler_test.go index 3ceae9a..2a743ea 100644 --- a/transform/flatten_mangler_test.go +++ b/transform/flatten_mangler_test.go @@ -319,9 +319,9 @@ func TestFlattenMangler(t *testing.T) { i2 := 42 val.Field(0).Set(reflect.ValueOf(&s1)) - val.Field(1).Set(reflect.Zero(reflect.TypeOf((*string)(nil)))) - val.Field(2).Set(reflect.Zero(reflect.TypeOf((*int)(nil)))) - val.Field(3).Set(reflect.Zero(reflect.TypeOf((*time.Duration)(nil)))) + val.Field(1).Set(reflect.Zero(reflect.TypeFor[*string]())) + val.Field(2).Set(reflect.Zero(reflect.TypeFor[*int]())) + val.Field(3).Set(reflect.Zero(reflect.TypeFor[*time.Duration]())) val.Field(4).Set(reflect.ValueOf(&i2)) }, assertion: func(t testing.TB, i any) { @@ -787,7 +787,7 @@ func TestTopLevelEmbed(t *testing.T) { World: "hello world", }, } - typeOfC := reflect.TypeOf(c) + typeOfC := reflect.TypeFor[*Config]() tVal := reflect.ValueOf(c) typeInstance := ptrify.Pointerify(typeOfC.Elem(), tVal.Elem()) diff --git a/transform/set_slice_mangler_test.go b/transform/set_slice_mangler_test.go index 7090147..6eb4be4 100644 --- a/transform/set_slice_mangler_test.go +++ b/transform/set_slice_mangler_test.go @@ -18,7 +18,7 @@ func TestSetSliceManglerMangle(t *testing.T) { require.NoError(t, err) - assert.Equal(t, reflect.TypeOf([]string{}), sfs[0].Type) + assert.Equal(t, reflect.TypeFor[[]string](), sfs[0].Type) } func TestSetSliceManglerUnmangle(t *testing.T) { diff --git a/transform/single_type_substitution_mangler.go b/transform/single_type_substitution_mangler.go index 9477645..df6725d 100644 --- a/transform/single_type_substitution_mangler.go +++ b/transform/single_type_substitution_mangler.go @@ -20,8 +20,8 @@ type SingleTypeSubstitutionMangler[F, T any] struct { // If T is not convertible to F, an error is returned. (only Unmangle converts // values back, so convertibility in the other direction is irrelevant) func NewSingleTypeSubstitutionMangler[F, T any]() (*SingleTypeSubstitutionMangler[F, T], error) { - from := reflect.TypeOf((*F)(nil)).Elem() - to := reflect.TypeOf((*T)(nil)).Elem() + from := reflect.TypeFor[F]() + to := reflect.TypeFor[T]() if !to.ConvertibleTo(from) { return nil, fmt.Errorf("type %s is not convertible to %s", to, from) } diff --git a/transform/single_type_substitution_mangler_test.go b/transform/single_type_substitution_mangler_test.go index 5685c3c..f47377c 100644 --- a/transform/single_type_substitution_mangler_test.go +++ b/transform/single_type_substitution_mangler_test.go @@ -46,15 +46,15 @@ func TestSingleTypeSubstitutionMangler_Int64_Int8(t *testing.T) { t.Fatalf("failed to construct substitution mangler: %s", constrErr) } - itype := reflect.TypeOf(testStruct{}) + itype := reflect.TypeFor[testStruct]() tfmr := NewTransformer(itype, m) val, trErr := tfmr.Translate() if trErr != nil { t.Fatalf("failed to translate type: %s", trErr) } - int8T := reflect.TypeOf(int8(8)) - strT := reflect.TypeOf("") + int8T := reflect.TypeFor[int8]() + strT := reflect.TypeFor[string]() bazChanVal := int8(31) checkFieldTypeAndSetVals(t, val, []checkFieldTypeAndSetValsFieldDesc{ @@ -142,15 +142,15 @@ func TestSingleTypeSubstitutionMangler_timeDuration_int64(t *testing.T) { t.Fatalf("failed to construct substitution mangler: %s", constrErr) } - itype := reflect.TypeOf(testStruct{}) + itype := reflect.TypeFor[testStruct]() tfmr := NewTransformer(itype, m) val, trErr := tfmr.Translate() if trErr != nil { t.Fatalf("failed to translate type: %s", trErr) } - int64T := reflect.TypeOf(int64(8)) - strT := reflect.TypeOf("") + int64T := reflect.TypeFor[int64]() + strT := reflect.TypeFor[string]() checkFieldTypeAndSetVals(t, val, []checkFieldTypeAndSetValsFieldDesc{ {fieldName: "Foo", expType: int64T, setVal: int64(8_000_023)}, @@ -203,15 +203,15 @@ func TestSingleTypeSubstitutionMangler_Int64_Int16_map_slice_fields(t *testing.T t.Fatalf("failed to construct substitution mangler: %s", constrErr) } - itype := reflect.TypeOf(testStruct{}) + itype := reflect.TypeFor[testStruct]() tfmr := NewTransformer(itype, m) val, trErr := tfmr.Translate() if trErr != nil { t.Fatalf("failed to translate type: %s", trErr) } - int16T := reflect.TypeOf(int16(8)) - strT := reflect.TypeOf("") + int16T := reflect.TypeFor[int16]() + strT := reflect.TypeFor[string]() checkFieldTypeAndSetVals(t, val, []checkFieldTypeAndSetValsFieldDesc{ {fieldName: "Foo", expType: reflect.MapOf(strT, int16T), setVal: map[string]int16{"abc": 8}}, @@ -275,15 +275,15 @@ func TestSingleTypeSubstitutionMangler_Int64_Int16_nested_fields(t *testing.T) { t.Fatalf("failed to construct substitution mangler: %s", constrErr) } - itype := reflect.TypeOf(testStruct{}) + itype := reflect.TypeFor[testStruct]() tfmr := NewTransformer(itype, m) val, trErr := tfmr.Translate() if trErr != nil { t.Fatalf("failed to translate type: %s", trErr) } - int16T := reflect.TypeOf(int16(8)) - strT := reflect.TypeOf("") + int16T := reflect.TypeFor[int16]() + strT := reflect.TypeFor[string]() iVal := val.FieldByName("I") checkFieldTypeAndSetVals(t, iVal, []checkFieldTypeAndSetValsFieldDesc{ diff --git a/transform/string_casting_mangler.go b/transform/string_casting_mangler.go index 75909b5..0cc15ec 100644 --- a/transform/string_casting_mangler.go +++ b/transform/string_casting_mangler.go @@ -8,7 +8,7 @@ import ( var ( zeroStr = "" - strPtrType = reflect.TypeOf(&zeroStr) + strPtrType = reflect.TypeFor[*string]() ) // StringCastingMangler mangles config struct fields into string types, then diff --git a/transform/string_casting_mangler_test.go b/transform/string_casting_mangler_test.go index 39927f6..d6177f1 100644 --- a/transform/string_casting_mangler_test.go +++ b/transform/string_casting_mangler_test.go @@ -15,7 +15,7 @@ import ( func TestStringCastingManglerMangle(t *testing.T) { m := StringCastingMangler{} sf := reflect.StructField{ - Type: reflect.TypeOf(0), + Type: reflect.TypeFor[int](), } sfs, err := m.Mangle(sf) @@ -32,131 +32,131 @@ func TestStringCastingManglerUnmangle(t *testing.T) { ExpectedErr string }{ "string": { - StructFieldType: reflect.TypeOf(""), + StructFieldType: reflect.TypeFor[string](), StringValue: "asdf", AssertFunc: func(i any) { assert.Equal(t, "asdf", *(i.(*string))) }, }, "bool": { - StructFieldType: reflect.TypeOf(false), + StructFieldType: reflect.TypeFor[bool](), StringValue: "true", AssertFunc: func(i any) { assert.Equal(t, true, *(i.(*bool))) }, }, "int": { - StructFieldType: reflect.TypeOf(0), + StructFieldType: reflect.TypeFor[int](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, 1, *(i.(*int))) }, }, "int8": { - StructFieldType: reflect.TypeOf(int8(0)), + StructFieldType: reflect.TypeFor[int8](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, int8(1), *(i.(*int8))) }, }, "int16": { - StructFieldType: reflect.TypeOf(int16(0)), + StructFieldType: reflect.TypeFor[int16](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, int16(1), *(i.(*int16))) }, }, "int32": { - StructFieldType: reflect.TypeOf(int32(0)), + StructFieldType: reflect.TypeFor[int32](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, int32(1), *(i.(*int32))) }, }, "int64": { - StructFieldType: reflect.TypeOf(int64(0)), + StructFieldType: reflect.TypeFor[int64](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, int64(1), *(i.(*int64))) }, }, "uint": { - StructFieldType: reflect.TypeOf(uint(0)), + StructFieldType: reflect.TypeFor[uint](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, uint(1), *(i.(*uint))) }, }, "uint8": { - StructFieldType: reflect.TypeOf(uint8(0)), + StructFieldType: reflect.TypeFor[uint8](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, uint8(1), *(i.(*uint8))) }, }, "uint16": { - StructFieldType: reflect.TypeOf(uint16(0)), + StructFieldType: reflect.TypeFor[uint16](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, uint16(1), *(i.(*uint16))) }, }, "uint32": { - StructFieldType: reflect.TypeOf(uint32(0)), + StructFieldType: reflect.TypeFor[uint32](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, uint32(1), *(i.(*uint32))) }, }, "uint64": { - StructFieldType: reflect.TypeOf(uint64(0)), + StructFieldType: reflect.TypeFor[uint64](), StringValue: "1", AssertFunc: func(i any) { assert.Equal(t, uint64(1), *(i.(*uint64))) }, }, "float32": { - StructFieldType: reflect.TypeOf(float32(1.0)), + StructFieldType: reflect.TypeFor[float32](), StringValue: "1.5", AssertFunc: func(i any) { assert.Equal(t, float32(1.5), *(i.(*float32))) }, }, "float64": { - StructFieldType: reflect.TypeOf(1.0), + StructFieldType: reflect.TypeFor[float64](), StringValue: "1.9", AssertFunc: func(i any) { assert.Equal(t, 1.9, *(i.(*float64))) }, }, "complex64": { - StructFieldType: reflect.TypeOf(complex64(10 + 3i)), + StructFieldType: reflect.TypeFor[complex64](), StringValue: "10+3i", AssertFunc: func(i any) { assert.Equal(t, complex64(10+3i), *(i.(*complex64))) }, }, "complex128": { - StructFieldType: reflect.TypeOf(complex128(10 + 3i)), + StructFieldType: reflect.TypeFor[complex128](), StringValue: "10+3i", AssertFunc: func(i any) { assert.Equal(t, complex128(10+3i), *(i.(*complex128))) }, }, "duration": { - StructFieldType: reflect.TypeOf(time.Duration(0)), + StructFieldType: reflect.TypeFor[time.Duration](), StringValue: "1h", AssertFunc: func(i any) { assert.Equal(t, time.Duration(3600000000000), *(i.(*time.Duration))) }, }, "duration_error": { - StructFieldType: reflect.TypeOf(time.Duration(0)), + StructFieldType: reflect.TypeFor[time.Duration](), StringValue: "1", ExpectedErr: "missing unit in duration ", }, "string_slice": { - StructFieldType: reflect.TypeOf([]string{}), + StructFieldType: reflect.TypeFor[[]string](), StringValue: `a,b,c`, AssertFunc: func(i any) { expected := []string{"a", "b", "c"} @@ -165,7 +165,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "int_slice": { - StructFieldType: reflect.TypeOf([]int{}), + StructFieldType: reflect.TypeFor[[]int](), StringValue: `1,2,3`, AssertFunc: func(i any) { expected := []int{1, 2, 3} @@ -174,7 +174,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "int8_slice": { - StructFieldType: reflect.TypeOf([]int8{}), + StructFieldType: reflect.TypeFor[[]int8](), StringValue: `1,2,3`, AssertFunc: func(i any) { expected := []int8{1, 2, 3} @@ -183,7 +183,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "float64_slice": { - StructFieldType: reflect.TypeOf([]float64{}), + StructFieldType: reflect.TypeFor[[]float64](), StringValue: `1.1, 2.1, 3.1`, AssertFunc: func(i any) { expected := []float64{1.1, 2.1, 3.1} @@ -192,7 +192,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "complex128_slice": { - StructFieldType: reflect.TypeOf([]complex128{}), + StructFieldType: reflect.TypeFor[[]complex128](), StringValue: `"10+3i", "5+2i", "3+3i"`, AssertFunc: func(i any) { expected := []complex128{10 + 3i, 5 + 2i, 3 + 3i} @@ -201,7 +201,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "string_string_map": { - StructFieldType: reflect.TypeOf(map[string]string{}), + StructFieldType: reflect.TypeFor[map[string]string](), StringValue: `"Origin": "foobar", "Referer": "fimbat"`, AssertFunc: func(i any) { expected := map[string]string{ @@ -213,7 +213,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "string_string_slice_map": { - StructFieldType: reflect.TypeOf(map[string][]string{}), + StructFieldType: reflect.TypeFor[map[string][]string](), StringValue: `"Origin": "foobar", "Origin": "foobat", "Referer": "fimbat"`, AssertFunc: func(i any) { expected := map[string][]string{ @@ -225,7 +225,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "int_int_map": { - StructFieldType: reflect.TypeOf(map[int]int{}), + StructFieldType: reflect.TypeFor[map[int]int](), StringValue: `1: 1, 2: 3, 10: 8`, AssertFunc: func(i any) { expected := map[int]int{ @@ -238,7 +238,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "int_str_map": { - StructFieldType: reflect.TypeOf(map[int]string{}), + StructFieldType: reflect.TypeFor[map[int]string](), StringValue: `1: "a", 2: "b", 10: "c"`, AssertFunc: func(i any) { expected := map[int]string{ @@ -251,7 +251,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "str_int_map": { - StructFieldType: reflect.TypeOf(map[string]int{}), + StructFieldType: reflect.TypeFor[map[string]int](), StringValue: `"a": 1, "b": 2, "c": 10`, AssertFunc: func(i any) { expected := map[string]int{ @@ -265,7 +265,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "str_bool_map": { - StructFieldType: reflect.TypeOf(map[string]bool{}), + StructFieldType: reflect.TypeFor[map[string]bool](), StringValue: `"a": true, "b": false, "c": true`, AssertFunc: func(i any) { expected := map[string]bool{ @@ -278,7 +278,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "str_complex_map": { - StructFieldType: reflect.TypeOf(map[string]complex128{}), + StructFieldType: reflect.TypeFor[map[string]complex128](), StringValue: `"asdf": "3+5i", "b": "3+5i", "c": "3+5i"`, AssertFunc: func(i any) { expected := map[string]complex128{ @@ -291,7 +291,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "complex_bool_map": { - StructFieldType: reflect.TypeOf(map[complex64]bool{}), + StructFieldType: reflect.TypeFor[map[complex64]bool](), StringValue: `"3+5i": true, "10+5i": false, "1+2i": true`, AssertFunc: func(i any) { expected := map[complex64]bool{ @@ -304,7 +304,7 @@ func TestStringCastingManglerUnmangle(t *testing.T) { }, }, "invalid_map": { - StructFieldType: reflect.TypeOf(map[string][]int{}), + StructFieldType: reflect.TypeFor[map[string][]int](), StringValue: `"asdf": 1, "asdf": 2, "zxcv": 3`, ExpectedErr: "unsupported map type", }, @@ -360,51 +360,51 @@ func TestParseOverflow(t *testing.T) { StringValue string }{ "int8": { - StructFieldType: reflect.TypeOf(int8(0)), + StructFieldType: reflect.TypeFor[int8](), StringValue: "128", }, "int16": { - StructFieldType: reflect.TypeOf(int16(0)), + StructFieldType: reflect.TypeFor[int16](), StringValue: "32768", }, "int32": { - StructFieldType: reflect.TypeOf(int32(0)), + StructFieldType: reflect.TypeFor[int32](), StringValue: "2147483648", }, "int64": { - StructFieldType: reflect.TypeOf(int64(0)), + StructFieldType: reflect.TypeFor[int64](), StringValue: "9223372036854775808", }, "uint8": { - StructFieldType: reflect.TypeOf(uint8(0)), + StructFieldType: reflect.TypeFor[uint8](), StringValue: "256", }, "uint16": { - StructFieldType: reflect.TypeOf(uint16(0)), + StructFieldType: reflect.TypeFor[uint16](), StringValue: "65537", }, "uint32": { - StructFieldType: reflect.TypeOf(uint32(0)), + StructFieldType: reflect.TypeFor[uint32](), StringValue: "4294967296", }, "uint64": { - StructFieldType: reflect.TypeOf(uint64(0)), + StructFieldType: reflect.TypeFor[uint64](), StringValue: "18446744073709551616", }, "float32": { - StructFieldType: reflect.TypeOf(float32(0.0)), + StructFieldType: reflect.TypeFor[float32](), StringValue: "1e+40", }, "float64": { - StructFieldType: reflect.TypeOf(float64(0.0)), + StructFieldType: reflect.TypeFor[float64](), StringValue: "1e+400", }, "complex64": { - StructFieldType: reflect.TypeOf(complex64(0)), + StructFieldType: reflect.TypeFor[complex64](), StringValue: "1e+400", }, "complex128": { - StructFieldType: reflect.TypeOf(complex64(0)), + StructFieldType: reflect.TypeFor[complex64](), StringValue: "1e+400", }, } diff --git a/transform/text_unmarshaler_mangler.go b/transform/text_unmarshaler_mangler.go index aa4447e..2d3178b 100644 --- a/transform/text_unmarshaler_mangler.go +++ b/transform/text_unmarshaler_mangler.go @@ -9,7 +9,7 @@ import ( ) var ( - textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() + textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]() ) // TextUnmarshalerMangler changes types that implement encoding.TextUnmarshaler diff --git a/transform/text_unmarshaler_mangler_test.go b/transform/text_unmarshaler_mangler_test.go index 282fec0..331592a 100644 --- a/transform/text_unmarshaler_mangler_test.go +++ b/transform/text_unmarshaler_mangler_test.go @@ -27,21 +27,21 @@ func TestTextUnmarshalerManglerUnmangle(t *testing.T) { ExpectedErr string }{ "TextUnmarshaler": { - StructFieldType: reflect.TypeOf(net.IP{}), + StructFieldType: reflect.TypeFor[net.IP](), StringValue: "10.0.0.1", AssertFunc: func(t testing.TB, i any) { assert.Equal(t, net.ParseIP("10.0.0.1"), i) }, }, "*TextUnmarshaler": { - StructFieldType: reflect.TypeOf(&net.IP{}), + StructFieldType: reflect.TypeFor[*net.IP](), StringValue: "10.0.0.2", AssertFunc: func(t testing.TB, i any) { assert.Equal(t, net.ParseIP("10.0.0.2"), *(i.(*net.IP))) }, }, "TextUnmarshalerNil": { - StructFieldType: reflect.TypeOf(net.IP{}), + StructFieldType: reflect.TypeFor[net.IP](), StringValue: "", AssertFunc: func(t testing.TB, i any) { var ip net.IP @@ -49,7 +49,7 @@ func TestTextUnmarshalerManglerUnmangle(t *testing.T) { }, }, "CustomStructType": { - StructFieldType: reflect.TypeOf(simpleTextUnmarshaler{}), + StructFieldType: reflect.TypeFor[simpleTextUnmarshaler](), StringValue: "foo", AssertFunc: func(t testing.TB, i any) { stm, ok := i.(*simpleTextUnmarshaler) @@ -58,7 +58,7 @@ func TestTextUnmarshalerManglerUnmangle(t *testing.T) { }, }, "NotTextUnmarshaler": { - StructFieldType: reflect.TypeOf(map[string]any{}), + StructFieldType: reflect.TypeFor[map[string]any](), StringValue: "", AssertFunc: func(t testing.TB, i any) { var m map[string]any diff --git a/transform/transformer_test.go b/transform/transformer_test.go index b88ea67..9551019 100644 --- a/transform/transformer_test.go +++ b/transform/transformer_test.go @@ -61,8 +61,8 @@ func strPtr(in string) *string { } func TestTransformer(t *testing.T) { - intType := reflect.TypeOf(int(1)) - strType := reflect.TypeOf(string("")) + intType := reflect.TypeFor[int]() + strType := reflect.TypeFor[string]() for _, itbl := range []struct { name string inStruct any