Skip to content

Commit

Permalink
Bugfix: port collection fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Sep 17, 2024
1 parent 7a9fbda commit 2cdb18b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func (c *Component) WithDescription(description string) *Component {

// WithInputs ads input ports
func (c *Component) WithInputs(portNames ...string) *Component {
c.inputs.Add(port.NewPortGroup(portNames...)...)
c.inputs = c.inputs.Add(port.NewPortGroup(portNames...)...)
return c
}

// WithOutputs adds output ports
func (c *Component) WithOutputs(portNames ...string) *Component {
c.outputs.Add(port.NewPortGroup(portNames...)...)
c.outputs = c.outputs.Add(port.NewPortGroup(portNames...)...)
return c
}

Expand Down
16 changes: 8 additions & 8 deletions component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func TestComponent_Inputs(t *testing.T) {
name: "with inputs",
component: NewComponent("c1").WithInputs("i1", "i2"),
want: port.Collection{
"i1": port.NewPort("i1"),
"i2": port.NewPort("i2"),
port.NewPort("i1"),
port.NewPort("i2"),
},
},
}
Expand Down Expand Up @@ -200,8 +200,8 @@ func TestComponent_Outputs(t *testing.T) {
name: "with outputs",
component: NewComponent("c1").WithOutputs("o1", "o2"),
want: port.Collection{
"o1": port.NewPort("o1"),
"o2": port.NewPort("o2"),
port.NewPort("o1"),
port.NewPort("o2"),
},
},
}
Expand Down Expand Up @@ -306,8 +306,8 @@ func TestComponent_WithInputs(t *testing.T) {
name: "c1",
description: "",
inputs: port.Collection{
"p1": port.NewPort("p1"),
"p2": port.NewPort("p2"),
port.NewPort("p1"),
port.NewPort("p2"),
},
outputs: port.Collection{},
f: nil,
Expand Down Expand Up @@ -358,8 +358,8 @@ func TestComponent_WithOutputs(t *testing.T) {
description: "",
inputs: port.Collection{},
outputs: port.Collection{
"p1": port.NewPort("p1"),
"p2": port.NewPort("p2"),
port.NewPort("p1"),
port.NewPort("p2"),
},
f: nil,
},
Expand Down
21 changes: 14 additions & 7 deletions port/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ import (
)

// Collection is a port collection with useful methods
type Collection map[string]*Port
type Collection []*Port

// NewPortsCollection creates empty collection
func NewPortsCollection() Collection {
return make(Collection)
return make(Collection, 0)
}

// ByName returns a port by its name
func (collection Collection) ByName(name string) *Port {
return collection[name]
for _, p := range collection {
if p.Name() == name {
return p
}
}
return nil
}

// ByNames returns multiple ports by their names
func (collection Collection) ByNames(names ...string) Collection {
selectedPorts := make(Collection)
selectedPorts := NewPortsCollection()

for _, name := range names {
if p, ok := collection[name]; ok {
selectedPorts[name] = p
p := collection.ByName(name)
if p != nil {
selectedPorts = selectedPorts.Add(p)
}
}

Expand Down Expand Up @@ -66,12 +72,13 @@ 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[port.Name()] = port
collection = append(collection, port)
}

return collection
Expand Down
12 changes: 6 additions & 6 deletions port/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestCollection_ByNames(t *testing.T) {
names: []string{"p1"},
},
want: Collection{
"p1": &Port{
&Port{
name: "p1",
pipes: Collection{},
},
Expand All @@ -164,8 +164,8 @@ func TestCollection_ByNames(t *testing.T) {
names: []string{"p1", "p2"},
},
want: Collection{
"p1": &Port{name: "p1", pipes: Collection{}},
"p2": &Port{name: "p2", pipes: Collection{}},
&Port{name: "p1", pipes: Collection{}},
&Port{name: "p2", pipes: Collection{}},
},
},
{
Expand All @@ -183,8 +183,8 @@ func TestCollection_ByNames(t *testing.T) {
names: []string{"p1", "p2", "p3"},
},
want: Collection{
"p1": &Port{name: "p1", pipes: Collection{}},
"p2": &Port{name: "p2", pipes: Collection{}},
&Port{name: "p1", pipes: Collection{}},
&Port{name: "p2", pipes: Collection{}},
},
},
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestCollection_Add(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.collection.Add(tt.args.ports...)
tt.collection = tt.collection.Add(tt.args.ports...)
if tt.assertions != nil {
tt.assertions(t, tt.collection)
}
Expand Down
4 changes: 2 additions & 2 deletions port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *Port) PipeTo(toPorts ...*Port) {
if toPort == nil {
continue
}
p.pipes.Add(toPort)
p.pipes = p.pipes.Add(toPort)
}
}

Expand All @@ -61,7 +61,7 @@ func (p *Port) Flush() {
}

for _, outboundPort := range p.pipes {
//Multiplexing
//Fan-Out
ForwardSignal(p, outboundPort)
}
p.ClearSignal()
Expand Down
22 changes: 7 additions & 15 deletions port/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package port
import (
"github.com/hovsep/fmesh/signal"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

Expand Down Expand Up @@ -74,9 +73,8 @@ func TestPort_Signal(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.port.Signal(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Signal() = %v, want %v", got, tt.want)
}
got := tt.port.Signal()
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -104,9 +102,7 @@ func TestPort_ClearSignal(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.before.ClearSignal()
if !reflect.DeepEqual(tt.before, tt.after) {
t.Errorf("ClearSignal() = %v, want %v", tt.before, tt.after)
}
assert.Equal(t, tt.after, tt.before)
})
}
}
Expand Down Expand Up @@ -149,9 +145,7 @@ func TestPort_PipeTo(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.before.PipeTo(tt.args.toPorts...)
if !reflect.DeepEqual(tt.before, tt.after) {
t.Errorf("PipeTo() = %v, want %v", tt.before, tt.after)
}
assert.Equal(t, tt.after, tt.before)
})
}
}
Expand Down Expand Up @@ -239,9 +233,7 @@ func TestPort_PutSignal(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.before.PutSignal(tt.args.sig)
if !reflect.DeepEqual(tt.before, tt.after) {
t.Errorf("ClearSignal() = %v, want %v", tt.before, tt.after)
}
assert.Equal(t, tt.after, tt.before)
})
}
}
Expand All @@ -260,7 +252,7 @@ func TestPort_Name(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.port.Name(), "Name()")
assert.Equal(t, tt.want, tt.port.Name())
})
}
}
Expand Down Expand Up @@ -291,7 +283,7 @@ func TestNewPort(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, NewPort(tt.args.name), "NewPort(%v)", tt.args.name)
assert.Equal(t, tt.want, NewPort(tt.args.name))
})
}
}
Expand Down

0 comments on commit 2cdb18b

Please sign in to comment.