diff --git a/component/component_test.go b/component/component_test.go index 1cc3554..54d0507 100644 --- a/component/component_test.go +++ b/component/component_test.go @@ -171,8 +171,8 @@ func TestComponent_Inputs(t *testing.T) { name: "with inputs", component: NewComponent("c1").WithInputs("i1", "i2"), want: port.Collection{ - port.NewPort("i1"), - port.NewPort("i2"), + "i1": port.NewPort("i1"), + "i2": port.NewPort("i2"), }, }, } @@ -200,8 +200,8 @@ func TestComponent_Outputs(t *testing.T) { name: "with outputs", component: NewComponent("c1").WithOutputs("o1", "o2"), want: port.Collection{ - port.NewPort("o1"), - port.NewPort("o2"), + "o1": port.NewPort("o1"), + "o2": port.NewPort("o2"), }, }, } @@ -306,8 +306,8 @@ func TestComponent_WithInputs(t *testing.T) { name: "c1", description: "", inputs: port.Collection{ - port.NewPort("p1"), - port.NewPort("p2"), + "p1": port.NewPort("p1"), + "p2": port.NewPort("p2"), }, outputs: port.Collection{}, f: nil, @@ -358,8 +358,8 @@ func TestComponent_WithOutputs(t *testing.T) { description: "", inputs: port.Collection{}, outputs: port.Collection{ - port.NewPort("p1"), - port.NewPort("p2"), + "p1": port.NewPort("p1"), + "p2": port.NewPort("p2"), }, f: nil, }, diff --git a/port/collection.go b/port/collection.go index 370af50..5f44ca9 100644 --- a/port/collection.go +++ b/port/collection.go @@ -5,31 +5,25 @@ import ( ) // Collection is a port collection with useful methods -type Collection []*Port +type Collection map[string]*Port // NewPortsCollection creates empty collection func NewPortsCollection() Collection { - return make(Collection, 0) + return make(Collection) } // ByName returns a port by its name func (collection Collection) ByName(name string) *Port { - for _, p := range collection { - if p.Name() == name { - return p - } - } - return nil + return collection[name] } // ByNames returns multiple ports by their names func (collection Collection) ByNames(names ...string) Collection { - selectedPorts := NewPortsCollection() + selectedPorts := make(Collection) for _, name := range names { - p := collection.ByName(name) - if p != nil { - selectedPorts = selectedPorts.Add(p) + if p, ok := collection[name]; ok { + selectedPorts[name] = p } } @@ -72,13 +66,12 @@ func (collection Collection) ClearSignal() { } } -// Add adds ports to collection func (collection Collection) Add(ports ...*Port) Collection { for _, port := range ports { if port == nil { continue } - collection = append(collection, port) + collection[port.Name()] = port } return collection diff --git a/port/collection_test.go b/port/collection_test.go index 7e92f68..7a09582 100644 --- a/port/collection_test.go +++ b/port/collection_test.go @@ -102,7 +102,7 @@ func TestCollection_ByName(t *testing.T) { args: args{ name: "p1", }, - want: &Port{name: "p1", pipes: Collection{}}, + want: &Port{name: "p1", pipes: Group{}}, }, { name: "port with signal found", @@ -113,7 +113,7 @@ func TestCollection_ByName(t *testing.T) { want: &Port{ name: "p2", signal: signal.New(12), - pipes: Collection{}, + pipes: Group{}, }, }, { @@ -151,9 +151,9 @@ func TestCollection_ByNames(t *testing.T) { names: []string{"p1"}, }, want: Collection{ - &Port{ + "p1": &Port{ name: "p1", - pipes: Collection{}, + pipes: Group{}, }, }, }, @@ -164,8 +164,8 @@ func TestCollection_ByNames(t *testing.T) { names: []string{"p1", "p2"}, }, want: Collection{ - &Port{name: "p1", pipes: Collection{}}, - &Port{name: "p2", pipes: Collection{}}, + "p1": &Port{name: "p1", pipes: Group{}}, + "p2": &Port{name: "p2", pipes: Group{}}, }, }, { @@ -183,8 +183,8 @@ func TestCollection_ByNames(t *testing.T) { names: []string{"p1", "p2", "p3"}, }, want: Collection{ - &Port{name: "p1", pipes: Collection{}}, - &Port{name: "p2", pipes: Collection{}}, + "p1": &Port{name: "p1", pipes: Group{}}, + "p2": &Port{name: "p2", pipes: Group{}}, }, }, } diff --git a/port/group.go b/port/group.go index 4e58643..f53dc93 100644 --- a/port/group.go +++ b/port/group.go @@ -11,3 +11,15 @@ func NewPortGroup(names ...string) Group { } return group } + +// Add adds ports to group +func (group Group) Add(ports ...*Port) Group { + for _, port := range ports { + if port == nil { + continue + } + group = append(group, port) + } + + return group +} diff --git a/port/port.go b/port/port.go index 5b647f4..6358083 100644 --- a/port/port.go +++ b/port/port.go @@ -8,14 +8,14 @@ import ( type Port struct { name string signal *signal.Signal //Current signal set on the port - pipes Collection //Refs to all outbound pipes connected to this port + pipes Group //Refs to all outbound pipes connected to this port } // NewPort creates a new port func NewPort(name string) *Port { return &Port{ name: name, - pipes: NewPortsCollection(), + pipes: NewPortGroup(), } } diff --git a/port/port_test.go b/port/port_test.go index f94c314..d926f7c 100644 --- a/port/port_test.go +++ b/port/port_test.go @@ -91,12 +91,12 @@ func TestPort_ClearSignal(t *testing.T) { { name: "happy path", before: portWithSignal, - after: &Port{name: "portWithSignal", pipes: Collection{}}, + after: &Port{name: "portWithSignal", pipes: Group{}}, }, { name: "cleaning empty port", before: NewPort("emptyPort"), - after: &Port{name: "emptyPort", pipes: Collection{}}, + after: &Port{name: "emptyPort", pipes: Group{}}, }, } for _, tt := range tests { @@ -124,7 +124,7 @@ func TestPort_PipeTo(t *testing.T) { before: p1, after: &Port{ name: "p1", - pipes: NewPortsCollection().Add(p2, p3), + pipes: Group{p2, p3}, }, args: args{ toPorts: []*Port{p2, p3}, @@ -135,7 +135,7 @@ func TestPort_PipeTo(t *testing.T) { before: p4, after: &Port{ name: "p4", - pipes: NewPortsCollection().Add(p2), + pipes: Group{p2}, }, args: args{ toPorts: []*Port{p2, nil}, @@ -175,7 +175,7 @@ func TestPort_PutSignal(t *testing.T) { after: &Port{ name: "emptyPort", signal: signal.New(11), - pipes: Collection{}, + pipes: Group{}, }, args: args{ sig: signal.New(11), @@ -187,7 +187,7 @@ func TestPort_PutSignal(t *testing.T) { after: &Port{ name: "p", signal: signal.New(11, 12), - pipes: Collection{}, + pipes: Group{}, }, args: args{ sig: signal.New(11, 12), @@ -199,7 +199,7 @@ func TestPort_PutSignal(t *testing.T) { after: &Port{ name: "portWithSingleSignal", signal: signal.New(12, 11), //Notice LIFO order - pipes: Collection{}, + pipes: Group{}, }, args: args{ sig: signal.New(12), @@ -211,7 +211,7 @@ func TestPort_PutSignal(t *testing.T) { after: &Port{ name: "portWithMultipleSignals", signal: signal.New(13, 11, 12), //Notice LIFO order - pipes: Collection{}, + pipes: Group{}, }, args: args{ sig: signal.New(13), @@ -223,7 +223,7 @@ func TestPort_PutSignal(t *testing.T) { after: &Port{ name: "portWithMultipleSignals2", signal: signal.New(13, 14, 55, 66), //Notice LIFO order - pipes: Collection{}, + pipes: Group{}, }, args: args{ sig: signal.New(13, 14), @@ -271,14 +271,14 @@ func TestNewPort(t *testing.T) { args: args{ name: "", }, - want: &Port{name: "", pipes: Collection{}}, + want: &Port{name: "", pipes: Group{}}, }, { name: "with name", args: args{ name: "p1", }, - want: &Port{name: "p1", pipes: Collection{}}, + want: &Port{name: "p1", pipes: Group{}}, }, } for _, tt := range tests {