diff --git a/annotated.go b/annotated.go index 03eebc821..4a732f1e3 100644 --- a/annotated.go +++ b/annotated.go @@ -79,7 +79,7 @@ type Annotated struct { Group string // Target is the constructor or value being annotated with fx.Annotated. - Target interface{} + Target any } func (a Annotated) String() string { @@ -116,7 +116,7 @@ var ( // or with [Supply], for a value. type Annotation interface { apply(*annotated) error - build(*annotated) (interface{}, error) + build(*annotated) (any, error) } var ( @@ -129,7 +129,7 @@ var ( // that it encountered as well as the target interface that was attempted // to be annotated. type annotationError struct { - target interface{} + target any err error } @@ -247,7 +247,7 @@ func (pt paramTagsAnnotation) apply(ann *annotated) error { } // build builds and returns a constructor after applying a ParamTags annotation -func (pt paramTagsAnnotation) build(ann *annotated) (interface{}, error) { +func (pt paramTagsAnnotation) build(ann *annotated) (any, error) { paramTypes, remap := pt.parameters(ann) resultTypes, _ := ann.currentResultTypes() @@ -390,7 +390,7 @@ func (rt resultTagsAnnotation) apply(ann *annotated) error { } // build builds and returns a constructor after applying a ResultTags annotation -func (rt resultTagsAnnotation) build(ann *annotated) (interface{}, error) { +func (rt resultTagsAnnotation) build(ann *annotated) (any, error) { paramTypes := ann.currentParamTypes() resultTypes, remapResults := rt.results(ann) origFn := reflect.ValueOf(ann.Target) @@ -558,7 +558,7 @@ const ( type lifecycleHookAnnotation struct { Type _lifecycleHookAnnotationType - Target interface{} + Target any } var _ Annotation = (*lifecycleHookAnnotation)(nil) @@ -625,7 +625,7 @@ func (la *lifecycleHookAnnotation) apply(ann *annotated) error { } // build builds and returns a constructor after applying a lifecycle hook annotation. -func (la *lifecycleHookAnnotation) build(ann *annotated) (interface{}, error) { +func (la *lifecycleHookAnnotation) build(ann *annotated) (any, error) { resultTypes, hasError := ann.currentResultTypes() if !hasError { resultTypes = append(resultTypes, _typeOfError) @@ -809,7 +809,7 @@ func (la *lifecycleHookAnnotation) buildHookInstaller(ann *annotated) ( // parameter is a context, inject the provided context. if ctxStructPos < 0 { offset := 0 - for i := 0; i < len(hookArgs); i++ { + for i := range hookArgs { if i == ctxPos { hookArgs[i] = reflect.ValueOf(ctx) offset = 1 @@ -887,7 +887,7 @@ var ( // that the lifecycle hook being appended can depend on. It also deduplicates // duplicate param and result types, which is possible when using fx.Decorate, // and uses values from results for providing the deduplicated types. -func makeHookScopeCtor(paramTypes []reflect.Type, resultTypes []reflect.Type, args []reflect.Value) interface{} { +func makeHookScopeCtor(paramTypes []reflect.Type, resultTypes []reflect.Type, args []reflect.Value) any { type key struct { t reflect.Type name string @@ -1112,7 +1112,7 @@ func (la *lifecycleHookAnnotation) buildHook(fn func(context.Context) error) (ho // as OnStop. The hook function passed into OnStart cannot take any arguments // outside of the annotated constructor's existing dependencies or results, except // a context.Context. -func OnStart(onStart interface{}) Annotation { +func OnStart(onStart any) Annotation { return &lifecycleHookAnnotation{ Type: _onStartHookType, Target: onStart, @@ -1176,7 +1176,7 @@ func OnStart(onStart interface{}) Annotation { // as OnStart. The hook function passed into OnStop cannot take any arguments // outside of the annotated constructor's existing dependencies or results, except // a context.Context. -func OnStop(onStop interface{}) Annotation { +func OnStop(onStop any) Annotation { return &lifecycleHookAnnotation{ Type: _onStopHookType, Target: onStop, @@ -1184,7 +1184,7 @@ func OnStop(onStop interface{}) Annotation { } type asAnnotation struct { - targets []interface{} + targets []any types []asType } @@ -1256,7 +1256,7 @@ var _ Annotation = (*asAnnotation)(nil) // to maintain the original return types when using As, see [Self]. // // As annotation cannot be used in a function that returns an [Out] struct as a return type. -func As(interfaces ...interface{}) Annotation { +func As(interfaces ...any) Annotation { return &asAnnotation{targets: interfaces} } @@ -1311,7 +1311,7 @@ func (at *asAnnotation) apply(ann *annotated) error { } // build implements Annotation -func (at *asAnnotation) build(ann *annotated) (interface{}, error) { +func (at *asAnnotation) build(ann *annotated) (any, error) { paramTypes := ann.currentParamTypes() resultTypes, remapResults, err := at.results(ann) @@ -1430,7 +1430,7 @@ func extractResultFields(types []reflect.Type) ([]reflect.StructField, func(int, } type fromAnnotation struct { - targets []interface{} + targets []any types []reflect.Type } @@ -1483,7 +1483,7 @@ var _ Annotation = (*fromAnnotation)(nil) // // From annotation cannot be used in a function that takes an [In] struct as a // parameter. -func From(interfaces ...interface{}) Annotation { +func From(interfaces ...any) Annotation { return &fromAnnotation{targets: interfaces} } @@ -1508,7 +1508,7 @@ func (fr *fromAnnotation) apply(ann *annotated) error { } // build builds and returns a constructor after applying a From annotation -func (fr *fromAnnotation) build(ann *annotated) (interface{}, error) { +func (fr *fromAnnotation) build(ann *annotated) (any, error) { paramTypes, remap, err := fr.parameters(ann) if err != nil { return nil, err @@ -1613,7 +1613,7 @@ func (fr *fromAnnotation) parameters(ann *annotated) ( } type annotated struct { - Target interface{} + Target any Annotations []Annotation ParamTags []string ResultTags []string @@ -1647,7 +1647,7 @@ func (ann annotated) String() string { // Build builds and returns a constructor based on fx.In/fx.Out params and // results wrapping the original constructor passed to fx.Annotate. -func (ann *annotated) Build() (interface{}, error) { +func (ann *annotated) Build() (any, error) { ann.container = dig.New() ft := reflect.TypeOf(ann.Target) if ft.Kind() != reflect.Func { @@ -1758,7 +1758,7 @@ func (ann *annotated) cleanUpAsResults() { func (ann *annotated) typeCheckOrigFn() error { ft := reflect.TypeOf(ann.Target) numOut := ft.NumOut() - for i := 0; i < numOut; i++ { + for i := range numOut { ot := ft.Out(i) if ot == _typeOfError && i != numOut-1 { return fmt.Errorf( @@ -1796,7 +1796,7 @@ func (ann *annotated) currentResultTypes() (resultTypes []reflect.Type, hasError numOut := ft.NumOut() resultTypes = make([]reflect.Type, numOut) - for i := 0; i < numOut; i++ { + for i := range numOut { resultTypes[i] = ft.Out(i) if resultTypes[i] == _typeOfError && i == numOut-1 { hasError = true @@ -1898,7 +1898,7 @@ func (ann *annotated) currentParamTypes() []reflect.Type { // Target: func(..) http.Handler { ... }, // Group: "server", // } -func Annotate(t interface{}, anns ...Annotation) interface{} { +func Annotate(t any, anns ...Annotation) any { result := annotated{Target: t} for _, ann := range anns { if err := ann.apply(&result); err != nil { diff --git a/annotated_test.go b/annotated_test.go index fc05bba03..e10fb120c 100644 --- a/annotated_test.go +++ b/annotated_test.go @@ -110,7 +110,7 @@ func TestAnnotatedFrom(t *testing.T) { tests := []struct { desc string provide fx.Option - invoke interface{} + invoke any }{ { desc: "provide a good stringer", @@ -275,7 +275,6 @@ func TestAnnotatedFrom(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -304,7 +303,7 @@ func TestAnnotatedFromFailures(t *testing.T) { tests := []struct { desc string provide fx.Option - invoke interface{} + invoke any errorContains string }{ { @@ -409,7 +408,6 @@ func TestAnnotatedFromFailures(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() app := NewForTest(t, @@ -452,7 +450,7 @@ func TestAnnotatedAs(t *testing.T) { tests := []struct { desc string provide fx.Option - invoke interface{} + invoke any startApp bool }{ { @@ -802,7 +800,6 @@ func TestAnnotatedAs(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -837,7 +834,7 @@ func TestAnnotatedAsFailures(t *testing.T) { tests := []struct { desc string provide fx.Option - invoke interface{} + invoke any errorContains string }{ { @@ -895,7 +892,6 @@ func TestAnnotatedAsFailures(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() app := NewForTest(t, @@ -1044,7 +1040,6 @@ func TestAnnotatedString(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -1108,7 +1103,7 @@ func TestAnnotate(t *testing.T) { app := fxtest.New(t, fx.Provide( fx.Annotate(newB, fx.ParamTags(`optional:"true"`)), - fx.Annotate(func(a *a, b *b) interface{} { return nil }, + fx.Annotate(func(a *a, b *b) any { return nil }, fx.ParamTags(`name:"a" optional:"true"`, `name:"b"`), fx.ResultTags(`name:"nil"`), ), @@ -1553,7 +1548,7 @@ func TestAnnotate(t *testing.T) { t.Run("annotate a fx.Out with As", func(t *testing.T) { t.Parallel() - type I interface{} + type I any type B struct { // implements I @@ -1606,7 +1601,7 @@ func TestAnnotate(t *testing.T) { t.Run("annotate a fx.In with From", func(t *testing.T) { t.Parallel() - type I interface{} + type I any type B struct { // implements I @@ -1903,7 +1898,7 @@ func TestHookAnnotations(t *testing.T) { t.Run("start and stop without dependencies", func(t *testing.T) { t.Parallel() - type stub interface{} + type stub any var ( invoked bool @@ -1937,9 +1932,9 @@ func TestHookAnnotations(t *testing.T) { t.Parallel() type ( - A interface{} - B interface{} - C interface{} + A any + B any + C any ) var value int @@ -2111,7 +2106,7 @@ func TestHookAnnotations(t *testing.T) { t.Run("with Supply and Decorate", func(t *testing.T) { t.Parallel() - type A interface{} + type A any ch := make(chan string, 3) @@ -2178,7 +2173,7 @@ func TestHookAnnotations(t *testing.T) { app := fxtest.New(t, fx.Provide( fx.Annotate(newB, fx.ParamTags(`optional:"true"`)), - fx.Annotate(func(a *a, b *b) interface{} { return nil }, + fx.Annotate(func(a *a, b *b) any { return nil }, fx.ParamTags(`name:"a" optional:"true"`, `name:"b"`), fx.ResultTags(`name:"nil"`), fx.OnStart(func(_ paramStruct) error { @@ -2287,8 +2282,8 @@ func TestHookAnnotationFailures(t *testing.T) { } type ( - A interface{} - B interface{} + A any + B any ) type namedAndGroupHookParams struct { @@ -2311,7 +2306,7 @@ func TestHookAnnotationFailures(t *testing.T) { table := []struct { name string - annotation interface{} + annotation any extraOpts fx.Option useNew bool errContains string @@ -2450,7 +2445,6 @@ func TestHookAnnotationFailures(t *testing.T) { } for _, tt := range table { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() opts := fx.Options( @@ -2478,11 +2472,11 @@ func TestHookAnnotationFailures(t *testing.T) { } func TestHookAnnotationFunctionFlexibility(t *testing.T) { - type A interface{} + type A any table := []struct { name string - annotation interface{} + annotation any }{ { name: "without error return", @@ -2525,7 +2519,6 @@ func TestHookAnnotationFunctionFlexibility(t *testing.T) { } for _, tt := range table { - tt := tt t.Run(tt.name, func(t *testing.T) { var ( called atomic.Bool diff --git a/app.go b/app.go index c340e15b7..c81e03d78 100644 --- a/app.go +++ b/app.go @@ -201,7 +201,7 @@ func (o recoverFromPanicsOption) String() string { // // If Fx fails to build the logger, or no logger is specified, it will fall back to // [fxevent.ConsoleLogger] configured to write to stderr. -func WithLogger(constructor interface{}) Option { +func WithLogger(constructor any) Option { return withLoggerOption{ constructor: constructor, Stack: fxreflect.CallerStack(1, 0), @@ -209,7 +209,7 @@ func WithLogger(constructor interface{}) Option { } type withLoggerOption struct { - constructor interface{} + constructor any Stack fxreflect.Stack } @@ -230,7 +230,7 @@ func (l withLoggerOption) String() string { // Note, this will be deprecated in a future release. // Prefer to use [fxevent.Logger] instead. type Printer interface { - Printf(string, ...interface{}) + Printf(string, ...any) } // Logger redirects the application's log output to the provided printer. @@ -323,7 +323,7 @@ type App struct { // provide is a single constructor provided to Fx. type provide struct { // Constructor provided to Fx. This may be an fx.Annotated. - Target interface{} + Target any // Stack trace of where this provide was made. Stack fxreflect.Stack @@ -339,7 +339,7 @@ type provide struct { // invoke is a single invocation request to Fx. type invoke struct { // Function to invoke. - Target interface{} + Target any // Stack trace of where this invoke was made. Stack fxreflect.Stack diff --git a/app_test.go b/app_test.go index 5344389de..bf5e49bc8 100644 --- a/app_test.go +++ b/app_test.go @@ -978,7 +978,6 @@ func TestRunEventEmission(t *testing.T) { wantErr: `panic: "bad decorate"`, }, } { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -1335,7 +1334,7 @@ func TestAppRunTimeout(t *testing.T) { return func() error { // We'll exceed the start and stop timeouts, // and then some. - for i := 0; i < 3; i++ { + for range 3 { clock.Add(time.Second) } @@ -1389,7 +1388,6 @@ func TestAppRunTimeout(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -1906,8 +1904,7 @@ func TestAppStart(t *testing.T) { t.Run("StartTwiceWithHooksErrors", func(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() app := fxtest.New(t, Invoke(func(lc Lifecycle) { @@ -2721,7 +2718,6 @@ func TestOptionString(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -2738,7 +2734,7 @@ func TestMain(m *testing.M) { type testLogger struct{ t *testing.T } -func (l testLogger) Printf(s string, args ...interface{}) { +func (l testLogger) Printf(s string, args ...any) { l.t.Logf(s, args...) } diff --git a/decorate.go b/decorate.go index c07d2a7da..79c06f619 100644 --- a/decorate.go +++ b/decorate.go @@ -175,7 +175,7 @@ import ( // }), // ), // ) -func Decorate(decorators ...interface{}) Option { +func Decorate(decorators ...any) Option { return decorateOption{ Targets: decorators, Stack: fxreflect.CallerStack(1, 0), @@ -183,7 +183,7 @@ func Decorate(decorators ...interface{}) Option { } type decorateOption struct { - Targets []interface{} + Targets []any Stack fxreflect.Stack } @@ -207,7 +207,7 @@ func (o decorateOption) String() string { // decorator is a single decorator used in Fx. type decorator struct { // Decorator provided to Fx. - Target interface{} + Target any // Stack trace of where this provide was made. Stack fxreflect.Stack diff --git a/decorate_test.go b/decorate_test.go index 0518bf224..ee59a099b 100644 --- a/decorate_test.go +++ b/decorate_test.go @@ -315,7 +315,7 @@ func TestDecorateSuccess(t *testing.T) { }) t.Run("decoration must execute when required by a member of group", func(t *testing.T) { - type Drinks interface{} + type Drinks any type Coffee struct { Type string Name string diff --git a/extract.go b/extract.go index c09e6d3ce..713216c83 100644 --- a/extract.go +++ b/extract.go @@ -34,7 +34,7 @@ var _typeOfIn = reflect.TypeOf(In{}) // struct. Only exported fields will be filled. // // Deprecated: Use Populate instead. -func Extract(target interface{}) Option { +func Extract(target any) Option { v := reflect.ValueOf(target) if t := v.Type(); t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { diff --git a/extract_test.go b/extract_test.go index d034ab1df..9a085e022 100644 --- a/extract_test.go +++ b/extract_test.go @@ -43,7 +43,7 @@ func TestExtract(t *testing.T) { t.Run("Failures", func(t *testing.T) { t.Parallel() - tests := []interface{}{ + tests := []any{ 3, func() {}, struct{}{}, @@ -51,7 +51,6 @@ func TestExtract(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(fmt.Sprintf("%T", tt), func(t *testing.T) { t.Parallel() @@ -69,7 +68,7 @@ func TestExtract(t *testing.T) { t.Run("ValidateApp", func(t *testing.T) { t.Parallel() - tests := []interface{}{ + tests := []any{ 3, func() {}, struct{}{}, @@ -77,7 +76,6 @@ func TestExtract(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(fmt.Sprintf("%T", tt), func(t *testing.T) { t.Parallel() diff --git a/fxevent/console.go b/fxevent/console.go index d343d1fbe..4a2a800e8 100644 --- a/fxevent/console.go +++ b/fxevent/console.go @@ -36,7 +36,7 @@ type ConsoleLogger struct { var _ Logger = (*ConsoleLogger)(nil) -func (l *ConsoleLogger) logf(msg string, args ...interface{}) { +func (l *ConsoleLogger) logf(msg string, args ...any) { fmt.Fprintf(l.W, "[Fx] "+msg+"\n", args...) } diff --git a/fxevent/console_test.go b/fxevent/console_test.go index c41c97048..1d24b5ab2 100644 --- a/fxevent/console_test.go +++ b/fxevent/console_test.go @@ -402,7 +402,6 @@ func TestConsoleLogger(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() diff --git a/fxevent/slog_test.go b/fxevent/slog_test.go index ceaa5858c..1163692ba 100644 --- a/fxevent/slog_test.go +++ b/fxevent/slog_test.go @@ -40,7 +40,7 @@ type slogObservableEntry struct { record slog.Record } -func (s slogObservableEntry) unwrap(attr slog.Attr, out map[string]interface{}) { +func (s slogObservableEntry) unwrap(attr slog.Attr, out map[string]any) { anyAttr := attr.Value.Any() sliceAttr, ok := anyAttr.([]slog.Attr) @@ -57,8 +57,8 @@ func (s slogObservableEntry) unwrap(attr slog.Attr, out map[string]interface{}) out[attr.Key] = sliceAttrValues } -func (s slogObservableEntry) ContextMap() map[string]interface{} { - contextMap := map[string]interface{}{} +func (s slogObservableEntry) ContextMap() map[string]any { + contextMap := map[string]any{} s.record.Attrs(func(a slog.Attr) bool { s.unwrap(a, contextMap) @@ -112,7 +112,7 @@ func TestSlogLogger(t *testing.T) { name string give Event wantMessage string - wantFields map[string]interface{} + wantFields map[string]any }{ { name: "OnStartExecuting", @@ -121,7 +121,7 @@ func TestSlogLogger(t *testing.T) { CallerName: "bytes.NewBuffer", }, wantMessage: "OnStart hook executing", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart", }, @@ -133,7 +133,7 @@ func TestSlogLogger(t *testing.T) { CallerName: "bytes.NewBuffer", }, wantMessage: "OnStop hook executing", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStop1", }, @@ -146,7 +146,7 @@ func TestSlogLogger(t *testing.T) { Err: fmt.Errorf("some error"), }, wantMessage: "OnStop hook failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "error": "some error", @@ -160,7 +160,7 @@ func TestSlogLogger(t *testing.T) { Runtime: time.Millisecond * 3, }, wantMessage: "OnStop hook executed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "runtime": "3ms", @@ -174,7 +174,7 @@ func TestSlogLogger(t *testing.T) { Err: fmt.Errorf("some error"), }, wantMessage: "OnStart hook failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "error": "some error", @@ -188,7 +188,7 @@ func TestSlogLogger(t *testing.T) { Runtime: time.Millisecond * 3, }, wantMessage: "OnStart hook executed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "runtime": "3ms", @@ -202,10 +202,10 @@ func TestSlogLogger(t *testing.T) { ModuleTrace: []string{"main.main"}, }, wantMessage: "supplied", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "type": "*bytes.Buffer", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, }, }, { @@ -217,10 +217,10 @@ func TestSlogLogger(t *testing.T) { Err: someError, }, wantMessage: "error encountered while applying options", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "type": "*bytes.Buffer", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -235,10 +235,10 @@ func TestSlogLogger(t *testing.T) { Private: false, }, wantMessage: "provided", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "constructor": "bytes.NewBuffer()", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "type": "*bytes.Buffer", "module": "myModule", }, @@ -254,10 +254,10 @@ func TestSlogLogger(t *testing.T) { Private: true, }, wantMessage: "provided", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "constructor": "bytes.NewBuffer()", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "type": "*bytes.Buffer", "module": "myModule", "private": true, @@ -271,9 +271,9 @@ func TestSlogLogger(t *testing.T) { Err: someError, }, wantMessage: "error encountered while applying options", - wantFields: map[string]interface{}{ - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + wantFields: map[string]any{ + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -286,10 +286,10 @@ func TestSlogLogger(t *testing.T) { OutputTypeNames: []string{"*bytes.Buffer"}, }, wantMessage: "replaced", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "type": "*bytes.Buffer", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "module": "myModule", }, }, @@ -302,9 +302,9 @@ func TestSlogLogger(t *testing.T) { }, wantMessage: "error encountered while replacing", - wantFields: map[string]interface{}{ - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + wantFields: map[string]any{ + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -318,10 +318,10 @@ func TestSlogLogger(t *testing.T) { OutputTypeNames: []string{"*bytes.Buffer"}, }, wantMessage: "decorated", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "decorator": "bytes.NewBuffer()", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "type": "*bytes.Buffer", "module": "myModule", }, @@ -334,9 +334,9 @@ func TestSlogLogger(t *testing.T) { Err: someError, }, wantMessage: "error encountered while applying options", - wantFields: map[string]interface{}{ - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + wantFields: map[string]any{ + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -344,7 +344,7 @@ func TestSlogLogger(t *testing.T) { name: "BeforeRun", give: &BeforeRun{Name: "bytes.NewBuffer()", Kind: "constructor"}, wantMessage: "before run", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", }, @@ -353,7 +353,7 @@ func TestSlogLogger(t *testing.T) { name: "Run", give: &Run{Name: "bytes.NewBuffer()", Kind: "constructor", Runtime: 3 * time.Millisecond}, wantMessage: "run", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", "runtime": "3ms", @@ -368,7 +368,7 @@ func TestSlogLogger(t *testing.T) { Runtime: 3 * time.Millisecond, }, wantMessage: "run", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", "module": "myModule", @@ -383,7 +383,7 @@ func TestSlogLogger(t *testing.T) { Err: someError, }, wantMessage: "error returned", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", "error": "some error", @@ -393,7 +393,7 @@ func TestSlogLogger(t *testing.T) { name: "Invoking/Success", give: &Invoking{ModuleName: "myModule", FunctionName: "bytes.NewBuffer()"}, wantMessage: "invoking", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "function": "bytes.NewBuffer()", "module": "myModule", }, @@ -402,7 +402,7 @@ func TestSlogLogger(t *testing.T) { name: "Invoked/Error", give: &Invoked{FunctionName: "bytes.NewBuffer()", Err: someError}, wantMessage: "invoke failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", "stack": "", "function": "bytes.NewBuffer()", @@ -412,7 +412,7 @@ func TestSlogLogger(t *testing.T) { name: "Start/Error", give: &Started{Err: someError}, wantMessage: "start failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -420,7 +420,7 @@ func TestSlogLogger(t *testing.T) { name: "Stopping", give: &Stopping{Signal: os.Interrupt}, wantMessage: "received signal", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "signal": "INTERRUPT", }, }, @@ -428,7 +428,7 @@ func TestSlogLogger(t *testing.T) { name: "Stopped/Error", give: &Stopped{Err: someError}, wantMessage: "stop failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -436,7 +436,7 @@ func TestSlogLogger(t *testing.T) { name: "RollingBack/Error", give: &RollingBack{StartErr: someError}, wantMessage: "start failed, rolling back", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -444,7 +444,7 @@ func TestSlogLogger(t *testing.T) { name: "RolledBack/Error", give: &RolledBack{Err: someError}, wantMessage: "rollback failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -452,13 +452,13 @@ func TestSlogLogger(t *testing.T) { name: "Started", give: &Started{}, wantMessage: "started", - wantFields: map[string]interface{}{}, + wantFields: map[string]any{}, }, { name: "LoggerInitialized/Error", give: &LoggerInitialized{Err: someError}, wantMessage: "custom logger initialization failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -466,7 +466,7 @@ func TestSlogLogger(t *testing.T) { name: "LoggerInitialized", give: &LoggerInitialized{ConstructorName: "bytes.NewBuffer()"}, wantMessage: "initialized custom fxevent.Logger", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "function": "bytes.NewBuffer()", }, }, diff --git a/fxevent/zap_test.go b/fxevent/zap_test.go index 3ac3def70..67d7cfbd6 100644 --- a/fxevent/zap_test.go +++ b/fxevent/zap_test.go @@ -44,7 +44,7 @@ func TestZapLogger(t *testing.T) { name string give Event wantMessage string - wantFields map[string]interface{} + wantFields map[string]any }{ { name: "OnStartExecuting", @@ -53,7 +53,7 @@ func TestZapLogger(t *testing.T) { CallerName: "bytes.NewBuffer", }, wantMessage: "OnStart hook executing", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart", }, @@ -65,7 +65,7 @@ func TestZapLogger(t *testing.T) { CallerName: "bytes.NewBuffer", }, wantMessage: "OnStop hook executing", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStop1", }, @@ -78,7 +78,7 @@ func TestZapLogger(t *testing.T) { Err: fmt.Errorf("some error"), }, wantMessage: "OnStop hook failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "error": "some error", @@ -92,7 +92,7 @@ func TestZapLogger(t *testing.T) { Runtime: time.Millisecond * 3, }, wantMessage: "OnStop hook executed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "runtime": "3ms", @@ -106,7 +106,7 @@ func TestZapLogger(t *testing.T) { Err: fmt.Errorf("some error"), }, wantMessage: "OnStart hook failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "error": "some error", @@ -120,7 +120,7 @@ func TestZapLogger(t *testing.T) { Runtime: time.Millisecond * 3, }, wantMessage: "OnStart hook executed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "caller": "bytes.NewBuffer", "callee": "hook.onStart1", "runtime": "3ms", @@ -134,10 +134,10 @@ func TestZapLogger(t *testing.T) { ModuleTrace: []string{"main.main"}, }, wantMessage: "supplied", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "type": "*bytes.Buffer", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, }, }, { @@ -149,10 +149,10 @@ func TestZapLogger(t *testing.T) { Err: someError, }, wantMessage: "error encountered while applying options", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "type": "*bytes.Buffer", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -167,10 +167,10 @@ func TestZapLogger(t *testing.T) { Private: false, }, wantMessage: "provided", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "constructor": "bytes.NewBuffer()", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "type": "*bytes.Buffer", "module": "myModule", }, @@ -186,10 +186,10 @@ func TestZapLogger(t *testing.T) { Private: true, }, wantMessage: "provided", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "constructor": "bytes.NewBuffer()", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "type": "*bytes.Buffer", "module": "myModule", "private": true, @@ -203,9 +203,9 @@ func TestZapLogger(t *testing.T) { Err: someError, }, wantMessage: "error encountered while applying options", - wantFields: map[string]interface{}{ - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + wantFields: map[string]any{ + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -218,10 +218,10 @@ func TestZapLogger(t *testing.T) { OutputTypeNames: []string{"*bytes.Buffer"}, }, wantMessage: "replaced", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "type": "*bytes.Buffer", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "module": "myModule", }, }, @@ -234,9 +234,9 @@ func TestZapLogger(t *testing.T) { }, wantMessage: "error encountered while replacing", - wantFields: map[string]interface{}{ - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + wantFields: map[string]any{ + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -250,10 +250,10 @@ func TestZapLogger(t *testing.T) { OutputTypeNames: []string{"*bytes.Buffer"}, }, wantMessage: "decorated", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "decorator": "bytes.NewBuffer()", - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "type": "*bytes.Buffer", "module": "myModule", }, @@ -266,9 +266,9 @@ func TestZapLogger(t *testing.T) { Err: someError, }, wantMessage: "error encountered while applying options", - wantFields: map[string]interface{}{ - "stacktrace": []interface{}{"main.main", "runtime.main"}, - "moduletrace": []interface{}{"main.main"}, + wantFields: map[string]any{ + "stacktrace": []any{"main.main", "runtime.main"}, + "moduletrace": []any{"main.main"}, "error": "some error", }, }, @@ -276,7 +276,7 @@ func TestZapLogger(t *testing.T) { name: "Run", give: &Run{Name: "bytes.NewBuffer()", Kind: "constructor", Runtime: time.Second}, wantMessage: "run", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", "runtime": "1s", @@ -291,7 +291,7 @@ func TestZapLogger(t *testing.T) { Runtime: time.Millisecond, }, wantMessage: "run", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", "module": "myModule", @@ -306,7 +306,7 @@ func TestZapLogger(t *testing.T) { Err: someError, }, wantMessage: "error returned", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "name": "bytes.NewBuffer()", "kind": "constructor", "error": "some error", @@ -316,7 +316,7 @@ func TestZapLogger(t *testing.T) { name: "Invoking/Success", give: &Invoking{ModuleName: "myModule", FunctionName: "bytes.NewBuffer()"}, wantMessage: "invoking", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "function": "bytes.NewBuffer()", "module": "myModule", }, @@ -325,7 +325,7 @@ func TestZapLogger(t *testing.T) { name: "Invoked/Error", give: &Invoked{FunctionName: "bytes.NewBuffer()", Err: someError}, wantMessage: "invoke failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", "stack": "", "function": "bytes.NewBuffer()", @@ -335,7 +335,7 @@ func TestZapLogger(t *testing.T) { name: "Start/Error", give: &Started{Err: someError}, wantMessage: "start failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -343,7 +343,7 @@ func TestZapLogger(t *testing.T) { name: "Stopping", give: &Stopping{Signal: os.Interrupt}, wantMessage: "received signal", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "signal": "INTERRUPT", }, }, @@ -351,7 +351,7 @@ func TestZapLogger(t *testing.T) { name: "Stopped/Error", give: &Stopped{Err: someError}, wantMessage: "stop failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -359,7 +359,7 @@ func TestZapLogger(t *testing.T) { name: "RollingBack/Error", give: &RollingBack{StartErr: someError}, wantMessage: "start failed, rolling back", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -367,7 +367,7 @@ func TestZapLogger(t *testing.T) { name: "RolledBack/Error", give: &RolledBack{Err: someError}, wantMessage: "rollback failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -375,13 +375,13 @@ func TestZapLogger(t *testing.T) { name: "Started", give: &Started{}, wantMessage: "started", - wantFields: map[string]interface{}{}, + wantFields: map[string]any{}, }, { name: "LoggerInitialized/Error", give: &LoggerInitialized{Err: someError}, wantMessage: "custom logger initialization failed", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "error": "some error", }, }, @@ -389,7 +389,7 @@ func TestZapLogger(t *testing.T) { name: "LoggerInitialized", give: &LoggerInitialized{ConstructorName: "bytes.NewBuffer()"}, wantMessage: "initialized custom fxevent.Logger", - wantFields: map[string]interface{}{ + wantFields: map[string]any{ "function": "bytes.NewBuffer()", }, }, @@ -397,7 +397,6 @@ func TestZapLogger(t *testing.T) { t.Run("debug observer, log at default (info)", func(t *testing.T) { for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() @@ -416,7 +415,6 @@ func TestZapLogger(t *testing.T) { t.Run("info observer, log at debug", func(t *testing.T) { for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() @@ -441,7 +439,6 @@ func TestZapLogger(t *testing.T) { t.Run("info observer, log/error at debug", func(t *testing.T) { for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() diff --git a/fxtest/lifecycle.go b/fxtest/lifecycle.go index 37dc08775..f0e1f8ddb 100644 --- a/fxtest/lifecycle.go +++ b/fxtest/lifecycle.go @@ -45,15 +45,15 @@ type panicT struct { var _ TB = &panicT{} -func (t *panicT) format(s string, args ...interface{}) string { +func (t *panicT) format(s string, args ...any) string { return fmt.Sprintf(s, args...) } -func (t *panicT) Logf(s string, args ...interface{}) { +func (t *panicT) Logf(s string, args ...any) { fmt.Fprintln(t.W, t.format(s, args...)) } -func (t *panicT) Errorf(s string, args ...interface{}) { +func (t *panicT) Errorf(s string, args ...any) { t.lastErr = t.format(s, args...) fmt.Fprintln(t.W, t.lastErr) } diff --git a/fxtest/lifecycle_test.go b/fxtest/lifecycle_test.go index 97df4960b..7ae60a257 100644 --- a/fxtest/lifecycle_test.go +++ b/fxtest/lifecycle_test.go @@ -254,7 +254,7 @@ func TestLifecycle_OptionalT(t *testing.T) { }, }) - var pval interface{} + var pval any func() { defer func() { pval = recover() }() lc.RequireStart() @@ -291,7 +291,7 @@ func TestPanicT(t *testing.T) { t.Run("FailNow", func(t *testing.T) { t.Parallel() - var pval interface{} + var pval any func() { defer func() { pval = recover() }() pt.FailNow() @@ -308,7 +308,7 @@ func TestPanicT(t *testing.T) { pt := panicT{W: &buff} pt.Logf("hello: %v", "world") - var pval interface{} + var pval any func() { defer func() { pval = recover() }() pt.FailNow() diff --git a/fxtest/printer.go b/fxtest/printer.go index c16c155b6..e890394df 100644 --- a/fxtest/printer.go +++ b/fxtest/printer.go @@ -49,6 +49,6 @@ func NewTestPrinter(t TB) fx.Printer { return &testPrinter{t} } -func (p *testPrinter) Printf(format string, args ...interface{}) { +func (p *testPrinter) Printf(format string, args ...any) { p.Logf(format, args...) } diff --git a/fxtest/tb.go b/fxtest/tb.go index afd198c18..a2820f140 100644 --- a/fxtest/tb.go +++ b/fxtest/tb.go @@ -23,7 +23,7 @@ package fxtest // TB is a subset of the standard library's testing.TB interface. It's // satisfied by both *testing.T and *testing.B. type TB interface { - Logf(string, ...interface{}) - Errorf(string, ...interface{}) + Logf(string, ...any) + Errorf(string, ...any) FailNow() } diff --git a/fxtest/tb_test.go b/fxtest/tb_test.go index 07718f768..a0e2a908d 100644 --- a/fxtest/tb_test.go +++ b/fxtest/tb_test.go @@ -43,12 +43,12 @@ func (t *tb) FailNow() { t.failures++ } -func (t *tb) Errorf(format string, args ...interface{}) { +func (t *tb) Errorf(format string, args ...any) { fmt.Fprintf(t.errors, format, args...) t.errors.WriteRune('\n') } -func (t *tb) Logf(format string, args ...interface{}) { +func (t *tb) Logf(format string, args ...any) { fmt.Fprintf(t.logs, format, args...) t.logs.WriteRune('\n') } diff --git a/internal/fxclock/clock_test.go b/internal/fxclock/clock_test.go index 22a538a2e..52e433c62 100644 --- a/internal/fxclock/clock_test.go +++ b/internal/fxclock/clock_test.go @@ -171,7 +171,7 @@ func TestMock_ManySleepers(t *testing.T) { var wg sync.WaitGroup wg.Add(N) - for i := 0; i < N; i++ { + for range N { go func() { defer wg.Done() diff --git a/internal/fxreflect/fxreflect.go b/internal/fxreflect/fxreflect.go index c4ab1c675..e706aa9c0 100644 --- a/internal/fxreflect/fxreflect.go +++ b/internal/fxreflect/fxreflect.go @@ -52,7 +52,7 @@ func Caller() string { } // FuncName returns a funcs formatted name -func FuncName(fn interface{}) string { +func FuncName(fn any) string { fnV := reflect.ValueOf(fn) if fnV.Kind() != reflect.Func { return fmt.Sprint(fn) diff --git a/internal/fxreflect/fxreflect_test.go b/internal/fxreflect/fxreflect_test.go index 73817bed6..0ffb14d8d 100644 --- a/internal/fxreflect/fxreflect_test.go +++ b/internal/fxreflect/fxreflect_test.go @@ -40,7 +40,7 @@ func TestFuncName(t *testing.T) { tests := []struct { desc string - give interface{} + give any want string }{ { @@ -56,7 +56,6 @@ func TestFuncName(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -90,7 +89,6 @@ func TestSanitizeFuncNames(t *testing.T) { }, } for _, c := range cases { - c := c t.Run(c.name, func(t *testing.T) { t.Parallel() diff --git a/internal/fxreflect/stack_test.go b/internal/fxreflect/stack_test.go index b3ce56147..aebdc0ad8 100644 --- a/internal/fxreflect/stack_test.go +++ b/internal/fxreflect/stack_test.go @@ -158,7 +158,6 @@ func TestStackCallerName(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() @@ -213,7 +212,6 @@ func TestFrameString(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() diff --git a/internal/testutil/writer.go b/internal/testutil/writer.go index 695cd6761..269fc883b 100644 --- a/internal/testutil/writer.go +++ b/internal/testutil/writer.go @@ -26,7 +26,7 @@ import ( // TestingT is a subset of the testing.T interface. type TestingT interface { - Logf(string, ...interface{}) + Logf(string, ...any) } // WriteSyncer is a zapcore.WriteSyncer that writes to the provided test diff --git a/internal/testutil/writer_test.go b/internal/testutil/writer_test.go index 91be4320e..6a9f8324d 100644 --- a/internal/testutil/writer_test.go +++ b/internal/testutil/writer_test.go @@ -33,7 +33,7 @@ type spy struct{ Logs []string } func (s *spy) Clear() { s.Logs = nil } -func (s *spy) Logf(msg string, args ...interface{}) { +func (s *spy) Logf(msg string, args ...any) { s.Logs = append(s.Logs, fmt.Sprintf(msg, args...)) } diff --git a/invoke.go b/invoke.go index f60b7c9eb..c75933b02 100644 --- a/invoke.go +++ b/invoke.go @@ -61,7 +61,7 @@ import ( // To see an invocation in use, read through the package-level example. For // advanced features, including optional parameters and named instances, see // the documentation of the In and Out types. -func Invoke(funcs ...interface{}) Option { +func Invoke(funcs ...any) Option { return invokeOption{ Targets: funcs, Stack: fxreflect.CallerStack(1, 0), @@ -69,7 +69,7 @@ func Invoke(funcs ...interface{}) Option { } type invokeOption struct { - Targets []interface{} + Targets []any Stack fxreflect.Stack } diff --git a/module.go b/module.go index a71fb65a7..d3f7d0679 100644 --- a/module.go +++ b/module.go @@ -35,9 +35,9 @@ import ( // // This definition corresponds to the dig.Container and dig.Scope. type container interface { - Invoke(interface{}, ...dig.InvokeOption) error - Provide(interface{}, ...dig.ProvideOption) error - Decorate(interface{}, ...dig.DecorateOption) error + Invoke(any, ...dig.InvokeOption) error + Provide(any, ...dig.ProvideOption) error + Decorate(any, ...dig.DecorateOption) error } // Module is a named group of zero or more fx.Options. @@ -136,9 +136,9 @@ type module struct { // We can consider moving this into Fx using type constraints after Go 1.20 // is released and 1.17 is deprecated. type scope interface { - Decorate(f interface{}, opts ...dig.DecorateOption) error - Invoke(f interface{}, opts ...dig.InvokeOption) error - Provide(f interface{}, opts ...dig.ProvideOption) error + Decorate(f any, opts ...dig.DecorateOption) error + Invoke(f any, opts ...dig.InvokeOption) error + Provide(f any, opts ...dig.ProvideOption) error Scope(name string, opts ...dig.ScopeOption) *dig.Scope String() string } diff --git a/module_test.go b/module_test.go index 3fa5fdc93..39dc5ab8b 100644 --- a/module_test.go +++ b/module_test.go @@ -276,7 +276,6 @@ func TestModuleSuccess(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() var spy fxlog.Spy @@ -599,7 +598,6 @@ func TestModuleFailures(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.desc, func(t *testing.T) { t.Parallel() diff --git a/populate.go b/populate.go index 09148b220..2ace21488 100644 --- a/populate.go +++ b/populate.go @@ -61,7 +61,7 @@ import ( // This is most helpful in unit tests: it lets tests leverage Fx's automatic // constructor wiring to build a few structs, but then extract those structs // for further testing. -func Populate(targets ...interface{}) Option { +func Populate(targets ...any) Option { // Validate all targets are non-nil pointers. fields := make([]reflect.StructField, len(targets)+1) fields[0] = reflect.StructField{ diff --git a/populate_test.go b/populate_test.go index 14e88b76d..a3ed8a103 100644 --- a/populate_test.go +++ b/populate_test.go @@ -346,48 +346,47 @@ func TestPopulateValidateApp(t *testing.T) { tests := []struct { msg string - opts []interface{} + opts []any wantErr string }{ { msg: "inline value", - opts: []interface{}{t1{}}, + opts: []any{t1{}}, wantErr: "not a pointer", }, { msg: "container value", - opts: []interface{}{container{}}, + opts: []any{container{}}, wantErr: "not a pointer", }, { msg: "container pointer without fx.In", - opts: []interface{}{&containerNoIn{}}, + opts: []any{&containerNoIn{}}, wantErr: "missing type: fx_test.containerNoIn", }, { msg: "function", - opts: []interface{}{fn}, + opts: []any{fn}, wantErr: "not a pointer", }, { msg: "function pointer", - opts: []interface{}{&fn}, + opts: []any{&fn}, wantErr: "missing type: func()", }, { msg: "invalid last argument", - opts: []interface{}{&v, t1{}}, + opts: []any{&v, t1{}}, wantErr: "target 2 is not a pointer type", }, { msg: "nil argument", - opts: []interface{}{&v, nil, &v}, + opts: []any{&v, nil, &v}, wantErr: "target 2 is nil", }, } for _, tt := range tests { - tt := tt t.Run(tt.msg, func(t *testing.T) { t.Parallel() diff --git a/provide.go b/provide.go index 72db3f83b..f55348966 100644 --- a/provide.go +++ b/provide.go @@ -61,7 +61,7 @@ import ( // possible, and should avoid spawning goroutines. Things like server listen // loops, background timer loops, and background processing goroutines should // instead be managed using Lifecycle callbacks. -func Provide(constructors ...interface{}) Option { +func Provide(constructors ...any) Option { return provideOption{ Targets: constructors, Stack: fxreflect.CallerStack(1, 0), @@ -69,14 +69,14 @@ func Provide(constructors ...interface{}) Option { } type provideOption struct { - Targets []interface{} + Targets []any Stack fxreflect.Stack } func (o provideOption) apply(mod *module) { var private bool - targets := make([]interface{}, 0, len(o.Targets)) + targets := make([]any, 0, len(o.Targets)) for _, target := range o.Targets { if _, ok := target.(privateOption); ok { private = true diff --git a/replace.go b/replace.go index b457797db..9e9064d03 100644 --- a/replace.go +++ b/replace.go @@ -74,8 +74,8 @@ import ( // fx.Replace( // fx.Annotate(os.Stderr, fx.As(new(io.Writer))) // ) -func Replace(values ...interface{}) Option { - decorators := make([]interface{}, len(values)) // one function per value +func Replace(values ...any) Option { + decorators := make([]any, len(values)) // one function per value types := make([]reflect.Type, len(values)) for i, value := range values { switch value := value.(type) { @@ -97,7 +97,7 @@ func Replace(values ...interface{}) Option { } type replaceOption struct { - Targets []interface{} + Targets []any Types []reflect.Type // type of value produced by constructor[i] Stack fxreflect.Stack } @@ -122,7 +122,7 @@ func (o replaceOption) String() string { } // Returns a function that takes no parameters, and returns the given value. -func newReplaceDecorator(value interface{}) (interface{}, reflect.Type) { +func newReplaceDecorator(value any) (any, reflect.Type) { switch value.(type) { case nil: panic("untyped nil passed to fx.Replace") diff --git a/shutdown_test.go b/shutdown_test.go index c4971ffc2..13edc23b4 100644 --- a/shutdown_test.go +++ b/shutdown_test.go @@ -119,7 +119,7 @@ func TestShutdown(t *testing.T) { t.Cleanup(func() { app.Stop(context.Background()) }) // in t.Cleanup so this happens after all subtests return (not just this function) defer require.NoError(t, app.Stop(context.Background())) - for i := 0; i < 10; i++ { + for i := range 10 { t.Run(fmt.Sprintf("Wait %v", i), func(t *testing.T) { t.Parallel() wait := <-app.Wait() @@ -162,7 +162,7 @@ func TestShutdown(t *testing.T) { fx.Populate(&shutdowner), ) - for i := 0; i < 10; i++ { + for i := range 10 { app.RequireStart() shutdowner.Shutdown(fx.ExitCode(i)) assert.Equal(t, i, (<-app.Wait()).ExitCode, "run %d", i) @@ -188,7 +188,7 @@ func TestDataRace(t *testing.T) { // Spawn N goroutines, each of which call app.Done() and assert // the signal received. wg.Add(N) - for i := 0; i < N; i++ { + for i := range N { i := i go func() { defer wg.Done() diff --git a/signal_test.go b/signal_test.go index cd85b54e0..517582658 100644 --- a/signal_test.go +++ b/signal_test.go @@ -83,8 +83,7 @@ func TestSignal(t *testing.T) { }) t.Run("no error", func(t *testing.T) { recv := newSignalReceivers() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() recv.Start() recv.Start() // should be a no-op if already running require.NoError(t, recv.Stop(ctx)) @@ -103,8 +102,7 @@ func TestSignal(t *testing.T) { recv.stopNotify = func(ch chan<- os.Signal) { stopCalledTimes++ } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() recv.Start() stub <- syscall.SIGTERM stub <- syscall.SIGTERM diff --git a/supply.go b/supply.go index 23d1a6ef0..71c231089 100644 --- a/supply.go +++ b/supply.go @@ -79,8 +79,8 @@ import ( // fx.Supply( // fx.Annotate(handler, fx.As(new(http.Handler))), // ) -func Supply(values ...interface{}) Option { - constructors := make([]interface{}, 0, len(values)) +func Supply(values ...any) Option { + constructors := make([]any, 0, len(values)) types := make([]reflect.Type, 0, len(values)) var private bool for _, value := range values { @@ -114,7 +114,7 @@ func Supply(values ...interface{}) Option { } type supplyOption struct { - Targets []interface{} + Targets []any Types []reflect.Type // type of value produced by constructor[i] Stack fxreflect.Stack Private bool @@ -141,7 +141,7 @@ func (o supplyOption) String() string { } // Returns a function that takes no parameters, and returns the given value. -func newSupplyConstructor(value interface{}) (interface{}, reflect.Type) { +func newSupplyConstructor(value any) (any, reflect.Type) { switch value.(type) { case nil: panic("untyped nil passed to fx.Supply")