diff --git a/constructor.go b/constructor.go index d46a273d..51b2aa2c 100644 --- a/constructor.go +++ b/constructor.go @@ -36,7 +36,7 @@ import ( // For the Provide path, we verify that constructorNodes produce at least one value, // otherwise the function will never be called. type constructorNode struct { - ctor interface{} + ctor any ctype reflect.Type // Location where this function was defined. @@ -76,13 +76,13 @@ type constructorOptions struct { // belong to the specified value group or implement any of the interfaces. ResultName string ResultGroup string - ResultAs []interface{} + ResultAs []any Location *digreflect.Func Callback Callback BeforeCallback BeforeCallback } -func newConstructorNode(ctor interface{}, s *Scope, origS *Scope, opts constructorOptions) (*constructorNode, error) { +func newConstructorNode(ctor any, s *Scope, origS *Scope, opts constructorOptions) (*constructorNode, error) { cval := reflect.ValueOf(ctor) ctype := cval.Type() cptr := cval.Pointer() diff --git a/container.go b/container.go index a875b5e0..5917b902 100644 --- a/container.go +++ b/container.go @@ -95,7 +95,7 @@ type containerStore interface { containerWriter // Adds a new graph node to the Container - newGraphNode(w interface{}, orders map[*Scope]int) + newGraphNode(w any, orders map[*Scope]int) // Returns a slice containing all known types. knownTypes() []reflect.Type diff --git a/decorate.go b/decorate.go index 5cc500d7..72c30643 100644 --- a/decorate.go +++ b/decorate.go @@ -43,7 +43,7 @@ type decorator interface { } type decoratorNode struct { - dcor interface{} + dcor any dtype reflect.Type id dot.CtorID @@ -73,7 +73,7 @@ type decoratorNode struct { beforeCallback BeforeCallback } -func newDecoratorNode(dcor interface{}, s *Scope, opts decorateOptions) (*decoratorNode, error) { +func newDecoratorNode(dcor any, s *Scope, opts decorateOptions) (*decoratorNode, error) { dval := reflect.ValueOf(dcor) dtype := dval.Type() dptr := dval.Pointer() @@ -203,7 +203,7 @@ type DecorateInfo struct { // Decorate provides a decorator for a type that has already been provided in the Container. // Decorations at this level affect all scopes of the container. // See Scope.Decorate for information on how to use this method. -func (c *Container) Decorate(decorator interface{}, opts ...DecorateOption) error { +func (c *Container) Decorate(decorator any, opts ...DecorateOption) error { return c.scope.Decorate(decorator, opts...) } @@ -244,7 +244,7 @@ func (c *Container) Decorate(decorator interface{}, opts ...DecorateOption) erro // Decorating a Scope affects all the child scopes of this Scope. // // Similar to a provider, the decorator function gets called *at most once*. -func (s *Scope) Decorate(decorator interface{}, opts ...DecorateOption) error { +func (s *Scope) Decorate(decorator any, opts ...DecorateOption) error { var options decorateOptions for _, opt := range opts { opt.apply(&options) diff --git a/dig_test.go b/dig_test.go index 647c1494..67d2351b 100644 --- a/dig_test.go +++ b/dig_test.go @@ -1618,7 +1618,7 @@ func TestRecoverFromPanic(t *testing.T) { tests := []struct { name string setup func(*digtest.Container) - invoke interface{} + invoke any wantErr []string }{ { @@ -1885,7 +1885,7 @@ func TestProvideConstructorErrors(t *testing.T) { tests := []struct { desc string - constructor interface{} + constructor any msg string }{ { @@ -2005,7 +2005,7 @@ func TestCantProvideObjects(t *testing.T) { var writer io.Writer = &bytes.Buffer{} tests := []struct { - object interface{} + object any typeDesc string }{ {&bytes.Buffer{}, "pointer"}, @@ -2115,7 +2115,7 @@ func TestProvideInvalidAs(t *testing.T) { tests := []struct { name string - param interface{} + param any expectedErr string addlOption dig.ProvideOption }{ @@ -2261,7 +2261,7 @@ func TestCantProvideUntypedNil(t *testing.T) { func TestCantProvideErrorLikeType(t *testing.T) { t.Parallel() - tests := []interface{}{ + tests := []any{ func() *os.PathError { return &os.PathError{} }, func() error { return &os.PathError{} }, func() (*os.PathError, error) { return &os.PathError{}, nil }, @@ -2314,7 +2314,7 @@ func TestCantProvideParameterObjects(t *testing.T) { func TestProvideKnownTypesFails(t *testing.T) { t.Parallel() - provideArgs := []interface{}{ + provideArgs := []any{ func() *bytes.Buffer { return nil }, func() (*bytes.Buffer, error) { return nil, nil }, } @@ -2648,7 +2648,7 @@ func TestTypeCheckingEquality(t *testing.T) { B } tests := []struct { - item interface{} + item any isIn bool isOut bool }{ @@ -2743,7 +2743,7 @@ func testProvideFailures(t *testing.T, dryRun bool) { A2: A{idx: 2}, A3: A{idx: 3}, } - }, dig.As(new(interface{}))) + }, dig.As(new(any))) require.Error(t, err, "provide must return error") dig.AssertErrorMatches(t, err, `cannot provide function "go.uber.org/dig_test".testProvideFailures\S+`, @@ -3370,8 +3370,8 @@ func testInvokeFailures(t *testing.T, dryRun bool) { cases := []struct { name string - provide interface{} - invoke interface{} + provide any + invoke any errContains []string }{ { @@ -3455,8 +3455,8 @@ func testInvokeFailures(t *testing.T, dryRun bool) { cases := []struct { name string - provide interface{} - invoke interface{} + provide any + invoke any errContains []string }{ { @@ -3540,8 +3540,8 @@ func testInvokeFailures(t *testing.T, dryRun bool) { cases := []struct { name string - provide interface{} - invoke interface{} + provide any + invoke any errContains []string }{ { diff --git a/error_test.go b/error_test.go index 1656edef..6b0c2726 100644 --- a/error_test.go +++ b/error_test.go @@ -220,7 +220,7 @@ func TestRootCauseEndToEnd(t *testing.T) { tests := []struct { desc string setup func(c *Container) - invoke interface{} + invoke any wantAsDigError bool wantRootCauseMessage string wantRootCauseAsDigError bool diff --git a/graph.go b/graph.go index e08f1f54..20046607 100644 --- a/graph.go +++ b/graph.go @@ -24,7 +24,7 @@ import "go.uber.org/dig/internal/graph" // graphNode is a single node in the dependency graph. type graphNode struct { - Wrapped interface{} + Wrapped any } // graphHolder is the dependency graph of the container. @@ -78,7 +78,7 @@ func (gh *graphHolder) EdgesFrom(u int) []int { } // NewNode adds a new value to the graph and returns its order. -func (gh *graphHolder) NewNode(wrapped interface{}) int { +func (gh *graphHolder) NewNode(wrapped any) int { order := len(gh.nodes) gh.nodes = append(gh.nodes, &graphNode{ Wrapped: wrapped, @@ -88,7 +88,7 @@ func (gh *graphHolder) NewNode(wrapped interface{}) int { // Lookup retrieves the value for the node with the given order. // Lookup panics if i is invalid. -func (gh *graphHolder) Lookup(i int) interface{} { +func (gh *graphHolder) Lookup(i int) any { return gh.nodes[i].Wrapped } diff --git a/inout.go b/inout.go index 3d575842..9a0a740b 100644 --- a/inout.go +++ b/inout.go @@ -94,7 +94,7 @@ func isError(t reflect.Type) bool { // // See the documentation for dig.In for a comprehensive list of supported // tags. -func IsIn(o interface{}) bool { +func IsIn(o any) bool { return embedsType(o, _inType) } @@ -108,12 +108,12 @@ func IsIn(o interface{}) bool { // // See the documentation for dig.Out for a comprehensive list of supported // tags. -func IsOut(o interface{}) bool { +func IsOut(o any) bool { return embedsType(o, _outType) } // Returns true if t embeds e or if any of the types embedded by t embed e. -func embedsType(i interface{}, e reflect.Type) bool { +func embedsType(i any, e reflect.Type) bool { // TODO: this function doesn't consider e being a pointer. // given `type A foo { *In }`, this function would return false for // embedding dig.In, which makes for some extra error checking in places diff --git a/internal/digerror/errors.go b/internal/digerror/errors.go index 13dde3dc..aac186e8 100644 --- a/internal/digerror/errors.go +++ b/internal/digerror/errors.go @@ -26,7 +26,7 @@ import ( // BugPanicf panics with the provided message directing users to GitHub issues // creation page. -func BugPanicf(msg string, args ...interface{}) { +func BugPanicf(msg string, args ...any) { panic(fmt.Sprintf("It looks like you have found a bug in dig. "+ "Please file an issue at https://github.com/uber-go/dig/issues/new "+ "and provide the following message: "+ diff --git a/internal/digreflect/func.go b/internal/digreflect/func.go index 8554ed8d..1629c92e 100644 --- a/internal/digreflect/func.go +++ b/internal/digreflect/func.go @@ -64,7 +64,7 @@ func (f *Func) Format(w fmt.State, c rune) { // InspectFunc inspects and returns runtime information about the given // function. -func InspectFunc(function interface{}) *Func { +func InspectFunc(function any) *Func { fptr := reflect.ValueOf(function).Pointer() return InspectFuncPC(fptr) } diff --git a/internal/digreflect/func_test.go b/internal/digreflect/func_test.go index ffa3c59c..24053662 100644 --- a/internal/digreflect/func_test.go +++ b/internal/digreflect/func_test.go @@ -49,7 +49,7 @@ func TestInspectFunc(t *testing.T) { tests := []struct { desc string - give interface{} + give any wantName string wantPackage string diff --git a/internal/digtest/container.go b/internal/digtest/container.go index d73bbb02..d7151a06 100644 --- a/internal/digtest/container.go +++ b/internal/digtest/container.go @@ -65,7 +65,7 @@ func New(t testing.TB, opts ...dig.Option) *Container { // RequireProvide provides the given function to the container, // halting the test if it fails. -func (c *Container) RequireProvide(f interface{}, opts ...dig.ProvideOption) { +func (c *Container) RequireProvide(f any, opts ...dig.ProvideOption) { c.t.Helper() require.NoError(c.t, c.Provide(f, opts...), "failed to provide") @@ -73,7 +73,7 @@ func (c *Container) RequireProvide(f interface{}, opts ...dig.ProvideOption) { // RequireProvide provides the given function to the scope, // halting the test if it fails. -func (s *Scope) RequireProvide(f interface{}, opts ...dig.ProvideOption) { +func (s *Scope) RequireProvide(f any, opts ...dig.ProvideOption) { s.t.Helper() require.NoError(s.t, s.Provide(f, opts...), "failed to provide") @@ -81,7 +81,7 @@ func (s *Scope) RequireProvide(f interface{}, opts ...dig.ProvideOption) { // RequireInvoke invokes the given function to the container, // halting the test if it fails. -func (c *Container) RequireInvoke(f interface{}, opts ...dig.InvokeOption) { +func (c *Container) RequireInvoke(f any, opts ...dig.InvokeOption) { c.t.Helper() require.NoError(c.t, c.Invoke(f, opts...), "failed to invoke") @@ -89,7 +89,7 @@ func (c *Container) RequireInvoke(f interface{}, opts ...dig.InvokeOption) { // RequireInvoke invokes the given function to the scope, // halting the test if it fails. -func (s *Scope) RequireInvoke(f interface{}, opts ...dig.InvokeOption) { +func (s *Scope) RequireInvoke(f any, opts ...dig.InvokeOption) { s.t.Helper() require.NoError(s.t, s.Invoke(f, opts...), "failed to invoke") @@ -97,7 +97,7 @@ func (s *Scope) RequireInvoke(f interface{}, opts ...dig.InvokeOption) { // RequireDecorate decorates the scope using the given function, // halting the test if it fails. -func (c *Container) RequireDecorate(f interface{}, opts ...dig.DecorateOption) { +func (c *Container) RequireDecorate(f any, opts ...dig.DecorateOption) { c.t.Helper() require.NoError(c.t, c.Decorate(f, opts...), "failed to decorate") @@ -105,7 +105,7 @@ func (c *Container) RequireDecorate(f interface{}, opts ...dig.DecorateOption) { // RequireDecorate decorates the scope using the given function, // halting the test if it fails. -func (s *Scope) RequireDecorate(f interface{}, opts ...dig.DecorateOption) { +func (s *Scope) RequireDecorate(f any, opts ...dig.DecorateOption) { s.t.Helper() require.NoError(s.t, s.Decorate(f, opts...), "failed to decorate") diff --git a/invoke.go b/invoke.go index 766415b3..04a03ad0 100644 --- a/invoke.go +++ b/invoke.go @@ -79,7 +79,7 @@ func (o fillInvokeInfoOption) applyInvokeOption(opts *invokeOptions) { // If the [RecoverFromPanics] option was given to the container and a panic // occurs when invoking, a [PanicError] with the panic contained will be // returned. See [PanicError] for more info. -func (c *Container) Invoke(function interface{}, opts ...InvokeOption) error { +func (c *Container) Invoke(function any, opts ...InvokeOption) error { return c.scope.Invoke(function, opts...) } @@ -91,7 +91,7 @@ func (c *Container) Invoke(function interface{}, opts ...InvokeOption) error { // // The function may return an error to indicate failure. The error will be // returned to the caller as-is. -func (s *Scope) Invoke(function interface{}, opts ...InvokeOption) (err error) { +func (s *Scope) Invoke(function any, opts ...InvokeOption) (err error) { ftype := reflect.TypeOf(function) if ftype == nil { return newErrInvalidInput("can't invoke an untyped nil", nil) diff --git a/param_test.go b/param_test.go index 507529aa..6ec78a66 100644 --- a/param_test.go +++ b/param_test.go @@ -180,7 +180,7 @@ func TestParamObjectFailure(t *testing.T) { func TestParamGroupSliceErrors(t *testing.T) { tests := []struct { desc string - shape interface{} + shape any wantErr string }{ { diff --git a/provide.go b/provide.go index 9e47b6db..0afdfcc2 100644 --- a/provide.go +++ b/provide.go @@ -40,7 +40,7 @@ type provideOptions struct { Name string Group string Info *ProvideInfo - As []interface{} + As []any Location *digreflect.Func Exported bool Callback Callback @@ -259,11 +259,11 @@ func (o fillProvideInfoOption) applyProvideOption(opts *provideOptions) { // // This option cannot be provided for constructors which produce result // objects. -func As(i ...interface{}) ProvideOption { +func As(i ...any) ProvideOption { return provideAsOption(i) } -type provideAsOption []interface{} +type provideAsOption []any func (o provideAsOption) String() string { buf := bytes.NewBufferString("As(") @@ -380,7 +380,7 @@ type provider interface { // // Provide accepts argument types or dig.In structs as dependencies, and // separate return values or dig.Out structs for results. -func (c *Container) Provide(constructor interface{}, opts ...ProvideOption) error { +func (c *Container) Provide(constructor any, opts ...ProvideOption) error { return c.scope.Provide(constructor, opts...) } @@ -403,7 +403,7 @@ func (c *Container) Provide(constructor interface{}, opts ...ProvideOption) erro // Scopes that are descendents, but not ancestors of this Scope. // To provide a constructor to all the Scopes available, provide it to // Container, which is the root Scope. -func (s *Scope) Provide(constructor interface{}, opts ...ProvideOption) error { +func (s *Scope) Provide(constructor any, opts ...ProvideOption) error { ctype := reflect.TypeOf(constructor) if ctype == nil { return newErrInvalidInput("can't provide an untyped nil", nil) @@ -437,7 +437,7 @@ func (s *Scope) Provide(constructor interface{}, opts ...ProvideOption) error { return nil } -func (s *Scope) provide(ctor interface{}, opts provideOptions) (err error) { +func (s *Scope) provide(ctor any, opts provideOptions) (err error) { // If Export option is provided to the constructor, this should be injected to the // root-level Scope (Container) to allow it to propagate to all other Scopes. origScope := s diff --git a/result.go b/result.go index 369cd218..f59bf89d 100644 --- a/result.go +++ b/result.go @@ -62,7 +62,7 @@ type resultOptions struct { // For Result Objects, name:".." tags on fields override this. Name string Group string - As []interface{} + As []any } // newResult builds a result from the given type. diff --git a/result_test.go b/result_test.go index c19db20d..eb5b38a8 100644 --- a/result_test.go +++ b/result_test.go @@ -33,7 +33,7 @@ import ( func TestNewResultListErrors(t *testing.T) { tests := []struct { desc string - give interface{} + give any wantError string }{ { @@ -84,7 +84,7 @@ func TestNewResultErrors(t *testing.T) { } tests := []struct { - give interface{} + give any err string }{ { @@ -121,7 +121,7 @@ func TestNewResultObject(t *testing.T) { tests := []struct { desc string - give interface{} + give any opts resultOptions wantFields []resultObjectField @@ -198,7 +198,7 @@ func TestNewResultObject(t *testing.T) { func TestNewResultObjectErrors(t *testing.T) { tests := []struct { desc string - give interface{} + give any opts resultOptions err string }{ diff --git a/scope.go b/scope.go index b03b5087..0c0b99d2 100644 --- a/scope.go +++ b/scope.go @@ -280,7 +280,7 @@ func (s *Scope) clock() digclock.Clock { // adds a new graphNode to this Scope and all of its descendent // scope. -func (s *Scope) newGraphNode(wrapped interface{}, orders map[*Scope]int) { +func (s *Scope) newGraphNode(wrapped any, orders map[*Scope]int) { orders[s] = s.gh.NewNode(wrapped) for _, cs := range s.childScopes { cs.newGraphNode(wrapped, orders)