Skip to content

Commit

Permalink
chore: rename container.AutoGroupType to container.ManyPerContainerTy…
Browse files Browse the repository at this point in the history
…pe (#11978)

Co-authored-by: Marko <marbar3778@yahoo.com>
  • Loading branch information
facundomedica and tac0turtle authored May 18, 2022
1 parent 0b810ba commit bc2d553
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
16 changes: 8 additions & 8 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ func (c *container) getResolver(typ reflect.Type) (resolver, error) {
}

elemType := typ
if isAutoGroupSliceType(elemType) || isOnePerModuleMapType(elemType) {
if isManyPerContainerSliceType(elemType) || isOnePerModuleMapType(elemType) {
elemType = elemType.Elem()
}

var typeGraphNode *graphviz.Node

if isAutoGroupType(elemType) {
c.logf("Registering resolver for auto-group type %v", elemType)
if isManyPerContainerType(elemType) {
c.logf("Registering resolver for many-per-container type %v", elemType)
sliceType := reflect.SliceOf(elemType)

typeGraphNode = c.typeGraphNode(sliceType)
typeGraphNode.SetComment("auto-group")
typeGraphNode.SetComment("many-per-container")

r := &groupResolver{
typ: elemType,
Expand Down Expand Up @@ -141,8 +141,8 @@ func (c *container) addNode(provider *ProviderDescriptor, key *moduleKey) (inter
hasOwnModuleKeyParam = true
}

if isAutoGroupType(typ) {
return nil, fmt.Errorf("auto-group type %v can't be used as an input parameter", typ)
if isManyPerContainerType(typ) {
return nil, fmt.Errorf("many-per-container type %v can't be used as an input parameter", typ)
} else if isOnePerModuleType(typ) {
return nil, fmt.Errorf("one-per-module type %v can't be used as an input parameter", typ)
}
Expand Down Expand Up @@ -184,8 +184,8 @@ func (c *container) addNode(provider *ProviderDescriptor, key *moduleKey) (inter
typ, typ.Elem())
}

// auto-group slices of auto-group types
if isAutoGroupSliceType(typ) {
// many-per-container slices of many-per-container types
if isManyPerContainerSliceType(typ) {
typ = typ.Elem()
}

Expand Down
24 changes: 12 additions & 12 deletions container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Command struct {
Run func()
}

func (Command) IsAutoGroupType() {}
func (Command) IsManyPerContainerType() {}

func ProvideKVStoreKey(moduleKey container.ModuleKey) KVStoreKey {
return KVStoreKey{name: moduleKey.Name()}
Expand Down Expand Up @@ -401,19 +401,19 @@ func TestOnePerModule(t *testing.T) {
)
}

type AutoGroupInt int
type ManyPerContainerInt int

func (AutoGroupInt) IsAutoGroupType() {}
func (ManyPerContainerInt) IsManyPerContainerType() {}

func TestAutoGroup(t *testing.T) {
var xs []AutoGroupInt
func TestManyPerContainer(t *testing.T) {
var xs []ManyPerContainerInt
var sum string
require.NoError(t,
container.Build(
container.Provide(
func() AutoGroupInt { return 4 },
func() AutoGroupInt { return 9 },
func(xs []AutoGroupInt) string {
func() ManyPerContainerInt { return 4 },
func() ManyPerContainerInt { return 9 },
func(xs []ManyPerContainerInt) string {
sum := 0
for _, x := range xs {
sum += int(x)
Expand All @@ -426,15 +426,15 @@ func TestAutoGroup(t *testing.T) {
),
)
require.Len(t, xs, 2)
require.Contains(t, xs, AutoGroupInt(4))
require.Contains(t, xs, AutoGroupInt(9))
require.Contains(t, xs, ManyPerContainerInt(4))
require.Contains(t, xs, ManyPerContainerInt(9))
require.Equal(t, "13", sum)

var z AutoGroupInt
var z ManyPerContainerInt
require.Error(t,
container.Build(
container.Provide(
func() AutoGroupInt { return 0 },
func() ManyPerContainerInt { return 0 },
),
&z,
),
Expand Down
24 changes: 12 additions & 12 deletions container/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
"github.com/cosmos/cosmos-sdk/container/internal/graphviz"
)

// AutoGroupType marks a type which automatically gets grouped together. For an AutoGroupType T,
// ManyPerContainerType marks a type which automatically gets grouped together. For an ManyPerContainerType T,
// T and []T can be declared as output parameters for providers as many times within the container
// as desired. All of the provided values for T can be retrieved by declaring an
// []T input parameter.
type AutoGroupType interface {
// IsAutoGroupType is a marker function which just indicates that this is a auto-group type.
IsAutoGroupType()
type ManyPerContainerType interface {
// IsManyPerContainerType is a marker function which just indicates that this is a many-per-container type.
IsManyPerContainerType()
}

var autoGroupTypeType = reflect.TypeOf((*AutoGroupType)(nil)).Elem()
var manyPerContainerTypeType = reflect.TypeOf((*ManyPerContainerType)(nil)).Elem()

func isAutoGroupType(t reflect.Type) bool {
return t.Implements(autoGroupTypeType)
func isManyPerContainerType(t reflect.Type) bool {
return t.Implements(manyPerContainerTypeType)
}

func isAutoGroupSliceType(typ reflect.Type) bool {
return typ.Kind() == reflect.Slice && isAutoGroupType(typ.Elem())
func isManyPerContainerSliceType(typ reflect.Type) bool {
return typ.Kind() == reflect.Slice && isManyPerContainerType(typ.Elem())
}

type groupResolver struct {
Expand All @@ -43,12 +43,12 @@ type sliceGroupResolver struct {
}

func (g *groupResolver) describeLocation() string {
return fmt.Sprintf("auto-group type %v", g.typ)
return fmt.Sprintf("many-per-container type %v", g.typ)
}

func (g *sliceGroupResolver) resolve(c *container, _ *moduleKey, caller Location) (reflect.Value, error) {
// Log
c.logf("Providing auto-group type slice %v to %s from:", g.sliceType, caller.Name())
c.logf("Providing many-per-container type slice %v to %s from:", g.sliceType, caller.Name())
c.indentLogger()
for _, node := range g.providers {
c.logf(node.provider.Location.String())
Expand Down Expand Up @@ -81,7 +81,7 @@ func (g *sliceGroupResolver) resolve(c *container, _ *moduleKey, caller Location
}

func (g *groupResolver) resolve(_ *container, _ *moduleKey, _ Location) (reflect.Value, error) {
return reflect.Value{}, errors.Errorf("%v is an auto-group type and cannot be used as an input value, instead use %v", g.typ, g.sliceType)
return reflect.Value{}, errors.Errorf("%v is an many-per-container type and cannot be used as an input value, instead use %v", g.typ, g.sliceType)
}

func (g *groupResolver) addNode(n *simpleProvider, i int) error {
Expand Down
2 changes: 1 addition & 1 deletion container/testdata/example.dot
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ digraph "" {
"github.com/cosmos/cosmos-sdk/container_test.ProvideKVStoreKey"[color="black", fontcolor="black", penwidth="1.5", shape="box"];
}

"[]github.com/cosmos/cosmos-sdk/container_test.Command"[color="lightgrey", comment="auto-group", fontcolor="dimgrey", penwidth="0.5"];
"[]github.com/cosmos/cosmos-sdk/container_test.Command"[color="lightgrey", comment="many-per-container", fontcolor="dimgrey", penwidth="0.5"];
"github.com/cosmos/cosmos-sdk/container.ModuleKey"[color="black", fontcolor="black", penwidth="1.5"];
"github.com/cosmos/cosmos-sdk/container.OwnModuleKey"[color="lightgrey", fontcolor="dimgrey", penwidth="0.5"];
"github.com/cosmos/cosmos-sdk/container_test.KVStoreKey"[color="black", fontcolor="black", penwidth="1.5"];
Expand Down
2 changes: 1 addition & 1 deletion container/testdata/example_error.dot
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ digraph "" {
"github.com/cosmos/cosmos-sdk/container_test.ProvideKVStoreKey"[color="black", fontcolor="black", penwidth="1.5", shape="box"];
}

"[]github.com/cosmos/cosmos-sdk/container_test.Command"[color="lightgrey", comment="auto-group", fontcolor="dimgrey", penwidth="0.5"];
"[]github.com/cosmos/cosmos-sdk/container_test.Command"[color="lightgrey", comment="many-per-container", fontcolor="dimgrey", penwidth="0.5"];
"github.com/cosmos/cosmos-sdk/container.ModuleKey"[color="black", fontcolor="black", penwidth="1.5"];
"github.com/cosmos/cosmos-sdk/container.OwnModuleKey"[color="lightgrey", fontcolor="dimgrey", penwidth="0.5"];
"github.com/cosmos/cosmos-sdk/container_test.KVStoreKey"[color="black", fontcolor="black", penwidth="1.5"];
Expand Down

0 comments on commit bc2d553

Please sign in to comment.