From cd243fd0d2fa9ddd5d0a2d189ff9ab0caefae9bc Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Wed, 8 Jun 2022 13:25:31 -0300 Subject: [PATCH] docs(depinject): add examples (#12110) ## 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) --- depinject/README.md | 85 +++++++++++++++++++++++++++++++++++++++++++-- depinject/inject.go | 2 +- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/depinject/README.md b/depinject/README.md index 7534573a65..c234bead99 100644 --- a/depinject/README.md +++ b/depinject/README.md @@ -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 diff --git a/depinject/inject.go b/depinject/inject.go index cdb2941011..1f28300f0c 100644 --- a/depinject/inject.go +++ b/depinject/inject.go @@ -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.