## Description This renames `container.Scope` to `container.ModuleKey` to make it more consistent with the usage of scope for modules. It also renames all usages of `constructor` (in docs and variable name) to `provider` for consistency. --- ### 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)
185 lines
4.1 KiB
Go
185 lines
4.1 KiB
Go
package container
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// In can be embedded in another struct to inform the container that the
|
|
// fields of the struct should be treated as dependency inputs.
|
|
// This allows a struct to be used to specify dependencies rather than
|
|
// positional parameters.
|
|
//
|
|
// Fields of the struct may support the following tags:
|
|
// optional if set to true, the dependency is optional and will
|
|
// be set to its default value if not found, rather than causing
|
|
// an error
|
|
type In struct{}
|
|
|
|
func (In) isIn() {}
|
|
|
|
type isIn interface{ isIn() }
|
|
|
|
var isInType = reflect.TypeOf((*isIn)(nil)).Elem()
|
|
|
|
// Out can be embedded in another struct to inform the container that the
|
|
// fields of the struct should be treated as dependency outputs.
|
|
// This allows a struct to be used to specify outputs rather than
|
|
// positional return values.
|
|
type Out struct{}
|
|
|
|
func (Out) isOut() {}
|
|
|
|
type isOut interface{ isOut() }
|
|
|
|
var isOutType = reflect.TypeOf((*isOut)(nil)).Elem()
|
|
|
|
func expandStructArgsProvider(provider ProviderDescriptor) (ProviderDescriptor, error) {
|
|
var foundStructArgs bool
|
|
var newIn []ProviderInput
|
|
|
|
for _, in := range provider.Inputs {
|
|
if in.Type.AssignableTo(isInType) {
|
|
foundStructArgs = true
|
|
inTypes, err := structArgsInTypes(in.Type)
|
|
if err != nil {
|
|
return ProviderDescriptor{}, err
|
|
}
|
|
newIn = append(newIn, inTypes...)
|
|
} else {
|
|
newIn = append(newIn, in)
|
|
}
|
|
}
|
|
|
|
var newOut []ProviderOutput
|
|
for _, out := range provider.Outputs {
|
|
if out.Type.AssignableTo(isOutType) {
|
|
foundStructArgs = true
|
|
newOut = append(newOut, structArgsOutTypes(out.Type)...)
|
|
} else {
|
|
newOut = append(newOut, out)
|
|
}
|
|
}
|
|
|
|
if foundStructArgs {
|
|
return ProviderDescriptor{
|
|
Inputs: newIn,
|
|
Outputs: newOut,
|
|
Fn: expandStructArgsFn(provider),
|
|
Location: provider.Location,
|
|
}, nil
|
|
}
|
|
|
|
return provider, nil
|
|
}
|
|
|
|
func expandStructArgsFn(provider ProviderDescriptor) func(inputs []reflect.Value) ([]reflect.Value, error) {
|
|
fn := provider.Fn
|
|
inParams := provider.Inputs
|
|
outParams := provider.Outputs
|
|
return func(inputs []reflect.Value) ([]reflect.Value, error) {
|
|
j := 0
|
|
inputs1 := make([]reflect.Value, len(inParams))
|
|
for i, in := range inParams {
|
|
if in.Type.AssignableTo(isInType) {
|
|
v, n := buildIn(in.Type, inputs[j:])
|
|
inputs1[i] = v
|
|
j += n
|
|
} else {
|
|
inputs1[i] = inputs[j]
|
|
j++
|
|
}
|
|
}
|
|
|
|
outputs, err := fn(inputs1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var outputs1 []reflect.Value
|
|
for i, out := range outParams {
|
|
if out.Type.AssignableTo(isOutType) {
|
|
outputs1 = append(outputs1, extractFromOut(out.Type, outputs[i])...)
|
|
} else {
|
|
outputs1 = append(outputs1, outputs[i])
|
|
}
|
|
}
|
|
|
|
return outputs1, nil
|
|
}
|
|
}
|
|
|
|
func structArgsInTypes(typ reflect.Type) ([]ProviderInput, error) {
|
|
n := typ.NumField()
|
|
var res []ProviderInput
|
|
for i := 0; i < n; i++ {
|
|
f := typ.Field(i)
|
|
if f.Type.AssignableTo(isInType) {
|
|
continue
|
|
}
|
|
|
|
var optional bool
|
|
optTag, found := f.Tag.Lookup("optional")
|
|
if found {
|
|
if optTag == "true" {
|
|
optional = true
|
|
} else {
|
|
return nil, errors.Errorf("bad optional tag %q (should be \"true\") in %v", optTag, typ)
|
|
}
|
|
}
|
|
|
|
res = append(res, ProviderInput{
|
|
Type: f.Type,
|
|
Optional: optional,
|
|
})
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func structArgsOutTypes(typ reflect.Type) []ProviderOutput {
|
|
n := typ.NumField()
|
|
var res []ProviderOutput
|
|
for i := 0; i < n; i++ {
|
|
f := typ.Field(i)
|
|
if f.Type.AssignableTo(isOutType) {
|
|
continue
|
|
}
|
|
|
|
res = append(res, ProviderOutput{
|
|
Type: f.Type,
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func buildIn(typ reflect.Type, values []reflect.Value) (reflect.Value, int) {
|
|
numFields := typ.NumField()
|
|
j := 0
|
|
res := reflect.New(typ)
|
|
for i := 0; i < numFields; i++ {
|
|
f := typ.Field(i)
|
|
if f.Type.AssignableTo(isInType) {
|
|
continue
|
|
}
|
|
|
|
res.Elem().Field(i).Set(values[j])
|
|
j++
|
|
}
|
|
return res.Elem(), j
|
|
}
|
|
|
|
func extractFromOut(typ reflect.Type, value reflect.Value) []reflect.Value {
|
|
numFields := typ.NumField()
|
|
var res []reflect.Value
|
|
for i := 0; i < numFields; i++ {
|
|
f := typ.Field(i)
|
|
if f.Type.AssignableTo(isOutType) {
|
|
continue
|
|
}
|
|
|
|
res = append(res, value.Field(i))
|
|
}
|
|
return res
|
|
}
|