Skip to content

Commit

Permalink
Improve documentation for ProvideValue (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
matdurand authored Dec 12, 2023
1 parent e1c96e7 commit 282f32c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Modules](#modules)
- [Tags](#tags)
- [ProvideValue](#providevalue)
- [Optional Parameters](#optional-parameters)
- [Struct Field Injection](#struct-field-injection)
- [Iteration](#iteration)
Expand Down Expand Up @@ -101,6 +102,39 @@ var db []*Database
di.Resolve(&db, di.Tags{"type": "*"})
```

### ProvideValue

Instead of using `di.Provide` to provide a constructor, you can use `di.ProvideValue` and provide values directly.
This is useful to provide primitive values or values that are easily constructed. You can combine this with the use
of `di.Tags` if you have multiple values of the same type and want to identify each one.

```go
di.New(
di.ProvideValue(time.Duration(10*time.Second), di.Tags{"name": "http-timeout"}),
)

var timeout time.Duration
c.Resolve(&timeout, di.Tags{"name": "http-timeout"})
```

To differentiate between multiple values of the same type, you can also use golang type aliases instead of using
`di.Tags`.

```go
type ProjectName string
type ProjectVersion string

c, err := di.New(
di.ProvideValue(ProjectName("my-project")),
di.ProvideValue(ProjectVersion("1.0.0")),
)

var pn ProjectName
c.Resolve(&pn)
var pv ProjectVersion
c.Resolve(&pv)
```

### Optional Parameters

Also, `di.Inject` with tag `di:"optional"` provides the ability to skip a dependency
Expand Down

0 comments on commit 282f32c

Please sign in to comment.