Skip to content

Commit 65960d1

Browse files
Merge pull request #109 from vimeo/go-modernize-20260106
Modernize 20260106
2 parents 192b4e2 + 722b188 commit 65960d1

32 files changed

Lines changed: 190 additions & 246 deletions

decoders/yaml/yaml_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ func TestShallowlyNestedYAML(t *testing.T) {
7070
}`
7171

7272
myConfig := &testConfig{}
73-
ctx, cancel := context.WithCancel(context.Background())
74-
defer cancel()
73+
ctx := t.Context()
7574
d, err := dials.Config(
7675
ctx,
7776
myConfig,
@@ -127,8 +126,7 @@ func TestEmbedded(t *testing.T) {
127126
}`
128127

129128
myConfig := &testConfig{}
130-
ctx, cancel := context.WithCancel(context.Background())
131-
defer cancel()
129+
ctx := t.Context()
132130
d, err := dials.Config(
133131
ctx,
134132
myConfig,
@@ -235,8 +233,7 @@ func TestAnonymousNestedYAML(t *testing.T) {
235233
}`
236234

237235
myConfig := &testConfig{}
238-
ctx, cancel := context.WithCancel(context.Background())
239-
defer cancel()
236+
ctx := t.Context()
240237
d, err := dials.Config(
241238
ctx,
242239
myConfig,

dials.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (p Params[T]) Config(ctx context.Context, t *T, sources ...Source) (*Dials[
106106
watcherChan := make(chan watchStatusUpdate)
107107
computed := make([]sourceValue, len(sources))
108108

109-
typeOfT := reflect.TypeOf(t)
109+
typeOfT := reflect.TypeFor[*T]()
110110
if typeOfT.Kind() != reflect.Pointer {
111111
return nil, fmt.Errorf("config type %T is not a pointer", t)
112112
}
@@ -533,7 +533,6 @@ func (d *Dials[T]) updateSourceValue(
533533
}
534534

535535
func (d *Dials[T]) markSourceDone(
536-
ctx context.Context,
537536
sourceValues []sourceValue,
538537
watchTab *watcherDone,
539538
) bool {
@@ -729,7 +728,7 @@ func (d *Dials[T]) monitor(
729728
})
730729
}
731730
case *watcherDone:
732-
if !d.markSourceDone(ctx, sourceValues, v) {
731+
if !d.markSourceDone(sourceValues, v) {
733732
// if there are no watching sources, just exit.
734733
d.sourceVals.Store(&sourceValues)
735734
return

dials_test.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ func TestConfigWithoutVerifier(t *testing.T) {
8686
}
8787

8888
// setup a cancelable context so the monitor goroutine gets shutdown.
89-
ctx, cancel := context.WithCancel(context.Background())
90-
defer cancel()
89+
ctx := t.Context()
9190

9291
w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}}
9392
d, err := Config(ctx, &base, &fakeSource{outVal: emptyConf}, &w)
@@ -150,8 +149,7 @@ func TestConfigWithFailVerifier(t *testing.T) {
150149
}
151150

152151
// setup a cancelable context so the monitor goroutine gets shutdown.
153-
ctx, cancel := context.WithCancel(context.Background())
154-
defer cancel()
152+
ctx := t.Context()
155153

156154
w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}}
157155
_, err := Config(ctx, &base, &fakeSource{outVal: emptyConf}, &w)
@@ -184,8 +182,7 @@ func TestConfigWithSkippedInitialVerify(t *testing.T) {
184182
}
185183

186184
// setup a cancelable context so the monitor goroutine gets shutdown.
187-
ctx, cancel := context.WithCancel(context.Background())
188-
defer cancel()
185+
ctx := t.Context()
189186

190187
w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}}
191188
_, err := Params[testConfig]{
@@ -218,8 +215,7 @@ func TestConfigWithDelayInitialVerifyFailNoWatch(t *testing.T) {
218215
}
219216

220217
// setup a cancelable context so the monitor goroutine gets shutdown.
221-
ctx, cancel := context.WithCancel(context.Background())
222-
defer cancel()
218+
ctx := t.Context()
223219

224220
s := fakeSource{outVal: foozleConfig}
225221
d, err := Params[testConfig]{
@@ -257,8 +253,7 @@ func TestConfigWithDelayInitialVerifyFailWatchNoGlobalCBSuppress(t *testing.T) {
257253
}
258254

259255
// setup a cancelable context so the monitor goroutine gets shutdown.
260-
ctx, cancel := context.WithCancel(context.Background())
261-
defer cancel()
256+
ctx := t.Context()
262257

263258
globalCfgCh := make(chan *configurableVerifier, 1)
264259
w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}}
@@ -313,8 +308,7 @@ func TestConfigWithDelayInitialVerifyFailWatchlobalCBSuppress(t *testing.T) {
313308
}
314309

315310
// setup a cancelable context so the monitor goroutine gets shutdown.
316-
ctx, cancel := context.WithCancel(context.Background())
317-
defer cancel()
311+
ctx := t.Context()
318312

319313
errCBCh := make(chan error, 1)
320314
expglobalCBCall := make(chan struct{})
@@ -426,8 +420,7 @@ func TestConfigWithSuccessVerifier(t *testing.T) {
426420
}
427421

428422
// setup a cancelable context so the monitor goroutine gets shutdown.
429-
ctx, cancel := context.WithCancel(context.Background())
430-
defer cancel()
423+
ctx := t.Context()
431424

432425
w := fakeWatchingSource{fakeSource: fakeSource{outVal: foozleConfig}}
433426
d, err := Config(ctx, &base, &fakeSource{outVal: emptyConf}, &w)
@@ -502,8 +495,7 @@ func TestConfigWithConfigureVerifier(t *testing.T) {
502495
}
503496

504497
// setup a cancelable context so the monitor goroutine gets shutdown.
505-
ctx, cancel := context.WithCancel(context.Background())
506-
defer cancel()
498+
ctx := t.Context()
507499

508500
errCh := make(chan error, 1)
509501
params := Params[configurableVerifier]{
@@ -588,8 +580,7 @@ func TestWatcherWithDoneAndErrorCallback(t *testing.T) {
588580
}
589581

590582
// setup a cancelable context so the monitor goroutine gets shutdown.
591-
ctx, cancel := context.WithCancel(context.Background())
592-
defer cancel()
583+
ctx := t.Context()
593584

594585
reportedErrCh := make(chan error)
595586
p := Params[testConfig]{
@@ -673,8 +664,7 @@ func TestConfigWithNewConfigCallbacks(t *testing.T) {
673664
}
674665

675666
// setup a cancelable context so the monitor goroutine gets shutdown.
676-
ctx, cancel := context.WithCancel(context.Background())
677-
defer cancel()
667+
ctx := t.Context()
678668

679669
oldConf := make(chan *testConfig, 1)
680670
newConf := make(chan *testConfig)
@@ -792,8 +782,7 @@ func TestConfigWithNewConfigCallbacksSaturate(t *testing.T) {
792782
}
793783

794784
// setup a cancelable context so the monitor goroutine gets shutdown.
795-
ctx, cancel := context.WithCancel(context.Background())
796-
defer cancel()
785+
ctx := t.Context()
797786

798787
// give oldConf a large capacity (we don't want to block on both)
799788
oldConf := make(chan *testConfig, 128)

ez/ez_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func (c *config) ConfigPath() (string, bool) {
3333
// TestYAMLConfigEnvFlag cannot run concurrently with other tests because of
3434
// environment manipulation.
3535
func TestYAMLConfigEnvFlagWithValidConfig(t *testing.T) {
36-
ctx, cancel := context.WithCancel(context.Background())
37-
defer cancel()
36+
ctx := t.Context()
3837

3938
t.Setenv("CONFIGPATH", "../testhelper/testconfig.yaml")
4039

@@ -58,8 +57,7 @@ func TestYAMLConfigEnvFlagWithValidConfig(t *testing.T) {
5857
}
5958

6059
func TestYAMLConfigEnvFlagWithValidConfigAndAlias(t *testing.T) {
61-
ctx, cancel := context.WithCancel(context.Background())
62-
defer cancel()
60+
ctx := t.Context()
6361

6462
t.Setenv("ALTCONFIGPATH", "../testhelper/testconfig.yaml")
6563

@@ -94,8 +92,7 @@ func (bc *beatlesConfig) ConfigPath() (string, bool) {
9492
}
9593

9694
func TestYAMLConfigEnvFlagWithFileKeyNaming(t *testing.T) {
97-
ctx, cancel := context.WithCancel(context.Background())
98-
defer cancel()
95+
ctx := t.Context()
9996

10097
c := &beatlesConfig{YAMLPath: "../testhelper/testconfig.yaml"}
10198
view, dialsErr := YAMLConfigEnvFlag(ctx, c, Params[beatlesConfig]{
@@ -147,8 +144,7 @@ func (c *validatingConfig) Verify() error {
147144
}
148145

149146
func TestYAMLConfigEnvFlagWithValidatingConfig(t *testing.T) {
150-
ctx, cancel := context.WithCancel(context.Background())
151-
defer cancel()
147+
ctx := t.Context()
152148

153149
tmpFile, tmpErr := os.CreateTemp(t.TempDir(), "*")
154150
require.NoError(t, tmpErr)
@@ -164,8 +160,7 @@ func TestYAMLConfigEnvFlagWithValidatingConfig(t *testing.T) {
164160
}
165161

166162
func TestYAMLConfigEnvFlagWithValidatingConfigInitiallyValid(t *testing.T) {
167-
ctx, cancel := context.WithCancel(context.Background())
168-
defer cancel()
163+
ctx := t.Context()
169164

170165
tmpDir := t.TempDir()
171166
path := filepath.Join(tmpDir, "fim1.yaml")
@@ -204,8 +199,7 @@ func TestYAMLConfigEnvFlagWithValidatingConfigInitiallyValid(t *testing.T) {
204199
}
205200

206201
func TestJSONConfigEnvFlagWithNewConfigCallback(t *testing.T) {
207-
ctx, cancel := context.WithCancel(context.Background())
208-
defer cancel()
202+
ctx := t.Context()
209203

210204
tmpDir := t.TempDir()
211205
path := filepath.Join(tmpDir, "fim1.json")

helper/reflect_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestOnImplements(t *testing.T) {
13-
textUnmarshalerType := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
13+
textUnmarshalerType := reflect.TypeFor[encoding.TextUnmarshaler]()
1414

1515
for testName, itbl := range map[string]struct {
1616
seed func() any
@@ -25,7 +25,7 @@ func TestOnImplements(t *testing.T) {
2525
assert.Equal(t, "10.1.1.1", inputIP.String())
2626

2727
newType := v.Type()
28-
assert.Equal(t, reflect.PointerTo(reflect.TypeOf(net.IP{})), newType)
28+
assert.Equal(t, reflect.PointerTo(reflect.TypeFor[net.IP]()), newType)
2929

3030
outputIP, ok := v.Interface().(*net.IP)
3131
assert.True(t, ok)
@@ -51,7 +51,7 @@ func TestOnImplements(t *testing.T) {
5151
assert.Equal(t, "10.1.1.1", inputIP.String())
5252

5353
newType := v.Type()
54-
assert.Equal(t, reflect.PointerTo(reflect.TypeOf(net.IP{})), newType)
54+
assert.Equal(t, reflect.PointerTo(reflect.TypeFor[net.IP]()), newType)
5555

5656
outputIP, ok := v.Interface().(*net.IP)
5757
assert.True(t, ok)

parse/complex.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !go1.15
2-
// +build !go1.15
32

43
package parse
54

parse/complex_go115.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build go1.15
2-
// +build go1.15
32

43
package parse
54

parse/complex_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !go1.15
2-
// +build !go1.15
32

43
package parse
54

parse/map_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func TestParseMapForStringStringMaps(t *testing.T) {
113113
} {
114114
tbl := itbl
115115
t.Run(tbl.name, func(t *testing.T) {
116-
ss, err := Map(tbl.input, reflect.TypeOf(map[string]string{}))
116+
ss, err := Map(tbl.input, reflect.TypeFor[map[string]string]())
117117
if tbl.expectedErr != nil {
118118
assert.EqualError(t, err, tbl.expectedErr.Error())
119119
return

parse/number.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88
)
99

10-
var durationType = reflect.TypeOf(time.Duration(0))
10+
var durationType = reflect.TypeFor[time.Duration]()
1111

1212
func parseNumber(strVal string, numberType reflect.Type) (reflect.Value, error) {
1313
var castVal reflect.Value

0 commit comments

Comments
 (0)