uber-go / fx

A dependency injection based application framework for Go.
https://uber-go.github.io/fx/
MIT License
5.48k stars 283 forks source link

Support fx.Private w/ fx.Supply #1207

Closed JacobOaks closed 1 month ago

JacobOaks commented 1 month ago

fx.Supply is essentially an API that allows for conveniently fx.Provideing an exact value, rather than a function that will return that value. For example, fx.Provide(func() int { return 5 }) is equivalent to fx.Supply(5).

fx.Private allows for usage of a provided constructor's results to be restricted to the current module and its child modules.

fx.Module(
    "parent",
    fx.Invoke(func(int) { /* this will error out! */ }),
    fx.Module(
        "child",
        fx.Provide(func() int { return 5 }, fx.Private),
    ),
),

This PR allows for using fx.Private with fx.Supply as well, so that folks can enjoy the convenience of fx.Supply when they also wish to restrict the usage of the supplied value.

fx.Module(
    "parent"
    fx.Invoke(func(int) { /* this will error out! */ }),
    fx.Module(
        "child",
        fx.Supply(5, fx.Private),
    ),
),

Ref #1206

Since the behavior between Supply + Private and Provide + Private should be identical, I opted to generalize the existing fx.Private tests to run for both Provide and Supply. This keeps the tests a little more DRY but does complicate them/hurt readability. I feel like this is OK since there are a lot of tests, but I also am the one who wrote the tests, so I am biased regarding its readability. Thus, I am happy to break out Supply + Private into its own tests if folks feel strongly that these tests are hard to read.

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.41%. Comparing base (9e6f6c2) to head (258fe12).

:exclamation: Current head 258fe12 differs from pull request most recent head 8c372ea

Please upload reports for the commit 8c372ea to get more accurate results.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1207 +/- ## ========================================== - Coverage 98.51% 98.41% -0.10% ========================================== Files 34 34 Lines 2900 2908 +8 ========================================== + Hits 2857 2862 +5 - Misses 36 38 +2 - Partials 7 8 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.