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:
Alec Thomas
2024-11-01 12:23:32 +11:00
parent 373692af87
commit 7bbb0b76ad
3 changed files with 31 additions and 24 deletions
+6 -1
View File
@@ -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)