Skip to content

[proposal] Auto magically inject services #28

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

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-commenter commented Jan 28, 2023

Codecov Report

Attention: Patch coverage is 76.47059% with 16 lines in your changes missing coverage. Please review.

Project coverage is 87.85%. Comparing base (16a48d1) to head (7a95bf7).
Report is 28 commits behind head on master.

Files with missing lines Patch % Lines
tag.go 72.88% 12 Missing and 4 partials ⚠️
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.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

4 participants