From c225ee2a23ac7c11b9c68cd168d1bed4920f02b8 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:07:45 -0500 Subject: [PATCH 1/8] refactor: remove id in node --- cmd/resource/builder_test.go | 2 +- cmd/uniflow/apply/cmd_test.go | 2 +- cmd/uniflow/start/cmd_test.go | 2 +- pkg/database/index.go | 6 +- pkg/loader/loader_test.go | 8 +- pkg/loader/reconciler_test.go | 2 +- pkg/node/node.go | 2 - pkg/node/onetomany.go | 17 --- pkg/node/onetomany_test.go | 1 - pkg/node/onetoone.go | 16 --- pkg/node/onetoone_test.go | 1 - pkg/plugin/controllx/builder.go | 5 +- pkg/plugin/controllx/snippet.go | 4 - pkg/plugin/controllx/snippet_test.go | 1 - pkg/plugin/controllx/switch.go | 5 - pkg/plugin/controllx/switch_test.go | 1 - pkg/plugin/networkx/builder.go | 5 +- pkg/plugin/networkx/http.go | 17 --- pkg/plugin/networkx/http_test.go | 1 - pkg/plugin/networkx/router.go | 5 - pkg/plugin/networkx/router_test.go | 1 - pkg/plugin/systemx/builder.go | 1 - pkg/plugin/systemx/reflect.go | 3 - pkg/plugin/systemx/reflect_test.go | 3 - pkg/runtime/runtime_test.go | 6 +- pkg/scheme/scheme_test.go | 2 +- pkg/storage/storage_test.go | 40 ++---- pkg/symbol/symbol.go | 4 +- pkg/symbol/symbol_test.go | 4 +- pkg/symbol/table_test.go | 197 ++++++++++++++------------- 30 files changed, 131 insertions(+), 233 deletions(-) diff --git a/cmd/resource/builder_test.go b/cmd/resource/builder_test.go index ff63deab..c4cc6f5d 100644 --- a/cmd/resource/builder_test.go +++ b/cmd/resource/builder_test.go @@ -26,7 +26,7 @@ func TestBuilder_Build(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/cmd/uniflow/apply/cmd_test.go b/cmd/uniflow/apply/cmd_test.go index 11249c77..a9e439b9 100644 --- a/cmd/uniflow/apply/cmd_test.go +++ b/cmd/uniflow/apply/cmd_test.go @@ -35,7 +35,7 @@ func TestExecute(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/cmd/uniflow/start/cmd_test.go b/cmd/uniflow/start/cmd_test.go index 8ac66b7f..5479ec72 100644 --- a/cmd/uniflow/start/cmd_test.go +++ b/cmd/uniflow/start/cmd_test.go @@ -39,7 +39,7 @@ func TestExecute(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/pkg/database/index.go b/pkg/database/index.go index ebe98515..1c1935e2 100644 --- a/pkg/database/index.go +++ b/pkg/database/index.go @@ -11,8 +11,8 @@ type IndexView interface { // IndexModel represents a model for an index. type IndexModel struct { - Name string - Keys []string - Unique bool + Name string + Keys []string + Unique bool Partial *Filter } diff --git a/pkg/loader/loader_test.go b/pkg/loader/loader_test.go index f0881a16..84c30c55 100644 --- a/pkg/loader/loader_test.go +++ b/pkg/loader/loader_test.go @@ -53,7 +53,7 @@ func TestLoader_LoadOne(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) @@ -99,7 +99,7 @@ func TestLoader_LoadOne(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) @@ -143,7 +143,7 @@ func TestLoader_LoadOne(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) @@ -204,7 +204,7 @@ func TestLoader_LoadAll(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/pkg/loader/reconciler_test.go b/pkg/loader/reconciler_test.go index 06e652ef..b23281ca 100644 --- a/pkg/loader/reconciler_test.go +++ b/pkg/loader/reconciler_test.go @@ -55,7 +55,7 @@ func TestReconciler_Reconcile(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/pkg/node/node.go b/pkg/node/node.go index cb2b0dbd..9ea105c9 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -1,13 +1,11 @@ package node import ( - "github.com/oklog/ulid/v2" "github.com/siyul-park/uniflow/pkg/port" ) // Node is an operational unit that processes *packet.Packet. type Node interface { - ID() ulid.ULID Port(name string) (*port.Port, bool) Close() error } diff --git a/pkg/node/onetomany.go b/pkg/node/onetomany.go index 88805473..10f86539 100644 --- a/pkg/node/onetomany.go +++ b/pkg/node/onetomany.go @@ -3,7 +3,6 @@ package node import ( "sync" - "github.com/oklog/ulid/v2" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" "github.com/siyul-park/uniflow/pkg/process" @@ -11,13 +10,11 @@ import ( // OneToManyNodeConfig holds the configuration for OneToManyNode. type OneToManyNodeConfig struct { - ID ulid.ULID 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 { - id ulid.ULID action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet) inPort *port.Port outPorts []*port.Port @@ -29,12 +26,7 @@ var _ Node = (*OneToManyNode)(nil) // NewOneToManyNode creates a new OneToManyNode with the given configuration. func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode { - id := config.ID action := config.Action - - if id == (ulid.ULID{}) { - id = ulid.Make() - } if action == nil { action = func(_ *process.Process, _ *packet.Packet) ([]*packet.Packet, *packet.Packet) { return nil, nil @@ -42,7 +34,6 @@ func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode { } n := &OneToManyNode{ - id: id, action: action, inPort: port.New(), outPorts: nil, @@ -62,14 +53,6 @@ func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode { return n } -// ID returns the ID of the OneToManyNode. -func (n *OneToManyNode) ID() ulid.ULID { - n.mu.RLock() - defer n.mu.RUnlock() - - return n.id -} - // Port returns the specified port of the OneToManyNode. func (n *OneToManyNode) Port(name string) (*port.Port, bool) { n.mu.Lock() diff --git a/pkg/node/onetomany_test.go b/pkg/node/onetomany_test.go index b2f387e9..572fe353 100644 --- a/pkg/node/onetomany_test.go +++ b/pkg/node/onetomany_test.go @@ -20,7 +20,6 @@ func TestNewOneToManyNode(t *testing.T) { }, }) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) assert.NoError(t, n.Close()) } diff --git a/pkg/node/onetoone.go b/pkg/node/onetoone.go index 6678874a..71cc6e30 100644 --- a/pkg/node/onetoone.go +++ b/pkg/node/onetoone.go @@ -3,7 +3,6 @@ package node import ( "sync" - "github.com/oklog/ulid/v2" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" "github.com/siyul-park/uniflow/pkg/process" @@ -11,13 +10,11 @@ import ( // OneToOneNodeConfig holds the configuration for OneToOneNode. type OneToOneNodeConfig struct { - ID ulid.ULID Action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet) } // OneToOneNode represents a node that processes *packet.Packet with one input and one output. type OneToOneNode struct { - id ulid.ULID action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet) ioPort *port.Port inPort *port.Port @@ -30,12 +27,8 @@ var _ Node = (*OneToOneNode)(nil) // NewOneToOneNode creates a new OneToOneNode with the given configuration. func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode { - id := config.ID action := config.Action - if id == (ulid.ULID{}) { - id = ulid.Make() - } if action == nil { action = func(_ *process.Process, _ *packet.Packet) (*packet.Packet, *packet.Packet) { return nil, nil @@ -43,7 +36,6 @@ func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode { } n := &OneToOneNode{ - id: id, action: action, ioPort: port.New(), inPort: port.New(), @@ -88,14 +80,6 @@ func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode { return n } -// ID returns the ID of the OneToOneNode. -func (n *OneToOneNode) ID() ulid.ULID { - n.mu.RLock() - defer n.mu.RUnlock() - - return n.id -} - // Port returns the specified port of the OneToOneNode. func (n *OneToOneNode) Port(name string) (*port.Port, bool) { n.mu.RLock() diff --git a/pkg/node/onetoone_test.go b/pkg/node/onetoone_test.go index 52440357..7d1bfa54 100644 --- a/pkg/node/onetoone_test.go +++ b/pkg/node/onetoone_test.go @@ -20,7 +20,6 @@ func TestNewOneToOneNode(t *testing.T) { }, }) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) assert.NoError(t, n.Close()) } diff --git a/pkg/plugin/controllx/builder.go b/pkg/plugin/controllx/builder.go index b0d61917..18738c10 100644 --- a/pkg/plugin/controllx/builder.go +++ b/pkg/plugin/controllx/builder.go @@ -11,7 +11,6 @@ func AddToScheme() func(*scheme.Scheme) error { s.AddKnownType(KindSnippet, &SnippetSpec{}) s.AddCodec(KindSnippet, scheme.CodecWithType[*SnippetSpec](func(spec *SnippetSpec) (node.Node, error) { return NewSnippetNode(SnippetNodeConfig{ - ID: spec.ID, Lang: spec.Lang, Code: spec.Code, }) @@ -19,9 +18,7 @@ func AddToScheme() func(*scheme.Scheme) error { s.AddKnownType(KindSwitch, &SwitchSpec{}) s.AddCodec(KindSwitch, scheme.CodecWithType[*SwitchSpec](func(spec *SwitchSpec) (node.Node, error) { - n := NewSwitchNode(SwitchNodeConfig{ - ID: spec.ID, - }) + n := NewSwitchNode(SwitchNodeConfig{}) for _, v := range spec.Match { if err := n.Add(v.When, v.Port); err != nil { _ = n.Close() diff --git a/pkg/plugin/controllx/snippet.go b/pkg/plugin/controllx/snippet.go index 3476d9ea..ca59f508 100644 --- a/pkg/plugin/controllx/snippet.go +++ b/pkg/plugin/controllx/snippet.go @@ -9,7 +9,6 @@ import ( "github.com/dop251/goja" "github.com/evanw/esbuild/pkg/api" "github.com/iancoleman/strcase" - "github.com/oklog/ulid/v2" "github.com/pkg/errors" "github.com/siyul-park/uniflow/pkg/node" "github.com/siyul-park/uniflow/pkg/packet" @@ -21,7 +20,6 @@ import ( // SnippetNodeConfig holds the configuration for creating a SnippetNode. type SnippetNodeConfig struct { - ID ulid.ULID Lang string Code string } @@ -63,7 +61,6 @@ var _ scheme.Spec = (*SnippetSpec)(nil) func NewSnippetNode(config SnippetNodeConfig) (*SnippetNode, error) { defer func() { _ = recover() }() - id := config.ID lang := config.Lang code := config.Code @@ -76,7 +73,6 @@ func NewSnippetNode(config SnippetNodeConfig) (*SnippetNode, error) { run: run, } n.OneToOneNode = node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: id, Action: n.action, }) diff --git a/pkg/plugin/controllx/snippet_test.go b/pkg/plugin/controllx/snippet_test.go index c6f1a5de..3ace18ab 100644 --- a/pkg/plugin/controllx/snippet_test.go +++ b/pkg/plugin/controllx/snippet_test.go @@ -22,7 +22,6 @@ func TestNewSnippetNode(t *testing.T) { }) assert.NoError(t, err) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) _ = n.Close() } diff --git a/pkg/plugin/controllx/switch.go b/pkg/plugin/controllx/switch.go index bfb90118..0a8fc647 100644 --- a/pkg/plugin/controllx/switch.go +++ b/pkg/plugin/controllx/switch.go @@ -4,7 +4,6 @@ import ( "reflect" "sync" - "github.com/oklog/ulid/v2" "github.com/siyul-park/uniflow/pkg/node" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" @@ -15,7 +14,6 @@ import ( // SwitchNodeConfig holds the configuration for creating a SwitchNode. type SwitchNodeConfig struct { - ID ulid.ULID } // SwitchNode represents a node that switches packets based on conditions. @@ -50,11 +48,8 @@ var _ scheme.Spec = (*SwitchSpec)(nil) // NewSwitchNode creates a new SwitchNode with the given configuration. func NewSwitchNode(config SwitchNodeConfig) *SwitchNode { - id := config.ID - n := &SwitchNode{} n.OneToManyNode = node.NewOneToManyNode(node.OneToManyNodeConfig{ - ID: id, Action: n.action, }) diff --git a/pkg/plugin/controllx/switch_test.go b/pkg/plugin/controllx/switch_test.go index b0630379..7f065780 100644 --- a/pkg/plugin/controllx/switch_test.go +++ b/pkg/plugin/controllx/switch_test.go @@ -17,7 +17,6 @@ import ( func TestNewSwitchNode(t *testing.T) { n := NewSwitchNode(SwitchNodeConfig{}) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) _ = n.Close() } diff --git a/pkg/plugin/networkx/builder.go b/pkg/plugin/networkx/builder.go index 0143f948..fe7597ff 100644 --- a/pkg/plugin/networkx/builder.go +++ b/pkg/plugin/networkx/builder.go @@ -48,16 +48,13 @@ func AddToScheme() func(*scheme.Scheme) error { s.AddKnownType(KindHTTP, &HTTPSpec{}) s.AddCodec(KindHTTP, scheme.CodecWithType[*HTTPSpec](func(spec *HTTPSpec) (node.Node, error) { return NewHTTPNode(HTTPNodeConfig{ - ID: spec.ID, Address: spec.Address, }), nil })) s.AddKnownType(KindRouter, &RouterSpec{}) s.AddCodec(KindRouter, scheme.CodecWithType[*RouterSpec](func(spec *RouterSpec) (node.Node, error) { - n := NewRouterNode(RouterNodeConfig{ - ID: spec.ID, - }) + n := NewRouterNode(RouterNodeConfig{}) for _, r := range spec.Routes { n.Add(r.Method, r.Path, r.Port) diff --git a/pkg/plugin/networkx/http.go b/pkg/plugin/networkx/http.go index 32b2a620..09fe677f 100644 --- a/pkg/plugin/networkx/http.go +++ b/pkg/plugin/networkx/http.go @@ -14,7 +14,6 @@ import ( "sync" "time" - "github.com/oklog/ulid/v2" "github.com/siyul-park/uniflow/pkg/node" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" @@ -25,13 +24,11 @@ import ( // HTTPNodeConfig represents the configuration of an HTTP node. type HTTPNodeConfig struct { - ID ulid.ULID Address string } // HTTPNode represents a node based on the HTTP protocol. type HTTPNode struct { - id ulid.ULID address string server *http.Server listener net.Listener @@ -252,15 +249,9 @@ func init() { // NewHTTPNode creates a new HTTPNode with the given configuration. func NewHTTPNode(config HTTPNodeConfig) *HTTPNode { - id := config.ID address := config.Address - if id == (ulid.ULID{}) { - id = ulid.Make() - } - n := &HTTPNode{ - id: id, address: address, server: new(http.Server), listenerNetwork: "tcp", @@ -274,14 +265,6 @@ func NewHTTPNode(config HTTPNodeConfig) *HTTPNode { return n } -// ID returns the ID of the HTTPNode. -func (n *HTTPNode) ID() ulid.ULID { - n.mu.RLock() - defer n.mu.RUnlock() - - return n.id -} - // Port returns the port with the given name and a boolean indicating success. func (n *HTTPNode) Port(name string) (*port.Port, bool) { n.mu.RLock() diff --git a/pkg/plugin/networkx/http_test.go b/pkg/plugin/networkx/http_test.go index 4804f2aa..2ea92fd4 100644 --- a/pkg/plugin/networkx/http_test.go +++ b/pkg/plugin/networkx/http_test.go @@ -25,7 +25,6 @@ func TestNewHTTPNode(t *testing.T) { Address: fmt.Sprintf(":%d", port), }) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) assert.NoError(t, n.Close()) } diff --git a/pkg/plugin/networkx/router.go b/pkg/plugin/networkx/router.go index 33848c58..309f75f8 100644 --- a/pkg/plugin/networkx/router.go +++ b/pkg/plugin/networkx/router.go @@ -5,7 +5,6 @@ import ( "net/http" "sync" - "github.com/oklog/ulid/v2" "github.com/siyul-park/uniflow/pkg/node" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" @@ -16,7 +15,6 @@ import ( // RouterNodeConfig holds the configuration for RouterNode. type RouterNodeConfig struct { - ID ulid.ULID } // RouterNode represents a router node that handles routing based on HTTP methods, paths, and ports. @@ -75,15 +73,12 @@ var _ scheme.Spec = (*RouterSpec)(nil) // NewRouterNode creates a new instance of RouterNode with the given configuration. func NewRouterNode(config RouterNodeConfig) *RouterNode { - id := config.ID - n := &RouterNode{ tree: &route{ methods: map[string]string{}, }, } n.OneToManyNode = node.NewOneToManyNode(node.OneToManyNodeConfig{ - ID: id, Action: n.action, }) diff --git a/pkg/plugin/networkx/router_test.go b/pkg/plugin/networkx/router_test.go index a44c3265..2d02703a 100644 --- a/pkg/plugin/networkx/router_test.go +++ b/pkg/plugin/networkx/router_test.go @@ -17,7 +17,6 @@ import ( func TestNewRouterNode(t *testing.T) { n := NewRouterNode(RouterNodeConfig{}) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) _ = n.Close() } diff --git a/pkg/plugin/systemx/builder.go b/pkg/plugin/systemx/builder.go index 7afca868..c9b70b9b 100644 --- a/pkg/plugin/systemx/builder.go +++ b/pkg/plugin/systemx/builder.go @@ -12,7 +12,6 @@ func AddToScheme(storage *storage.Storage) func(*scheme.Scheme) error { s.AddKnownType(KindReflect, &ReflectSpec{}) s.AddCodec(KindReflect, scheme.CodecWithType[*ReflectSpec](func(spec *ReflectSpec) (node.Node, error) { return NewReflectNode(ReflectNodeConfig{ - ID: spec.ID, OP: spec.OP, Storage: storage, }), nil diff --git a/pkg/plugin/systemx/reflect.go b/pkg/plugin/systemx/reflect.go index e600db47..10c9dbd3 100644 --- a/pkg/plugin/systemx/reflect.go +++ b/pkg/plugin/systemx/reflect.go @@ -16,7 +16,6 @@ import ( // ReflectNodeConfig holds the configuration for ReflectNode. type ReflectNodeConfig struct { - ID ulid.ULID // ID is the unique identifier for ReflectNode. OP string // OP is the operation to be performed by ReflectNode. Storage *storage.Storage // Storage is the storage instance associated with ReflectNode. } @@ -50,7 +49,6 @@ var _ scheme.Spec = (*ReflectSpec)(nil) // NewReflectNode creates a new instance of ReflectNode with the given configuration. func NewReflectNode(config ReflectNodeConfig) *ReflectNode { - id := config.ID op := config.OP storage := config.Storage @@ -59,7 +57,6 @@ func NewReflectNode(config ReflectNodeConfig) *ReflectNode { storage: storage, } n.OneToOneNode = node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: id, Action: n.action, }) diff --git a/pkg/plugin/systemx/reflect_test.go b/pkg/plugin/systemx/reflect_test.go index 657e664e..6051ced7 100644 --- a/pkg/plugin/systemx/reflect_test.go +++ b/pkg/plugin/systemx/reflect_test.go @@ -30,7 +30,6 @@ func TestNewReflectNode(t *testing.T) { Storage: st, }) assert.NotNil(t, n) - assert.NotZero(t, n.ID()) _ = n.Close() } @@ -46,7 +45,6 @@ func TestReflectNode_Send(t *testing.T) { s.AddKnownType(KindReflect, &ReflectSpec{}) s.AddCodec(KindReflect, scheme.CodecWithType[*ReflectSpec](func(spec *ReflectSpec) (node.Node, error) { return NewReflectNode(ReflectNodeConfig{ - ID: spec.ID, OP: spec.OP, Storage: st, }), nil @@ -235,7 +233,6 @@ func BenchmarkReflectNode_Send(b *testing.B) { s.AddKnownType(KindReflect, &ReflectSpec{}) s.AddCodec(KindReflect, scheme.CodecWithType[*ReflectSpec](func(spec *ReflectSpec) (node.Node, error) { return NewReflectNode(ReflectNodeConfig{ - ID: spec.ID, OP: spec.OP, Storage: st, }), nil diff --git a/pkg/runtime/runtime_test.go b/pkg/runtime/runtime_test.go index 20050f45..c21f0a2d 100644 --- a/pkg/runtime/runtime_test.go +++ b/pkg/runtime/runtime_test.go @@ -19,7 +19,7 @@ func TestRuntime_Lookup(t *testing.T) { sb := scheme.NewBuilder(func(s *scheme.Scheme) error { s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) return nil }) @@ -56,7 +56,7 @@ func TestRuntime_Free(t *testing.T) { sb := scheme.NewBuilder(func(s *scheme.Scheme) error { s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) return nil }) @@ -94,7 +94,7 @@ func TestRuntime_Start(t *testing.T) { sb := scheme.NewBuilder(func(s *scheme.Scheme) error { s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) return nil }) diff --git a/pkg/scheme/scheme_test.go b/pkg/scheme/scheme_test.go index f956e555..c20f4762 100644 --- a/pkg/scheme/scheme_test.go +++ b/pkg/scheme/scheme_test.go @@ -25,7 +25,7 @@ func TestScheme_Codec(t *testing.T) { kind := faker.Word() c := CodecFunc(func(spec Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil }) s.AddCodec(kind, c) diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index 19d19373..23befe37 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -18,9 +18,7 @@ func TestStorage_Watch(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -60,9 +58,7 @@ func TestStorage_InsertOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -86,9 +82,7 @@ func TestStorage_InsertMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -123,9 +117,7 @@ func TestStorage_UpdateOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -155,9 +147,7 @@ func TestStorage_UpdateMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -195,9 +185,7 @@ func TestStorage_DeleteOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -228,9 +216,7 @@ func TestStorage_DeleteMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -262,9 +248,7 @@ func TestStorage_FindOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -292,9 +276,7 @@ func TestStorage_FindOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ @@ -324,9 +306,7 @@ func TestStorage_FindMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{ - ID: spec.GetID(), - }), nil + return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil })) st, _ := New(context.Background(), Config{ diff --git a/pkg/symbol/symbol.go b/pkg/symbol/symbol.go index efb725f9..9ea429ea 100644 --- a/pkg/symbol/symbol.go +++ b/pkg/symbol/symbol.go @@ -15,9 +15,9 @@ type Symbol struct { var _ node.Node = (*Symbol)(nil) -// ID returns the unique identifier of the Symbol, based on its Node. +// ID returns the unique identifier of the Symbol, based on its Spec. func (s *Symbol) ID() ulid.ULID { - return s.Node.ID() + return s.Spec.GetID() } // Kind returns the kind of the Symbol, based on its Spec. diff --git a/pkg/symbol/symbol_test.go b/pkg/symbol/symbol_test.go index 075871e9..132c1368 100644 --- a/pkg/symbol/symbol_test.go +++ b/pkg/symbol/symbol_test.go @@ -14,7 +14,7 @@ func TestSymbol_Getter(t *testing.T) { n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n.Close() spec := &scheme.SpecMeta{ - ID: n.ID(), + ID: ulid.Make(), Kind: faker.Word(), Namespace: scheme.DefaultNamespace, Name: faker.UUIDHyphenated(), @@ -30,7 +30,7 @@ func TestSymbol_Getter(t *testing.T) { sym := &Symbol{Node: n, Spec: spec} - assert.Equal(t, n.ID(), sym.ID()) + assert.Equal(t, spec.GetID(), sym.ID()) assert.Equal(t, spec.GetKind(), sym.Kind()) assert.Equal(t, spec.GetNamespace(), sym.Namespace()) assert.Equal(t, spec.GetName(), sym.Name()) diff --git a/pkg/symbol/table_test.go b/pkg/symbol/table_test.go index e0bf1b19..ca490481 100644 --- a/pkg/symbol/table_test.go +++ b/pkg/symbol/table_test.go @@ -24,38 +24,39 @@ func TestTable_Insert(t *testing.T) { defer n3.Close() spec1 := &scheme.SpecMeta{ - ID: n1.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n2.ID(), - Port: node.PortIn, - }, - }, - }, } spec2 := &scheme.SpecMeta{ - ID: n2.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n3.ID(), - Port: node.PortIn, - }, - }, - }, } spec3 := &scheme.SpecMeta{ - ID: n3.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n1.ID(), - Port: node.PortIn, - }, + } + + spec1.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec2.GetID(), + Port: node.PortIn, + }, + }, + } + spec2.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec3.GetID(), + Port: node.PortIn, + }, + }, + } + spec3.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec1.GetID(), + Port: node.PortIn, }, }, } @@ -92,9 +93,9 @@ func TestTable_Insert(t *testing.T) { id := ulid.Make() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{ID: id}) + n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{ID: id}) + n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n2.Close() n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n3.Close() @@ -104,48 +105,49 @@ func TestTable_Insert(t *testing.T) { spec1 := &scheme.SpecMeta{ ID: id, Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n3.ID(), - Port: node.PortIn, - }, - }, - }, } spec2 := &scheme.SpecMeta{ ID: id, Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n4.ID(), - Port: node.PortIn, - }, - }, - }, } spec3 := &scheme.SpecMeta{ - ID: n3.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: id, - Port: node.PortIn, - }, - }, - }, } spec4 := &scheme.SpecMeta{ - ID: n4.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: id, - Port: node.PortIn, - }, + } + + spec1.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec3.GetID(), + Port: node.PortIn, + }, + }, + } + spec2.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec4.GetID(), + Port: node.PortIn, + }, + }, + } + spec3.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: id, + Port: node.PortIn, + }, + }, + } + spec4.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: id, + Port: node.PortIn, }, }, } @@ -187,17 +189,17 @@ func TestTable_Insert(t *testing.T) { defer n3.Close() spec1 := &scheme.SpecMeta{ - ID: n1.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, Name: faker.UUIDHyphenated(), } spec2 := &scheme.SpecMeta{ - ID: n2.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, Name: faker.UUIDHyphenated(), } spec3 := &scheme.SpecMeta{ - ID: n3.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, Name: faker.UUIDHyphenated(), } @@ -260,9 +262,9 @@ func TestTable_Insert(t *testing.T) { id := ulid.Make() name := faker.UUIDHyphenated() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{ID: id}) + n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{ID: id}) + n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n2.Close() n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n3.Close() @@ -280,12 +282,12 @@ func TestTable_Insert(t *testing.T) { Name: name, } spec3 := &scheme.SpecMeta{ - ID: n3.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, Name: faker.UUIDHyphenated(), } spec4 := &scheme.SpecMeta{ - ID: n4.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, Name: faker.UUIDHyphenated(), } @@ -360,38 +362,39 @@ func TestTable_Free(t *testing.T) { defer n3.Close() spec1 := &scheme.SpecMeta{ - ID: n1.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n2.ID(), - Port: node.PortIn, - }, - }, - }, } spec2 := &scheme.SpecMeta{ - ID: n2.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n3.ID(), - Port: node.PortIn, - }, - }, - }, } spec3 := &scheme.SpecMeta{ - ID: n3.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, - Links: map[string][]scheme.PortLocation{ - node.PortOut: { - { - ID: n1.ID(), - Port: node.PortIn, - }, + } + + spec1.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec2.GetID(), + Port: node.PortIn, + }, + }, + } + spec2.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec3.GetID(), + Port: node.PortIn, + }, + }, + } + spec3.Links = map[string][]scheme.PortLocation{ + node.PortOut: { + { + ID: spec1.GetID(), + Port: node.PortIn, }, }, } @@ -404,20 +407,20 @@ func TestTable_Free(t *testing.T) { _ = tb.Insert(&Symbol{Node: n2, Spec: spec2}) _ = tb.Insert(&Symbol{Node: n3, Spec: spec3}) - ok, err := tb.Free(n1.ID()) + ok, err := tb.Free(spec1.GetID()) assert.NoError(t, err) assert.True(t, ok) assert.Equal(t, 0, p1.Links()) - ok, err = tb.Free(n2.ID()) + ok, err = tb.Free(spec2.GetID()) assert.NoError(t, err) assert.True(t, ok) assert.Equal(t, 0, p1.Links()) assert.Equal(t, 0, p2.Links()) - ok, err = tb.Free(n3.ID()) + ok, err = tb.Free(spec3.GetID()) assert.NoError(t, err) assert.True(t, ok) @@ -433,13 +436,13 @@ func TestTable_LookupByID(t *testing.T) { n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n.Close() spec := &scheme.SpecMeta{ - ID: n.ID(), + ID: ulid.Make(), } sym := &Symbol{Node: n, Spec: spec} _ = tb.Insert(sym) - r, ok := tb.LookupByID(n.ID()) + r, ok := tb.LookupByID(spec.GetID()) assert.True(t, ok) assert.Equal(t, sym, r) } @@ -451,7 +454,7 @@ func TestTable_LookupByName(t *testing.T) { n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) defer n.Close() spec := &scheme.SpecMeta{ - ID: n.ID(), + ID: ulid.Make(), Namespace: scheme.DefaultNamespace, Name: faker.Word(), } From 85a6bba3e94650550c7e82166621fedc7f8e799d Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:09:40 -0500 Subject: [PATCH 2/8] refactor: remove config in one to many --- pkg/node/onetomany.go | 8 +------- pkg/node/onetomany_test.go | 30 ++++++++++-------------------- pkg/plugin/controllx/switch.go | 4 +--- pkg/plugin/networkx/router.go | 4 +--- 4 files changed, 13 insertions(+), 33 deletions(-) diff --git a/pkg/node/onetomany.go b/pkg/node/onetomany.go index 10f86539..c63b97fc 100644 --- a/pkg/node/onetomany.go +++ b/pkg/node/onetomany.go @@ -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) @@ -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 diff --git a/pkg/node/onetomany_test.go b/pkg/node/onetomany_test.go index 572fe353..d030e9a2 100644 --- a/pkg/node/onetomany_test.go +++ b/pkg/node/onetomany_test.go @@ -14,10 +14,8 @@ 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) @@ -25,10 +23,8 @@ func TestNewOneToManyNode(t *testing.T) { } 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() }() @@ -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() }() @@ -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() }() @@ -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() }() diff --git a/pkg/plugin/controllx/switch.go b/pkg/plugin/controllx/switch.go index 0a8fc647..1643ac44 100644 --- a/pkg/plugin/controllx/switch.go +++ b/pkg/plugin/controllx/switch.go @@ -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 } diff --git a/pkg/plugin/networkx/router.go b/pkg/plugin/networkx/router.go index 309f75f8..0d205db5 100644 --- a/pkg/plugin/networkx/router.go +++ b/pkg/plugin/networkx/router.go @@ -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 } From ef5f0373baf61958e30464dd091eca49917d4cab Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:12:08 -0500 Subject: [PATCH 3/8] refactor: remove one to one config --- cmd/resource/builder_test.go | 2 +- cmd/uniflow/apply/cmd_test.go | 2 +- cmd/uniflow/start/cmd_test.go | 2 +- pkg/hook/hook_test.go | 4 +-- pkg/loader/loader_test.go | 8 +++--- pkg/loader/reconciler_test.go | 2 +- pkg/node/onetomany_test.go | 8 ++---- pkg/node/onetoone.go | 9 +------ pkg/node/onetoone_test.go | 48 ++++++++++----------------------- pkg/plugin/controllx/snippet.go | 4 +-- pkg/plugin/systemx/reflect.go | 4 +-- pkg/runtime/runtime_test.go | 6 ++--- pkg/scheme/scheme_test.go | 4 +-- pkg/storage/storage_test.go | 20 +++++++------- pkg/symbol/symbol_test.go | 2 +- pkg/symbol/table_test.go | 38 +++++++++++++------------- 16 files changed, 64 insertions(+), 99 deletions(-) diff --git a/cmd/resource/builder_test.go b/cmd/resource/builder_test.go index c4cc6f5d..36a482e1 100644 --- a/cmd/resource/builder_test.go +++ b/cmd/resource/builder_test.go @@ -26,7 +26,7 @@ func TestBuilder_Build(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/cmd/uniflow/apply/cmd_test.go b/cmd/uniflow/apply/cmd_test.go index a9e439b9..1743e566 100644 --- a/cmd/uniflow/apply/cmd_test.go +++ b/cmd/uniflow/apply/cmd_test.go @@ -35,7 +35,7 @@ func TestExecute(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/cmd/uniflow/start/cmd_test.go b/cmd/uniflow/start/cmd_test.go index 5479ec72..68bc9c9f 100644 --- a/cmd/uniflow/start/cmd_test.go +++ b/cmd/uniflow/start/cmd_test.go @@ -39,7 +39,7 @@ func TestExecute(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/pkg/hook/hook_test.go b/pkg/hook/hook_test.go index e3663ad2..b444db39 100644 --- a/pkg/hook/hook_test.go +++ b/pkg/hook/hook_test.go @@ -11,7 +11,7 @@ import ( func TestHook_LoadHook(t *testing.T) { hooks := New() - n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n := node.NewOneToOneNode(nil) count := 0 h := symbol.LoadHookFunc(func(_ node.Node) error { @@ -29,7 +29,7 @@ func TestHook_LoadHook(t *testing.T) { func TestHook_UnloadHook(t *testing.T) { hooks := New() - n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n := node.NewOneToOneNode(nil) count := 0 h := symbol.UnloadHookFunc(func(_ node.Node) error { diff --git a/pkg/loader/loader_test.go b/pkg/loader/loader_test.go index 84c30c55..bb638c2a 100644 --- a/pkg/loader/loader_test.go +++ b/pkg/loader/loader_test.go @@ -53,7 +53,7 @@ func TestLoader_LoadOne(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) @@ -99,7 +99,7 @@ func TestLoader_LoadOne(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) @@ -143,7 +143,7 @@ func TestLoader_LoadOne(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) @@ -204,7 +204,7 @@ func TestLoader_LoadAll(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/pkg/loader/reconciler_test.go b/pkg/loader/reconciler_test.go index b23281ca..abaf7392 100644 --- a/pkg/loader/reconciler_test.go +++ b/pkg/loader/reconciler_test.go @@ -55,7 +55,7 @@ func TestReconciler_Reconcile(t *testing.T) { } codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddKnownType(kind, &scheme.SpecMeta{}) diff --git a/pkg/node/onetomany_test.go b/pkg/node/onetomany_test.go index d030e9a2..0f37cff5 100644 --- a/pkg/node/onetomany_test.go +++ b/pkg/node/onetomany_test.go @@ -14,18 +14,14 @@ import ( ) func TestNewOneToManyNode(t *testing.T) { - n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) { - return []*packet.Packet{inPck}, nil - }) + n := NewOneToManyNode(nil) assert.NotNil(t, n) assert.NoError(t, n.Close()) } func TestOneToManyNode_Port(t *testing.T) { - n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) { - return []*packet.Packet{inPck}, nil - }) + n := NewOneToManyNode(nil) defer func() { _ = n.Close() }() p, ok := n.Port(PortIn) diff --git a/pkg/node/onetoone.go b/pkg/node/onetoone.go index 71cc6e30..1e160cc1 100644 --- a/pkg/node/onetoone.go +++ b/pkg/node/onetoone.go @@ -8,11 +8,6 @@ import ( "github.com/siyul-park/uniflow/pkg/process" ) -// OneToOneNodeConfig holds the configuration for OneToOneNode. -type OneToOneNodeConfig struct { - Action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet) -} - // OneToOneNode represents a node that processes *packet.Packet with one input and one output. type OneToOneNode struct { action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet) @@ -26,9 +21,7 @@ type OneToOneNode struct { var _ Node = (*OneToOneNode)(nil) // NewOneToOneNode creates a new OneToOneNode with the given configuration. -func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode { - action := config.Action - +func NewOneToOneNode(action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet)) *OneToOneNode { if action == nil { action = func(_ *process.Process, _ *packet.Packet) (*packet.Packet, *packet.Packet) { return nil, nil diff --git a/pkg/node/onetoone_test.go b/pkg/node/onetoone_test.go index 7d1bfa54..31d9212a 100644 --- a/pkg/node/onetoone_test.go +++ b/pkg/node/onetoone_test.go @@ -14,22 +14,14 @@ import ( ) func TestNewOneToOneNode(t *testing.T) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return inPck, nil - }, - }) + n := NewOneToOneNode(nil) assert.NotNil(t, n) assert.NoError(t, n.Close()) } func TestOneToOneNode_Port(t *testing.T) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return inPck, nil - }, - }) + n := NewOneToOneNode(nil) defer func() { _ = n.Close() }() p, ok := n.Port(PortIO) @@ -52,10 +44,8 @@ func TestOneToOneNode_Port(t *testing.T) { func TestOneToOneNode_Send(t *testing.T) { t.Run("IO", func(t *testing.T) { t.Run("return out", func(t *testing.T) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return inPck, nil - }, + n := NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { + return inPck, nil }) defer func() { _ = n.Close() }() @@ -85,10 +75,8 @@ func TestOneToOneNode_Send(t *testing.T) { }) t.Run("return err", func(t *testing.T) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return nil, packet.New(primitive.NewString(faker.Word())) - }, + n := NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { + return nil, packet.New(primitive.NewString(faker.Word())) }) defer func() { _ = n.Close() }() @@ -125,10 +113,8 @@ func TestOneToOneNode_Send(t *testing.T) { t.Run("In/Out", func(t *testing.T) { t.Run("return out", func(t *testing.T) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return inPck, nil - }, + n := NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { + return inPck, nil }) defer func() { _ = n.Close() }() @@ -171,10 +157,8 @@ func TestOneToOneNode_Send(t *testing.T) { }) t.Run("return err", func(t *testing.T) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return nil, packet.New(primitive.NewString(faker.Word())) - }, + n := NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { + return nil, packet.New(primitive.NewString(faker.Word())) }) defer func() { _ = n.Close() }() @@ -212,10 +196,8 @@ func TestOneToOneNode_Send(t *testing.T) { func BenchmarkOneToOneNode_Send(b *testing.B) { b.Run("IO", func(b *testing.B) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return inPck, nil - }, + n := NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { + return inPck, nil }) defer func() { _ = n.Close() }() @@ -240,10 +222,8 @@ func BenchmarkOneToOneNode_Send(b *testing.B) { }) b.Run("In/Out", func(b *testing.B) { - n := NewOneToOneNode(OneToOneNodeConfig{ - Action: func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { - return inPck, nil - }, + n := NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { + return inPck, nil }) defer func() { _ = n.Close() }() diff --git a/pkg/plugin/controllx/snippet.go b/pkg/plugin/controllx/snippet.go index ca59f508..452473c5 100644 --- a/pkg/plugin/controllx/snippet.go +++ b/pkg/plugin/controllx/snippet.go @@ -72,9 +72,7 @@ func NewSnippetNode(config SnippetNodeConfig) (*SnippetNode, error) { n := &SnippetNode{ run: run, } - n.OneToOneNode = node.NewOneToOneNode(node.OneToOneNodeConfig{ - Action: n.action, - }) + n.OneToOneNode = node.NewOneToOneNode(n.action) return n, nil } diff --git a/pkg/plugin/systemx/reflect.go b/pkg/plugin/systemx/reflect.go index 10c9dbd3..d3550df8 100644 --- a/pkg/plugin/systemx/reflect.go +++ b/pkg/plugin/systemx/reflect.go @@ -56,9 +56,7 @@ func NewReflectNode(config ReflectNodeConfig) *ReflectNode { op: op, storage: storage, } - n.OneToOneNode = node.NewOneToOneNode(node.OneToOneNodeConfig{ - Action: n.action, - }) + n.OneToOneNode = node.NewOneToOneNode(n.action) return n } diff --git a/pkg/runtime/runtime_test.go b/pkg/runtime/runtime_test.go index c21f0a2d..abe5b8c4 100644 --- a/pkg/runtime/runtime_test.go +++ b/pkg/runtime/runtime_test.go @@ -19,7 +19,7 @@ func TestRuntime_Lookup(t *testing.T) { sb := scheme.NewBuilder(func(s *scheme.Scheme) error { s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) return nil }) @@ -56,7 +56,7 @@ func TestRuntime_Free(t *testing.T) { sb := scheme.NewBuilder(func(s *scheme.Scheme) error { s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) return nil }) @@ -94,7 +94,7 @@ func TestRuntime_Start(t *testing.T) { sb := scheme.NewBuilder(func(s *scheme.Scheme) error { s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) return nil }) diff --git a/pkg/scheme/scheme_test.go b/pkg/scheme/scheme_test.go index c20f4762..145301c2 100644 --- a/pkg/scheme/scheme_test.go +++ b/pkg/scheme/scheme_test.go @@ -25,7 +25,7 @@ func TestScheme_Codec(t *testing.T) { kind := faker.Word() c := CodecFunc(func(spec Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil }) s.AddCodec(kind, c) @@ -51,7 +51,7 @@ func TestScheme_Decode(t *testing.T) { s.AddKnownType(kind, &SpecMeta{}) s.AddCodec(kind, CodecFunc(func(spec Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) n, err := s.Decode(&SpecMeta{}) diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index 23befe37..160b8a09 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -18,7 +18,7 @@ func TestStorage_Watch(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -58,7 +58,7 @@ func TestStorage_InsertOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -82,7 +82,7 @@ func TestStorage_InsertMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -117,7 +117,7 @@ func TestStorage_UpdateOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -147,7 +147,7 @@ func TestStorage_UpdateMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -185,7 +185,7 @@ func TestStorage_DeleteOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -216,7 +216,7 @@ func TestStorage_DeleteMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -248,7 +248,7 @@ func TestStorage_FindOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -276,7 +276,7 @@ func TestStorage_FindOne(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ @@ -306,7 +306,7 @@ func TestStorage_FindMany(t *testing.T) { s := scheme.New() s.AddKnownType(kind, &scheme.SpecMeta{}) s.AddCodec(kind, scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { - return node.NewOneToOneNode(node.OneToOneNodeConfig{}), nil + return node.NewOneToOneNode(nil), nil })) st, _ := New(context.Background(), Config{ diff --git a/pkg/symbol/symbol_test.go b/pkg/symbol/symbol_test.go index 132c1368..08ffc9f8 100644 --- a/pkg/symbol/symbol_test.go +++ b/pkg/symbol/symbol_test.go @@ -11,7 +11,7 @@ import ( ) func TestSymbol_Getter(t *testing.T) { - n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n := node.NewOneToOneNode(nil) defer n.Close() spec := &scheme.SpecMeta{ ID: ulid.Make(), diff --git a/pkg/symbol/table_test.go b/pkg/symbol/table_test.go index ca490481..ed88f0c9 100644 --- a/pkg/symbol/table_test.go +++ b/pkg/symbol/table_test.go @@ -16,11 +16,11 @@ func TestTable_Insert(t *testing.T) { tb := NewTable() defer tb.Close() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n1 := node.NewOneToOneNode(nil) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n2 := node.NewOneToOneNode(nil) defer n2.Close() - n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n3 := node.NewOneToOneNode(nil) defer n3.Close() spec1 := &scheme.SpecMeta{ @@ -93,13 +93,13 @@ func TestTable_Insert(t *testing.T) { id := ulid.Make() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n1 := node.NewOneToOneNode(nil) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n2 := node.NewOneToOneNode(nil) defer n2.Close() - n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n3 := node.NewOneToOneNode(nil) defer n3.Close() - n4 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n4 := node.NewOneToOneNode(nil) defer n3.Close() spec1 := &scheme.SpecMeta{ @@ -181,11 +181,11 @@ func TestTable_Insert(t *testing.T) { tb := NewTable() defer tb.Close() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n1 := node.NewOneToOneNode(nil) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n2 := node.NewOneToOneNode(nil) defer n2.Close() - n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n3 := node.NewOneToOneNode(nil) defer n3.Close() spec1 := &scheme.SpecMeta{ @@ -262,13 +262,13 @@ func TestTable_Insert(t *testing.T) { id := ulid.Make() name := faker.UUIDHyphenated() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n1 := node.NewOneToOneNode(nil) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n2 := node.NewOneToOneNode(nil) defer n2.Close() - n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n3 := node.NewOneToOneNode(nil) defer n3.Close() - n4 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n4 := node.NewOneToOneNode(nil) defer n3.Close() spec1 := &scheme.SpecMeta{ @@ -354,11 +354,11 @@ func TestTable_Free(t *testing.T) { tb := NewTable() defer tb.Close() - n1 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n1 := node.NewOneToOneNode(nil) defer n1.Close() - n2 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n2 := node.NewOneToOneNode(nil) defer n2.Close() - n3 := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n3 := node.NewOneToOneNode(nil) defer n3.Close() spec1 := &scheme.SpecMeta{ @@ -433,7 +433,7 @@ func TestTable_LookupByID(t *testing.T) { tb := NewTable() defer tb.Close() - n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n := node.NewOneToOneNode(nil) defer n.Close() spec := &scheme.SpecMeta{ ID: ulid.Make(), @@ -451,7 +451,7 @@ func TestTable_LookupByName(t *testing.T) { tb := NewTable() defer tb.Close() - n := node.NewOneToOneNode(node.OneToOneNodeConfig{}) + n := node.NewOneToOneNode(nil) defer n.Close() spec := &scheme.SpecMeta{ ID: ulid.Make(), From b9c8656030b1a5e5bf5667694cfc6cad55031d8d Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:13:47 -0500 Subject: [PATCH 4/8] docs: remove be simple comment --- pkg/symbol/symbol.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/symbol/symbol.go b/pkg/symbol/symbol.go index 9ea429ea..16b33b69 100644 --- a/pkg/symbol/symbol.go +++ b/pkg/symbol/symbol.go @@ -15,32 +15,32 @@ type Symbol struct { var _ node.Node = (*Symbol)(nil) -// ID returns the unique identifier of the Symbol, based on its Spec. +// ID returns the unique identifier. func (s *Symbol) ID() ulid.ULID { return s.Spec.GetID() } -// Kind returns the kind of the Symbol, based on its Spec. +// Kind returns the kind. func (s *Symbol) Kind() string { return s.Spec.GetKind() } -// Namespace returns the namespace of the Symbol, based on its Spec. +// Namespace returns the namespace. func (s *Symbol) Namespace() string { return s.Spec.GetNamespace() } -// Name returns the name of the Symbol, based on its Spec. +// Name returns the name. func (s *Symbol) Name() string { return s.Spec.GetName() } -// Links returns the links of the Symbol, based on its Spec. +// Links returns the links. func (s *Symbol) Links() map[string][]scheme.PortLocation { return s.Spec.GetLinks() } -// Port returns the specified port of the Symbol, based on its Node. +// Port returns the specified port. func (s *Symbol) Port(name string) (*port.Port, bool) { return s.Node.Port(name) } From ccafbc84fa01ca684a0ffdb8f68dee1690135e40 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:34:47 -0500 Subject: [PATCH 5/8] refactor: remove empty struct --- pkg/plugin/controllx/builder.go | 2 +- pkg/plugin/controllx/switch.go | 8 ++------ pkg/plugin/controllx/switch_test.go | 6 +++--- pkg/plugin/networkx/builder.go | 2 +- pkg/plugin/networkx/router.go | 8 ++------ pkg/plugin/networkx/router_test.go | 6 +++--- 6 files changed, 12 insertions(+), 20 deletions(-) diff --git a/pkg/plugin/controllx/builder.go b/pkg/plugin/controllx/builder.go index 18738c10..fa65d762 100644 --- a/pkg/plugin/controllx/builder.go +++ b/pkg/plugin/controllx/builder.go @@ -18,7 +18,7 @@ func AddToScheme() func(*scheme.Scheme) error { s.AddKnownType(KindSwitch, &SwitchSpec{}) s.AddCodec(KindSwitch, scheme.CodecWithType[*SwitchSpec](func(spec *SwitchSpec) (node.Node, error) { - n := NewSwitchNode(SwitchNodeConfig{}) + n := NewSwitchNode() for _, v := range spec.Match { if err := n.Add(v.When, v.Port); err != nil { _ = n.Close() diff --git a/pkg/plugin/controllx/switch.go b/pkg/plugin/controllx/switch.go index 1643ac44..9caff4a7 100644 --- a/pkg/plugin/controllx/switch.go +++ b/pkg/plugin/controllx/switch.go @@ -12,10 +12,6 @@ import ( "github.com/xiatechs/jsonata-go" ) -// SwitchNodeConfig holds the configuration for creating a SwitchNode. -type SwitchNodeConfig struct { -} - // SwitchNode represents a node that switches packets based on conditions. type SwitchNode struct { *node.OneToManyNode @@ -46,8 +42,8 @@ const KindSwitch = "switch" var _ node.Node = (*SwitchNode)(nil) var _ scheme.Spec = (*SwitchSpec)(nil) -// NewSwitchNode creates a new SwitchNode with the given configuration. -func NewSwitchNode(config SwitchNodeConfig) *SwitchNode { +// NewSwitchNode creates a new SwitchNode. +func NewSwitchNode() *SwitchNode { n := &SwitchNode{} n.OneToManyNode = node.NewOneToManyNode(n.action) diff --git a/pkg/plugin/controllx/switch_test.go b/pkg/plugin/controllx/switch_test.go index 7f065780..49dcfddf 100644 --- a/pkg/plugin/controllx/switch_test.go +++ b/pkg/plugin/controllx/switch_test.go @@ -15,14 +15,14 @@ import ( ) func TestNewSwitchNode(t *testing.T) { - n := NewSwitchNode(SwitchNodeConfig{}) + n := NewSwitchNode() assert.NotNil(t, n) _ = n.Close() } func TestSwitchNode_Send(t *testing.T) { - n := NewSwitchNode(SwitchNodeConfig{}) + n := NewSwitchNode() defer func() { _ = n.Close() }() in := port.New() @@ -102,7 +102,7 @@ func TestSwitchNode_Send(t *testing.T) { } func BenchmarkSwitchNode_Send(b *testing.B) { - n := NewSwitchNode(SwitchNodeConfig{}) + n := NewSwitchNode() defer func() { _ = n.Close() }() in := port.New() diff --git a/pkg/plugin/networkx/builder.go b/pkg/plugin/networkx/builder.go index fe7597ff..0c3d4cc5 100644 --- a/pkg/plugin/networkx/builder.go +++ b/pkg/plugin/networkx/builder.go @@ -54,7 +54,7 @@ func AddToScheme() func(*scheme.Scheme) error { s.AddKnownType(KindRouter, &RouterSpec{}) s.AddCodec(KindRouter, scheme.CodecWithType[*RouterSpec](func(spec *RouterSpec) (node.Node, error) { - n := NewRouterNode(RouterNodeConfig{}) + n := NewRouterNode() for _, r := range spec.Routes { n.Add(r.Method, r.Path, r.Port) diff --git a/pkg/plugin/networkx/router.go b/pkg/plugin/networkx/router.go index 0d205db5..5bcd7fad 100644 --- a/pkg/plugin/networkx/router.go +++ b/pkg/plugin/networkx/router.go @@ -13,10 +13,6 @@ import ( "github.com/siyul-park/uniflow/pkg/scheme" ) -// RouterNodeConfig holds the configuration for RouterNode. -type RouterNodeConfig struct { -} - // RouterNode represents a router node that handles routing based on HTTP methods, paths, and ports. type RouterNode struct { *node.OneToManyNode @@ -71,8 +67,8 @@ const ( var _ node.Node = (*RouterNode)(nil) var _ scheme.Spec = (*RouterSpec)(nil) -// NewRouterNode creates a new instance of RouterNode with the given configuration. -func NewRouterNode(config RouterNodeConfig) *RouterNode { +// NewRouterNode creates a new instance of RouterNode. +func NewRouterNode() *RouterNode { n := &RouterNode{ tree: &route{ methods: map[string]string{}, diff --git a/pkg/plugin/networkx/router_test.go b/pkg/plugin/networkx/router_test.go index 2d02703a..f0b965ad 100644 --- a/pkg/plugin/networkx/router_test.go +++ b/pkg/plugin/networkx/router_test.go @@ -15,14 +15,14 @@ import ( ) func TestNewRouterNode(t *testing.T) { - n := NewRouterNode(RouterNodeConfig{}) + n := NewRouterNode() assert.NotNil(t, n) _ = n.Close() } func TestRouterNode_Send(t *testing.T) { - n := NewRouterNode(RouterNodeConfig{}) + n := NewRouterNode() defer func() { _ = n.Close() }() in := port.New() @@ -106,7 +106,7 @@ func TestRouterNode_Send(t *testing.T) { } func BenchmarkRouterNode_Send(b *testing.B) { - n := NewRouterNode(RouterNodeConfig{}) + n := NewRouterNode() defer func() { _ = n.Close() }() in := port.New() From fb0db4153497ab5ab90e9613a638583bc128d433 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:43:27 -0500 Subject: [PATCH 6/8] refactor: remove snippet config --- pkg/plugin/controllx/builder.go | 5 +-- pkg/plugin/controllx/snippet.go | 13 ++----- pkg/plugin/controllx/snippet_test.go | 53 +++++++--------------------- 3 files changed, 16 insertions(+), 55 deletions(-) diff --git a/pkg/plugin/controllx/builder.go b/pkg/plugin/controllx/builder.go index fa65d762..a35fbce6 100644 --- a/pkg/plugin/controllx/builder.go +++ b/pkg/plugin/controllx/builder.go @@ -10,10 +10,7 @@ func AddToScheme() func(*scheme.Scheme) error { return func(s *scheme.Scheme) error { s.AddKnownType(KindSnippet, &SnippetSpec{}) s.AddCodec(KindSnippet, scheme.CodecWithType[*SnippetSpec](func(spec *SnippetSpec) (node.Node, error) { - return NewSnippetNode(SnippetNodeConfig{ - Lang: spec.Lang, - Code: spec.Code, - }) + return NewSnippetNode(spec.Lang, spec.Code) })) s.AddKnownType(KindSwitch, &SwitchSpec{}) diff --git a/pkg/plugin/controllx/snippet.go b/pkg/plugin/controllx/snippet.go index 452473c5..1c62eaad 100644 --- a/pkg/plugin/controllx/snippet.go +++ b/pkg/plugin/controllx/snippet.go @@ -18,12 +18,6 @@ import ( "github.com/xiatechs/jsonata-go" ) -// SnippetNodeConfig holds the configuration for creating a SnippetNode. -type SnippetNodeConfig struct { - Lang string - Code string -} - // SnippetNode represents a node that runs a snippet of code. type SnippetNode struct { *node.OneToOneNode @@ -57,13 +51,10 @@ var ( var _ node.Node = (*SnippetNode)(nil) var _ scheme.Spec = (*SnippetSpec)(nil) -// NewSnippetNode creates a new SnippetNode with the given configuration. -func NewSnippetNode(config SnippetNodeConfig) (*SnippetNode, error) { +// NewSnippetNode creates a new SnippetNode. +func NewSnippetNode(lang, code string) (*SnippetNode, error) { defer func() { _ = recover() }() - lang := config.Lang - code := config.Code - run, err := compile(lang, code) if err != nil { return nil, err diff --git a/pkg/plugin/controllx/snippet_test.go b/pkg/plugin/controllx/snippet_test.go index 3ace18ab..2bc41e32 100644 --- a/pkg/plugin/controllx/snippet_test.go +++ b/pkg/plugin/controllx/snippet_test.go @@ -16,10 +16,7 @@ import ( ) func TestNewSnippetNode(t *testing.T) { - n, err := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJSON, - Code: "{}", - }) + n, err := NewSnippetNode(LangJSON, "") assert.NoError(t, err) assert.NotNil(t, n) @@ -28,14 +25,11 @@ func TestNewSnippetNode(t *testing.T) { func TestSnippetNode_Send(t *testing.T) { t.Run(LangTypescript, func(t *testing.T) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangTypescript, - Code: ` + n, _ := NewSnippetNode(LangTypescript, ` function main(inPayload: any): any { return inPayload; } - `, - }) + `) defer func() { _ = n.Close() }() io := port.New() @@ -64,14 +58,11 @@ function main(inPayload: any): any { }) t.Run(LangJavascript, func(t *testing.T) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJavascript, - Code: ` + n, _ := NewSnippetNode(LangTypescript, ` function main(inPayload) { return inPayload; } - `, - }) + `) defer func() { _ = n.Close() }() io := port.New() @@ -102,10 +93,7 @@ function main(inPayload) { t.Run(LangJSON, func(t *testing.T) { data := faker.UUIDHyphenated() - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJSON, - Code: fmt.Sprintf("\"%s\"", data), - }) + n, _ := NewSnippetNode(LangJSON, fmt.Sprintf("\"%s\"", data)) defer func() { _ = n.Close() }() io := port.New() @@ -134,10 +122,7 @@ function main(inPayload) { }) t.Run(LangJSONata, func(t *testing.T) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJSONata, - Code: "$", - }) + n, _ := NewSnippetNode(LangJSONata, "$") defer func() { _ = n.Close() }() io := port.New() @@ -168,14 +153,11 @@ function main(inPayload) { func BenchmarkSnippetNode_Send(b *testing.B) { b.Run(LangTypescript, func(b *testing.B) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangTypescript, - Code: ` + n, _ := NewSnippetNode(LangTypescript, ` function main(inPayload: any): any { return inPayload; } - `, - }) + `) defer func() { _ = n.Close() }() io := port.New() @@ -199,14 +181,11 @@ function main(inPayload: any): any { }) b.Run(LangJavascript, func(b *testing.B) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJavascript, - Code: ` + n, _ := NewSnippetNode(LangJavascript, ` function main(inPayload) { return inPayload; } - `, - }) + `) defer func() { _ = n.Close() }() io := port.New() @@ -230,10 +209,7 @@ function main(inPayload) { }) b.Run(LangJSON, func(b *testing.B) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJSON, - Code: fmt.Sprintf("\"%s\"", faker.UUIDHyphenated()), - }) + n, _ := NewSnippetNode(LangJSON, fmt.Sprintf("\"%s\"", faker.UUIDHyphenated())) defer func() { _ = n.Close() }() io := port.New() @@ -257,10 +233,7 @@ function main(inPayload) { }) b.Run(LangJSONata, func(b *testing.B) { - n, _ := NewSnippetNode(SnippetNodeConfig{ - Lang: LangJSONata, - Code: "$", - }) + n, _ := NewSnippetNode(LangJSONata, "$") defer func() { _ = n.Close() }() io := port.New() From 5b5062a35d14d3f0a22b47b20afca1a83eada406 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 6 Dec 2023 11:45:09 -0500 Subject: [PATCH 7/8] refactor: remove http config --- pkg/plugin/networkx/builder.go | 4 +--- pkg/plugin/networkx/builder_test.go | 4 +--- pkg/plugin/networkx/http.go | 11 ++--------- pkg/plugin/networkx/http_test.go | 20 +++++++------------- 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/pkg/plugin/networkx/builder.go b/pkg/plugin/networkx/builder.go index 0c3d4cc5..08af681b 100644 --- a/pkg/plugin/networkx/builder.go +++ b/pkg/plugin/networkx/builder.go @@ -47,9 +47,7 @@ func AddToScheme() func(*scheme.Scheme) error { return func(s *scheme.Scheme) error { s.AddKnownType(KindHTTP, &HTTPSpec{}) s.AddCodec(KindHTTP, scheme.CodecWithType[*HTTPSpec](func(spec *HTTPSpec) (node.Node, error) { - return NewHTTPNode(HTTPNodeConfig{ - Address: spec.Address, - }), nil + return NewHTTPNode(spec.Address), nil })) s.AddKnownType(KindRouter, &RouterSpec{}) diff --git a/pkg/plugin/networkx/builder_test.go b/pkg/plugin/networkx/builder_test.go index 4fb9e077..c9c5ab0c 100644 --- a/pkg/plugin/networkx/builder_test.go +++ b/pkg/plugin/networkx/builder_test.go @@ -19,9 +19,7 @@ func TestAddToHooks(t *testing.T) { port, err := freeport.GetFreePort() assert.NoError(t, err) - n := NewHTTPNode(HTTPNodeConfig{ - Address: fmt.Sprintf(":%d", port), - }) + n := NewHTTPNode(fmt.Sprintf(":%d", port)) err = hk.Load(n) assert.NoError(t, err) diff --git a/pkg/plugin/networkx/http.go b/pkg/plugin/networkx/http.go index 09fe677f..1ebccce7 100644 --- a/pkg/plugin/networkx/http.go +++ b/pkg/plugin/networkx/http.go @@ -22,11 +22,6 @@ import ( "github.com/siyul-park/uniflow/pkg/scheme" ) -// HTTPNodeConfig represents the configuration of an HTTP node. -type HTTPNodeConfig struct { - Address string -} - // HTTPNode represents a node based on the HTTP protocol. type HTTPNode struct { address string @@ -247,10 +242,8 @@ func init() { } } -// NewHTTPNode creates a new HTTPNode with the given configuration. -func NewHTTPNode(config HTTPNodeConfig) *HTTPNode { - address := config.Address - +// NewHTTPNode creates a new HTTPNode. +func NewHTTPNode(address string) *HTTPNode { n := &HTTPNode{ address: address, server: new(http.Server), diff --git a/pkg/plugin/networkx/http_test.go b/pkg/plugin/networkx/http_test.go index 2ea92fd4..d4a70101 100644 --- a/pkg/plugin/networkx/http_test.go +++ b/pkg/plugin/networkx/http_test.go @@ -21,9 +21,7 @@ func TestNewHTTPNode(t *testing.T) { port, err := freeport.GetFreePort() assert.NoError(t, err) - n := NewHTTPNode(HTTPNodeConfig{ - Address: fmt.Sprintf(":%d", port), - }) + n := NewHTTPNode(fmt.Sprintf(":%d", port)) assert.NotNil(t, n) assert.NoError(t, n.Close()) @@ -33,9 +31,7 @@ func TestHTTPNode_Port(t *testing.T) { port, err := freeport.GetFreePort() assert.NoError(t, err) - n := NewHTTPNode(HTTPNodeConfig{ - Address: fmt.Sprintf(":%d", port), - }) + n := NewHTTPNode(fmt.Sprintf(":%d", port)) defer n.Close() p, ok := n.Port(node.PortIO) @@ -59,9 +55,7 @@ func TestHTTPNode_ServeAndShutdown(t *testing.T) { port, err := freeport.GetFreePort() assert.NoError(t, err) - n := NewHTTPNode(HTTPNodeConfig{ - Address: fmt.Sprintf(":%d", port), - }) + n := NewHTTPNode(fmt.Sprintf(":%d", port)) defer n.Close() errChan := make(chan error) @@ -83,7 +77,7 @@ func TestHTTPNode_ServeAndShutdown(t *testing.T) { func TestHTTPNode_ServeHTTP(t *testing.T) { t.Run("IO", func(t *testing.T) { - n := NewHTTPNode(HTTPNodeConfig{}) + n := NewHTTPNode("") defer func() { _ = n.Close() }() io := port.New() @@ -119,7 +113,7 @@ func TestHTTPNode_ServeHTTP(t *testing.T) { }) t.Run("In/Out", func(t *testing.T) { - n := NewHTTPNode(HTTPNodeConfig{}) + n := NewHTTPNode("") defer func() { _ = n.Close() }() in := port.New() @@ -162,7 +156,7 @@ func TestHTTPNode_ServeHTTP(t *testing.T) { func BenchmarkHTTPNode_Send(b *testing.B) { b.Run("IO", func(b *testing.B) { - n := NewHTTPNode(HTTPNodeConfig{}) + n := NewHTTPNode("") defer func() { _ = n.Close() }() io := port.New() @@ -198,7 +192,7 @@ func BenchmarkHTTPNode_Send(b *testing.B) { }) b.Run("In/Out", func(b *testing.B) { - n := NewHTTPNode(HTTPNodeConfig{}) + n := NewHTTPNode("") defer func() { _ = n.Close() }() in := port.New() From 0263d9d01f7c6c1c92dbbf6fa2f38905d6aa6733 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Thu, 7 Dec 2023 03:13:29 -0500 Subject: [PATCH 8/8] fix: test error --- pkg/plugin/controllx/snippet_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/plugin/controllx/snippet_test.go b/pkg/plugin/controllx/snippet_test.go index 2bc41e32..0167bb3c 100644 --- a/pkg/plugin/controllx/snippet_test.go +++ b/pkg/plugin/controllx/snippet_test.go @@ -16,7 +16,7 @@ import ( ) func TestNewSnippetNode(t *testing.T) { - n, err := NewSnippetNode(LangJSON, "") + n, err := NewSnippetNode(LangJSON, "{}") assert.NoError(t, err) assert.NotNil(t, n)