Skip to content

Commit

Permalink
Component collection refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Oct 3, 2024
1 parent 04f2727 commit b6d4d50
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions component/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package component
// Collection is a collection of components with useful methods
type Collection map[string]*Component

// NewComponentCollection creates empty collection
func NewComponentCollection() Collection {
// NewCollection creates empty collection
func NewCollection() Collection {
return make(Collection)
}

Expand All @@ -13,8 +13,8 @@ func (collection Collection) ByName(name string) *Component {
return collection[name]
}

// Add adds components to existing collection
func (collection Collection) Add(components ...*Component) Collection {
// With adds components and returns the collection
func (collection Collection) With(components ...*Component) Collection {
for _, component := range components {
collection[component.Name()] = component
}
Expand Down
16 changes: 8 additions & 8 deletions component/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCollection_ByName(t *testing.T) {
}{
{
name: "component found",
components: NewComponentCollection().Add(New("c1"), New("c2")),
components: NewCollection().With(New("c1"), New("c2")),
args: args{
name: "c2",
},
Expand All @@ -32,7 +32,7 @@ func TestCollection_ByName(t *testing.T) {
},
{
name: "component not found",
components: NewComponentCollection().Add(New("c1"), New("c2")),
components: NewCollection().With(New("c1"), New("c2")),
args: args{
name: "c3",
},
Expand All @@ -46,7 +46,7 @@ func TestCollection_ByName(t *testing.T) {
}
}

func TestCollection_Add(t *testing.T) {
func TestCollection_With(t *testing.T) {
type args struct {
components []*Component
}
Expand All @@ -58,7 +58,7 @@ func TestCollection_Add(t *testing.T) {
}{
{
name: "adding nothing to empty collection",
collection: NewComponentCollection(),
collection: NewCollection(),
args: args{
components: nil,
},
Expand All @@ -68,7 +68,7 @@ func TestCollection_Add(t *testing.T) {
},
{
name: "adding to empty collection",
collection: NewComponentCollection(),
collection: NewCollection(),
args: args{
components: []*Component{New("c1"), New("c2")},
},
Expand All @@ -81,7 +81,7 @@ func TestCollection_Add(t *testing.T) {
},
{
name: "adding to non-empty collection",
collection: NewComponentCollection().Add(New("c1"), New("c2")),
collection: NewCollection().With(New("c1"), New("c2")),
args: args{
components: []*Component{New("c3"), New("c4")},
},
Expand All @@ -97,9 +97,9 @@ func TestCollection_Add(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.collection.Add(tt.args.components...)
collectionAfter := tt.collection.With(tt.args.components...)
if tt.assertions != nil {
tt.assertions(t, tt.collection)
tt.assertions(t, collectionAfter)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions fmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type FMesh struct {
func New(name string) *FMesh {
return &FMesh{
name: name,
components: component.NewComponentCollection(),
components: component.NewCollection(),
config: defaultConfig,
}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func (fm *FMesh) WithDescription(description string) *FMesh {
// WithComponents adds components to f-mesh
func (fm *FMesh) WithComponents(components ...*component.Component) *FMesh {
for _, c := range components {
fm.components.Add(c)
fm.components = fm.components.With(c)
}
return fm
}
Expand Down

0 comments on commit b6d4d50

Please sign in to comment.