Skip to content
Closed
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
5 changes: 5 additions & 0 deletions annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,11 @@ func As(interfaces ...any) Annotation {
return &asAnnotation{targets: interfaces}
}

// AsType is [As](new(T)). Same as errors.AsType versus errors.As.
func AsType[T any]() Annotation {
return As(new(T))
}

// Self returns a special value that can be passed to [As] to indicate
// that a type should be provided as its original type, in addition to whatever other
// types it gets provided as via other [As] annotations.
Expand Down
9 changes: 9 additions & 0 deletions annotated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ func TestAnnotatedAs(t *testing.T) {
assert.Equal(t, s.String(), "a good stringer")
},
},
{
desc: "provide a good stringer via AsType",
provide: fx.Provide(
fx.Annotate(newAsStringer, fx.AsType[fmt.Stringer]()),
),
invoke: func(s fmt.Stringer) {
assert.Equal(t, s.String(), "a good stringer")
},
},
{
desc: "value type implementing interface",
provide: fx.Provide(
Expand Down