File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,13 @@ func BindTo(impl, iface any) Option {
248248 })
249249}
250250
251+ // BindFor allows binding of implementations to the compile-time type. It
252+ // is equivalent to BindTo(v, (*T)(nil)), but can usually infer the
253+ // correct type T.
254+ func BindFor [T any ](v T ) Option {
255+ return BindTo (v , (* T )(nil ))
256+ }
257+
251258// BindToProvider binds an injected value to a provider function.
252259//
253260// The provider function must have one of the following signatures:
Original file line number Diff line number Diff line change @@ -43,6 +43,26 @@ func TestBindTo(t *testing.T) {
4343 assert .Equal (t , "foo" , saw )
4444}
4545
46+ func TestBindFor (t * testing.T ) {
47+ type iface interface {
48+ Method ()
49+ }
50+
51+ saw := ""
52+ method := func (i iface ) error {
53+ saw = string (i .(impl )) //nolint
54+ return nil
55+ }
56+
57+ var cli struct {}
58+
59+ p , err := New (& cli , BindFor (iface (impl ("foo" ))))
60+ assert .NoError (t , err )
61+ err = callFunction (reflect .ValueOf (method ), p .bindings )
62+ assert .NoError (t , err )
63+ assert .Equal (t , "foo" , saw )
64+ }
65+
4666func TestInvalidCallback (t * testing.T ) {
4767 type iface interface {
4868 Method ()
You can’t perform that action at this time.
0 commit comments