Skip to content

Commit

Permalink
Use port group as pipes container, rollback port collection
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Sep 17, 2024
1 parent 3c08fc3 commit 3917558
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 43 deletions.
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{
port.NewPort("i1"),
port.NewPort("i2"),
"i1": port.NewPort("i1"),
"i2": 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{
port.NewPort("o1"),
port.NewPort("o2"),
"o1": port.NewPort("o1"),
"o2": port.NewPort("o2"),
},
},
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down
21 changes: 7 additions & 14 deletions port/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions port/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -113,7 +113,7 @@ func TestCollection_ByName(t *testing.T) {
want: &Port{
name: "p2",
signal: signal.New(12),
pipes: Collection{},
pipes: Group{},
},
},
{
Expand Down Expand Up @@ -151,9 +151,9 @@ func TestCollection_ByNames(t *testing.T) {
names: []string{"p1"},
},
want: Collection{
&Port{
"p1": &Port{
name: "p1",
pipes: Collection{},
pipes: Group{},
},
},
},
Expand All @@ -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{}},
},
},
{
Expand All @@ -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{}},
},
},
}
Expand Down
12 changes: 12 additions & 0 deletions port/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
22 changes: 11 additions & 11 deletions port/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3917558

Please sign in to comment.