Skip to content

Commit

Permalink
refactor: remove config in one to many
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 6, 2023
1 parent c225ee2 commit 85a6bba
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 33 deletions.
8 changes: 1 addition & 7 deletions pkg/node/onetomany.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"github.com/siyul-park/uniflow/pkg/process"
)

// OneToManyNodeConfig holds the configuration for OneToManyNode.
type OneToManyNodeConfig struct {
Action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet)
}

// OneToManyNode represents a node that processes *packet.Packet with one input and many outputs.
type OneToManyNode struct {
action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet)
Expand All @@ -25,8 +20,7 @@ type OneToManyNode struct {
var _ Node = (*OneToManyNode)(nil)

// NewOneToManyNode creates a new OneToManyNode with the given configuration.
func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode {
action := config.Action
func NewOneToManyNode(action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet)) *OneToManyNode {
if action == nil {
action = func(_ *process.Process, _ *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return nil, nil
Expand Down
30 changes: 10 additions & 20 deletions pkg/node/onetomany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ import (
)

func TestNewOneToManyNode(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
})
assert.NotNil(t, n)

assert.NoError(t, n.Close())
}

func TestOneToManyNode_Port(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
})
defer func() { _ = n.Close() }()

Expand All @@ -47,10 +43,8 @@ func TestOneToManyNode_Port(t *testing.T) {

func TestOneToManyNode_Send(t *testing.T) {
t.Run("return out", func(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
})
defer func() { _ = n.Close() }()

Expand Down Expand Up @@ -92,10 +86,8 @@ func TestOneToManyNode_Send(t *testing.T) {
})

t.Run("return err", func(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return nil, packet.New(primitive.NewString(faker.Word()))
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return nil, packet.New(primitive.NewString(faker.Word()))
})
defer func() { _ = n.Close() }()

Expand Down Expand Up @@ -131,10 +123,8 @@ func TestOneToManyNode_Send(t *testing.T) {
}

func BenchmarkOneToManyNode_Send(b *testing.B) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
})
defer func() { _ = n.Close() }()

Expand Down
4 changes: 1 addition & 3 deletions pkg/plugin/controllx/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ var _ scheme.Spec = (*SwitchSpec)(nil)
// NewSwitchNode creates a new SwitchNode with the given configuration.
func NewSwitchNode(config SwitchNodeConfig) *SwitchNode {
n := &SwitchNode{}
n.OneToManyNode = node.NewOneToManyNode(node.OneToManyNodeConfig{
Action: n.action,
})
n.OneToManyNode = node.NewOneToManyNode(n.action)

return n
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/plugin/networkx/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ func NewRouterNode(config RouterNodeConfig) *RouterNode {
methods: map[string]string{},
},
}
n.OneToManyNode = node.NewOneToManyNode(node.OneToManyNodeConfig{
Action: n.action,
})
n.OneToManyNode = node.NewOneToManyNode(n.action)

return n
}
Expand Down

0 comments on commit 85a6bba

Please sign in to comment.