-
Notifications
You must be signed in to change notification settings - Fork 295
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
Private Modules #1030
Private Modules #1030
Conversation
Some users have been requesting that our private provides functionality provided by uber-go#995 be extended by allowing a way to privatize an entire module. This change modifies fx.Private to implement fx.Option, allowing it to be passed to fx.App and fx.Module. When passed into a module, the entire module will be marked as private, meaning all provides and supplies of that module and its inner modules will be marked as private. ```go fx.New( fx.Module("SubModule", fx.Private, fx.Supply(5), fx.Provide(func() string { return "b"}), fx.Invoke(a int, b str) {}, // Runs w/ a = 5, b = "b" ), fx.Invoke(func(a int) {}), // This will fail fx.Invoke(func(b str) {}), // This will also fail ) ``` I opted for this API to stay consistent with the style of API we already allow for passing Private to Provide directly. This change also extracts all private-related definitions and testing out into separate files, `private.go` and `private_test.go`.
Codecov Report
@@ Coverage Diff @@
## master #1030 +/- ##
==========================================
- Coverage 98.04% 97.86% -0.19%
==========================================
Files 39 40 +1
Lines 1994 2010 +16
==========================================
+ Hits 1955 1967 +12
- Misses 31 34 +3
- Partials 8 9 +1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Closing for now, missed a couple key things |
@JacobOaks out of curiosity: do you remember what was missing here? |
I think we were unsure about the utility an entirely private module would provide. After all, an entirely private module, by definition, cannot contribute anything to an application's container. However, invokes within the private module would still run and cause the private module's dependencies to build - so I guess that's an exception. But that almost sort of feels like encouraging strange side-effect/hyrum's law-esque dependencies. I think something closer to @jkanywhere's suggestion on #1020 may be better: it allows easily "privatizing" larger collections of options, while still allowing otherwise mostly private modules to expose key type(s) to other modules/the container. |
Ack, yeah. So the use case I was imagining was something like this. fx.New(httpserver.Module).Run() For reasons, I want to have two separate HTTP servers.
I hadn't fully thought through how private modules would "export" values outside the private module.
I like that! It's intuitively similar to internal/ directories in Go: only siblings and direct parent can use outputs of private modules. |
Some users have been requesting that our private provides functionality provided by #995 be extended by allowing a way to privatize an entire module (Ref: #1020).
This change modifies fx.Private to implement fx.Option, allowing it to be passed to fx.App and fx.Module.
When passed into a module, the entire module will be marked as private, meaning all provides and supplies of that module and its inner modules will be marked as private.
I opted for this API to stay consistent with the style of API we already allow for passing Private to Provide directly.
This change also extracts all private-related definitions and testing out into separate files,
private.go
andprivate_test.go
.