Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Root container provided function may look provided parameter in child scope too to resolve it's params #374

Open
alexisvisco opened this issue Jan 25, 2023 · 1 comment
Assignees

Comments

@alexisvisco
Copy link

Describe the bug
When a invoker need a parameter that is provided by the root container, and the provided function need a param that is provided by the child scope : the parameter needed for invoking the function will not be resolved

To Reproduce

package main

import (
	"fmt"
	"go.uber.org/dig"
)

type config struct {
	str               string
	boolPassedInScope bool
}

func main() {

	mainContainer := dig.New()

	mainContainer.Provide(func() string { return "Hello" })

	// boolPassedInScope will be passed in the child scope
	mainContainer.Provide(func(helloStr string, boolPassedInScope bool) config {
		return config{str: helloStr, boolPassedInScope: boolPassedInScope}
	})

	scope := mainContainer.Scope("newscope")

	scope.Provide(func() bool { return true })

	// this will fail because boolPassedInScope is not provided in the main scope
	// and the config cannot be resolved due to the fact that in the mainContainer there
	// is no boolean provided
	err := scope.Invoke(func(c config) {
		fmt.Println(c.str)
		fmt.Println(c.boolPassedInScope)
	})

	if err != nil {
		panic(err)
	}

}

Expected behavior
I don't know if it's possible but without adding dig.Export to the line scope.Provide(func() bool { return true }) the provided function to create the config should look in the child scope to find the boolPassedInScope.

Additional context
Maybe my approach is wrong but in my application framework I need to pass context, logger that is scoped because I launch multiple invoke in parallel so I can't just set them in the root container (because the logger and ctx is customized for the invoker).

Also I can't use named dig parameter because the invoker signature must not change.

@sywhang sywhang self-assigned this Feb 21, 2023
@r-hang
Copy link
Contributor

r-hang commented Mar 21, 2023

Hey @alexisvisco, the current design intends for child types to not be available in their parent scopes.

One possible option is to utilize a value group in the parent scope to contain all of the unique values that you were producing in your child scopes that you could call your scoped invokes with. Here the invoker signature would not change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants