Skip to content

Commit

Permalink
docs(depinject): add examples (#12110)
Browse files Browse the repository at this point in the history
## Description

Closes: #11944



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
facundomedica authored Jun 8, 2022
1 parent 64409bf commit cd243fd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
85 changes: 82 additions & 3 deletions depinject/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,91 @@
# Cosmos SDK Dependency Injection `container` Module
# Cosmos SDK Dependency Injection `depinject` Module

## Overview

TODO
`depinject` is a dependency injection framework for the Cosmos SDK. This module together with `core/appconfig` are meant
to simplify the definition of a blockchain by replacing most of app.go's boilerplate code with a configuration file (YAML or JSON).

## Usage

TODO
### `depinject` example

```go
package main

import (
"fmt"

"cosmossdk.io/depinject"
)

type AnotherInt int

func main() {
var (
x int
y AnotherInt
)

fmt.Printf("Before (%v, %v)\n", x, y)
depinject.Inject(
depinject.Provide(
func() int { return 1 },
func() AnotherInt { return AnotherInt(2) },
),
&x,
&y,
)
fmt.Printf("After (%v, %v)\n", x, y)
}
```

### Full example in real app

```go
//go:embed app.yaml
var appConfigYaml []byte

var appConfig = appconfig.LoadYAML(appConfigYaml)

func NewSimApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
skipUpgradeHeights map[int64]bool,
homePath string,
invCheckPeriod uint,
encodingConfig simappparams.EncodingConfig,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
app := &SimApp{
invCheckPeriod: invCheckPeriod,
}

var (
appBuilder *runtime.AppBuilder
msgServiceRouter *baseapp.MsgServiceRouter
)

err := depinject.Inject(AppConfig,
&appBuilder,
&app.ParamsKeeper,
&app.CapabilityKeeper,
&app.appCodec,
&app.legacyAmino,
&app.interfaceRegistry,
&app.AccountKeeper,
&app.BankKeeper,
&app.FeeGrantKeeper,
&app.StakingKeeper,
&msgServiceRouter,
)
if err != nil {
panic(err)
}
...
```
## Debugging
Expand Down
2 changes: 1 addition & 1 deletion depinject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package depinject
//
// Ex:
// var x int
// Build(Provide(func() int { return 1 }), &x)
// Inject(Provide(func() int { return 1 }), &x)
//
// Inject uses the debug mode provided by AutoDebug which means there will be
// verbose debugging information if there is an error and nothing upon success.
Expand Down

0 comments on commit cd243fd

Please sign in to comment.