feat: support recursive injection of provider parameters
This allows provider functions to accept parameters that are injected by other
bindings or binding providers, eg. call the provider function with the root CLI
struct (which is automatically bound by Kong):
kong.BindToProvider(func(cli *CLI) (*Injected, error) { ... })
This commit is contained in:
+6
-1
@@ -89,11 +89,13 @@ func TestCallbackCustomError(t *testing.T) {
|
||||
}
|
||||
|
||||
type bindToProviderCLI struct {
|
||||
Filled bool `default:"true"`
|
||||
Called bool
|
||||
Cmd bindToProviderCmd `cmd:""`
|
||||
}
|
||||
|
||||
type boundThing struct {
|
||||
Filled bool
|
||||
}
|
||||
|
||||
type bindToProviderCmd struct{}
|
||||
@@ -105,7 +107,10 @@ func (*bindToProviderCmd) Run(cli *bindToProviderCLI, b *boundThing) error {
|
||||
|
||||
func TestBindToProvider(t *testing.T) {
|
||||
var cli bindToProviderCLI
|
||||
app, err := New(&cli, BindToProvider(func() (*boundThing, error) { return &boundThing{}, nil }))
|
||||
app, err := New(&cli, BindToProvider(func(cli *bindToProviderCLI) (*boundThing, error) {
|
||||
assert.True(t, cli.Filled, "CLI struct should have already been populated by Kong")
|
||||
return &boundThing{Filled: cli.Filled}, nil
|
||||
}))
|
||||
assert.NoError(t, err)
|
||||
ctx, err := app.Parse([]string{"cmd"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user