cosmos-sdk/container/provider_desc.go

105 lines
2.4 KiB
Go
Raw Normal View History

feat: implement low-level dependency injection container (#9666) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description closes #9775 needs #9658 --- ### 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/master/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/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
2021-10-04 13:36:41 -07:00
package container
import (
"reflect"
"github.com/pkg/errors"
)
// ProviderDescriptor defines a special constructor type that is defined by
// reflection. It should be passed as a value to the Provide function.
// Ex:
// option.Provide(ProviderDescriptor{ ... })
type ProviderDescriptor struct {
// Inputs defines the in parameter types to Fn.
Inputs []ProviderInput
// Outputs defines the out parameter types to Fn.
Outputs []ProviderOutput
// Fn defines the constructor function.
Fn func([]reflect.Value) ([]reflect.Value, error)
// Location defines the source code location to be used for this constructor
// in error messages.
Location Location
}
type ProviderInput struct {
Type reflect.Type
Optional bool
}
type ProviderOutput struct {
Type reflect.Type
}
func ExtractProviderDescriptor(provider interface{}) (ProviderDescriptor, error) {
rctr, ok := provider.(ProviderDescriptor)
if !ok {
var err error
rctr, err = doExtractProviderDescriptor(provider)
if err != nil {
return ProviderDescriptor{}, err
}
}
return expandStructArgsConstructor(rctr)
}
func doExtractProviderDescriptor(ctr interface{}) (ProviderDescriptor, error) {
val := reflect.ValueOf(ctr)
typ := val.Type()
if typ.Kind() != reflect.Func {
return ProviderDescriptor{}, errors.Errorf("expected a Func type, got %v", typ)
}
loc := LocationFromPC(val.Pointer())
if typ.IsVariadic() {
return ProviderDescriptor{}, errors.Errorf("variadic function can't be used as a constructor: %s", loc)
}
numIn := typ.NumIn()
in := make([]ProviderInput, numIn)
for i := 0; i < numIn; i++ {
in[i] = ProviderInput{
Type: typ.In(i),
}
}
errIdx := -1
numOut := typ.NumOut()
var out []ProviderOutput
for i := 0; i < numOut; i++ {
t := typ.Out(i)
if t == errType {
if i != numOut-1 {
return ProviderDescriptor{}, errors.Errorf("output error parameter is not last parameter in function %s", loc)
}
errIdx = i
} else {
out = append(out, ProviderOutput{Type: t})
}
}
return ProviderDescriptor{
Inputs: in,
Outputs: out,
Fn: func(values []reflect.Value) ([]reflect.Value, error) {
res := val.Call(values)
if errIdx >= 0 {
err := res[errIdx]
if !err.IsZero() {
return nil, err.Interface().(error)
}
return res[0:errIdx], nil
}
return res, nil
},
Location: loc,
}, nil
}
var errType = reflect.TypeOf((*error)(nil)).Elem()