Adds Context.BindToProvider (#201)

This commit is contained in:
Stan Rozenraukh
2021-08-30 16:47:02 -04:00
committed by GitHub
parent d0c0180cec
commit 74cb5130e3
3 changed files with 37 additions and 20 deletions
+9 -2
View File
@@ -113,8 +113,15 @@ func (c *Context) Bind(args ...interface{}) {
//
// BindTo(impl, (*MyInterface)(nil))
func (c *Context) BindTo(impl, iface interface{}) {
valueOf := reflect.ValueOf(impl)
c.bindings[reflect.TypeOf(iface).Elem()] = func() (reflect.Value, error) { return valueOf, nil }
c.bindings.addTo(impl, iface)
}
// BindToProvider allows binding of provider functions.
//
// This is useful when the Run() function of different commands require different values that may
// not all be initialisable from the main() function.
func (c *Context) BindToProvider(provider interface{}) error {
return c.bindings.addProvider(provider)
}
// Value returns the value for a particular path element.