Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 (
Expand All @@ -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
}

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -558,7 +558,7 @@ const (

type lifecycleHookAnnotation struct {
Type _lifecycleHookAnnotationType
Target interface{}
Target any
}

var _ Annotation = (*lifecycleHookAnnotation)(nil)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1176,15 +1176,15 @@ 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,
}
}

type asAnnotation struct {
targets []interface{}
targets []any
types []asType
}

Expand Down Expand Up @@ -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}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1430,7 +1430,7 @@ func extractResultFields(types []reflect.Type) ([]reflect.StructField, func(int,
}

type fromAnnotation struct {
targets []interface{}
targets []any
types []reflect.Type
}

Expand Down Expand Up @@ -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}
}

Expand All @@ -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
Expand Down Expand Up @@ -1613,7 +1613,7 @@ func (fr *fromAnnotation) parameters(ann *annotated) (
}

type annotated struct {
Target interface{}
Target any
Annotations []Annotation
ParamTags []string
ResultTags []string
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 18 additions & 25 deletions annotated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -304,7 +303,7 @@ func TestAnnotatedFromFailures(t *testing.T) {
tests := []struct {
desc string
provide fx.Option
invoke interface{}
invoke any
errorContains string
}{
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -452,7 +450,7 @@ func TestAnnotatedAs(t *testing.T) {
tests := []struct {
desc string
provide fx.Option
invoke interface{}
invoke any
startApp bool
}{
{
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -837,7 +834,7 @@ func TestAnnotatedAsFailures(t *testing.T) {
tests := []struct {
desc string
provide fx.Option
invoke interface{}
invoke any
errorContains string
}{
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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"`),
),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2287,8 +2282,8 @@ func TestHookAnnotationFailures(t *testing.T) {
}

type (
A interface{}
B interface{}
A any
B any
)

type namedAndGroupHookParams struct {
Expand All @@ -2311,7 +2306,7 @@ func TestHookAnnotationFailures(t *testing.T) {

table := []struct {
name string
annotation interface{}
annotation any
extraOpts fx.Option
useNew bool
errContains string
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading