samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.82k stars 75 forks source link

[proposal] Auto magically inject services #28

Open samber opened 1 year ago

samber commented 1 year ago

Linked to #9

This contribution adds support for a service declaration in struct tags. Such as:

type Car struct {
    Engine *Engine `do:""`
    Brand  string  `do:"awesome-brand"`
    Other  int
}

In this proposal, I suggest 3 new helpers:

This implementation is obviously unsafe since it uses reflect and unsafe std libs.

Example:

type Engine struct {
    Watt int
}

func NewEngine(i *do.Injector) (*Engine, error) {
    return &Engine{42}, nil
}

do.Provide(injector, NewEngine)
do.ProvideNamedValue(injector, "awesome-brand", "tesla")
type Car struct {
    Engine *Engine `do:""`
    Brand  string  `do:"awesome-brand"`
    Other  int
}

func NewCar(i *do.Injector) (*Car, error) {
    return do.InjectTag(i, &Car{})
}

do.Provide(injector, NewCar)
// could be replaced by do.Provide(injector, do.TagProvider(&Car{}))

IMO, we should avoid calling this helper automatically on service invocation. It would be error-prone and costly. Being declarative also allows the developer to execute do.InjectTag at the right time in the provider.

do.TagProvider would be a shortcut for very simple providers:

do.Provide(injector, do.TagProvider(&Car{}))

// is equivalent to:

do.Provide(injector, NewCar(i *do.Injector) (*Car, error) {
    return do.InjectTag(i, &Car{})
})

I would be very happy to receive feedback about this!

codecov-commenter commented 1 year ago

Codecov Report

Base: 89.56% // Head: 87.85% // Decreases project coverage by -1.71% :warning:

Coverage data is based on head (7a95bf7) compared to base (16a48d1). Patch coverage: 76.47% of modified lines in pull request are covered.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #28 +/- ## ========================================== - Coverage 89.56% 87.85% -1.71% ========================================== Files 6 7 +1 Lines 460 527 +67 ========================================== + Hits 412 463 +51 - Misses 40 52 +12 - Partials 8 12 +4 ``` | Flag | Coverage Δ | | |---|---|---| | unittests | `87.85% <76.47%> (-1.71%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#carryforward-flags-in-the-pull-request-comment) to find out more. | [Impacted Files](https://codecov.io/gh/samber/do/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe) | Coverage Δ | | |---|---|---| | [tag.go](https://codecov.io/gh/samber/do/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#diff-dGFnLmdv) | `72.88% <72.88%> (ø)` | | | [service.go](https://codecov.io/gh/samber/do/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#diff-c2VydmljZS5nbw==) | `92.30% <100.00%> (+3.41%)` | :arrow_up: | | [service\_eager.go](https://codecov.io/gh/samber/do/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#diff-c2VydmljZV9lYWdlci5nbw==) | `76.00% <100.00%> (+2.08%)` | :arrow_up: | | [service\_lazy.go](https://codecov.io/gh/samber/do/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#diff-c2VydmljZV9sYXp5Lmdv) | `94.52% <100.00%> (+0.15%)` | :arrow_up: | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

counterposition commented 1 year ago

If I wanted to use Uber Fx, I'd just use Uber Fx. DI without reflection is one of the biggest advantages do has over Fx.

NoGambiNoBugs commented 1 year ago

@counterposition without reflection? Ok, very good, but uses assertion, and assertion is not safe type like this package says in readme.MD ("type safe").

https://github.com/samber/do/blob/master/injector.go#L57 https://github.com/samber/do/blob/master/injector.go#L177 https://github.com/samber/do/blob/master/injector.go#L201 https://github.com/samber/do/blob/master/di.go#L128

What is the problem to use reflect? The generics here is a fake, is just to generate names for a map[string]any.