From b6d4d50a0416aa50b90a852f4c16b41013461bdf Mon Sep 17 00:00:00 2001 From: hovsep Date: Fri, 4 Oct 2024 01:21:44 +0300 Subject: [PATCH] Component collection refactored --- component/collection.go | 8 ++++---- component/collection_test.go | 16 ++++++++-------- fmesh.go | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/component/collection.go b/component/collection.go index cde1f62..af30267 100644 --- a/component/collection.go +++ b/component/collection.go @@ -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) } @@ -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 } diff --git a/component/collection_test.go b/component/collection_test.go index ff5888e..306b53d 100644 --- a/component/collection_test.go +++ b/component/collection_test.go @@ -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", }, @@ -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", }, @@ -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 } @@ -58,7 +58,7 @@ func TestCollection_Add(t *testing.T) { }{ { name: "adding nothing to empty collection", - collection: NewComponentCollection(), + collection: NewCollection(), args: args{ components: nil, }, @@ -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")}, }, @@ -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")}, }, @@ -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) } }) } diff --git a/fmesh.go b/fmesh.go index 398724e..7fecc08 100644 --- a/fmesh.go +++ b/fmesh.go @@ -32,7 +32,7 @@ type FMesh struct { func New(name string) *FMesh { return &FMesh{ name: name, - components: component.NewComponentCollection(), + components: component.NewCollection(), config: defaultConfig, } } @@ -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 }