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

[proposal] Auto magically inject services #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

samber
Copy link
Owner

@samber samber commented Jan 28, 2023

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:

  • do.InjectTag(do.Injector, Service) (Service, error)
  • do.MustInjectTag(do.Injector, Service) (Service)
  • do.TagProvider[T any](t T) Provider[T]

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
Copy link

Codecov Report

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

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

📣 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
@@            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%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tag.go 72.88% <72.88%> (ø)
service.go 92.30% <100.00%> (+3.41%) ⬆️
service_eager.go 76.00% <100.00%> (+2.08%) ⬆️
service_lazy.go 94.52% <100.00%> (+0.15%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

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

@counterposition
Copy link

counterposition commented Feb 11, 2023

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
Copy link

@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.

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

Successfully merging this pull request may close these issues.

None yet

4 participants