diff --git a/cmd/uniflow/apply/cmd.go b/cmd/uniflow/apply/cmd.go index c1b28e17..06dd058f 100644 --- a/cmd/uniflow/apply/cmd.go +++ b/cmd/uniflow/apply/cmd.go @@ -86,7 +86,7 @@ func NewCmd(config Config) *cobra.Command { } for _, spec := range specs { - if util.IsZero(spec.GetID()) { + if spec.GetID() == (ulid.ULID{}) { if spec.GetName() != "" { exist, err := st.FindOne(ctx, storage.Where[string](scheme.KeyName).EQ(spec.GetName()).And(storage.Where[string](scheme.KeyNamespace).EQ(spec.GetNamespace()))) if err != nil { diff --git a/internal/util/compare.go b/internal/util/compare.go index e4c10905..f4f75304 100644 --- a/internal/util/compare.go +++ b/internal/util/compare.go @@ -5,13 +5,6 @@ import ( "reflect" ) -func IsZero(v any) bool { - if IsNil(v) { - return true - } - return reflect.ValueOf(v).IsZero() -} - func Equal(x any, y any) bool { if IsNil(x) != IsNil(y) { return false diff --git a/internal/util/compare_test.go b/internal/util/compare_test.go index 4427af82..edd2194a 100644 --- a/internal/util/compare_test.go +++ b/internal/util/compare_test.go @@ -6,38 +6,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestIsZero(t *testing.T) { - var testCase = []struct { - when any - expect bool - }{ - { - when: nil, - expect: true, - }, - { - when: "", - expect: true, - }, - { - when: 0, - expect: true, - }, - { - when: false, - expect: true, - }, - { - when: struct{}{}, - expect: true, - }, - } - - for _, tc := range testCase { - assert.Equal(t, tc.expect, IsZero(tc.when)) - } -} - func TestEqual(t *testing.T) { var testCase = []struct { when []any diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index d11898cc..b3b3e672 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -289,7 +289,7 @@ func (ld *Loader) resolveLinks(ctx context.Context, local scheme.Spec, remote sc for _, location := range locations { id := location.ID - if util.IsZero(id) { + if id == (ulid.ULID{}) { if location.Name != "" { filter := storage.Where[string](scheme.KeyNamespace).EQ(spec.GetNamespace()) filter = filter.And(storage.Where[string](scheme.KeyName).EQ(location.Name)) @@ -301,7 +301,7 @@ func (ld *Loader) resolveLinks(ctx context.Context, local scheme.Spec, remote sc } } - if !util.IsZero(id) { + if id != (ulid.ULID{}) { if ref, ok := ld.table.Lookup(id); ok { referenced := ld.referenced[ref.ID()] var locations []scheme.PortLocation @@ -331,7 +331,7 @@ func (ld *Loader) resolveLinks(ctx context.Context, local scheme.Spec, remote sc for _, location := range locations { filter := storage.Where[string](scheme.KeyNamespace).EQ(spec.GetNamespace()) - if !util.IsZero(location.ID) { + if location.ID != (ulid.ULID{}) { filter = filter.And(storage.Where[ulid.ULID](scheme.KeyID).EQ(location.ID)) } else if location.Name != "" { filter = filter.And(storage.Where[string](scheme.KeyName).EQ(location.Name)) @@ -410,7 +410,7 @@ func (ld *Loader) resolveLinks(ctx context.Context, local scheme.Spec, remote sc } for _, location := range locations { - if (!util.IsZero(location.ID) && location.ID == spec.GetID()) || (location.Name != "" && location.Name == spec.GetName()) { + if (location.ID == spec.GetID()) || (location.Name != "" && location.Name == spec.GetName()) { if p2, ok := n.Port(location.Port); ok { p1.Link(p2) diff --git a/pkg/node/onetomany.go b/pkg/node/onetomany.go index 57fd88ff..308c5dc1 100644 --- a/pkg/node/onetomany.go +++ b/pkg/node/onetomany.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/oklog/ulid/v2" - "github.com/siyul-park/uniflow/internal/util" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" "github.com/siyul-park/uniflow/pkg/process" @@ -35,7 +34,7 @@ func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode { id := config.ID action := config.Action - if util.IsZero(id) { + if id == (ulid.ULID{}) { id = ulid.Make() } if action == nil { diff --git a/pkg/node/onetoone.go b/pkg/node/onetoone.go index 0c0a28a8..b7dc08d2 100644 --- a/pkg/node/onetoone.go +++ b/pkg/node/onetoone.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/oklog/ulid/v2" - "github.com/siyul-park/uniflow/internal/util" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" "github.com/siyul-park/uniflow/pkg/process" @@ -36,7 +35,7 @@ func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode { id := config.ID action := config.Action - if util.IsZero(id) { + if id == (ulid.ULID{}) { id = ulid.Make() } if action == nil { diff --git a/pkg/plugin/controllx/switch.go b/pkg/plugin/controllx/switch.go index 841a6362..ed602009 100644 --- a/pkg/plugin/controllx/switch.go +++ b/pkg/plugin/controllx/switch.go @@ -1,10 +1,10 @@ package controllx import ( + "reflect" "sync" "github.com/oklog/ulid/v2" - "github.com/siyul-park/uniflow/internal/util" "github.com/siyul-park/uniflow/pkg/node" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" @@ -91,7 +91,7 @@ func (n *SwitchNode) action(proc *process.Process, inPck *packet.Packet) ([]*pac } for _, cond := range n.conditions { - if output, _ := cond.when.Eval(input); !util.IsZero(output) { + if output, _ := cond.when.Eval(input); output != nil && !reflect.ValueOf(output).IsZero() { if i, ok := port.GetIndex(node.PortOut, cond.port); ok { outPcks := make([]*packet.Packet, i+1) outPcks[i] = inPck diff --git a/pkg/plugin/networkx/http.go b/pkg/plugin/networkx/http.go index f6873688..b8a91dfe 100644 --- a/pkg/plugin/networkx/http.go +++ b/pkg/plugin/networkx/http.go @@ -15,7 +15,6 @@ import ( "time" "github.com/oklog/ulid/v2" - "github.com/siyul-park/uniflow/internal/util" "github.com/siyul-park/uniflow/pkg/node" "github.com/siyul-park/uniflow/pkg/packet" "github.com/siyul-park/uniflow/pkg/port" @@ -258,7 +257,7 @@ func NewHTTPNode(config HTTPNodeConfig) *HTTPNode { id := config.ID address := config.Address - if util.IsZero(id) { + if id == (ulid.ULID{}) { id = ulid.Make() } diff --git a/pkg/plugin/systemx/reflect.go b/pkg/plugin/systemx/reflect.go index f7797808..3c398d2a 100644 --- a/pkg/plugin/systemx/reflect.go +++ b/pkg/plugin/systemx/reflect.go @@ -162,7 +162,7 @@ func (n *ReflectNode) action(proc *process.Process, inPck *packet.Packet) (*pack for i, spec := range specs { id := spec.GetID() - if !util.IsZero(id) { + if id != (ulid.ULID{}) { ids = append(ids, id) patches[id] = examples[i] } @@ -227,16 +227,16 @@ func examplesToFilter(examples []*primitive.Map) (*storage.Filter, error) { return nil, err } - if !util.IsZero(spec.ID) { + if spec.ID != (ulid.ULID{}) { sub = sub.And(storage.Where[ulid.ULID](scheme.KeyID).EQ(spec.ID)) } - if !util.IsZero(spec.Kind) { + if spec.Kind != "" { sub = sub.And(storage.Where[string](scheme.KeyKind).EQ(spec.Kind)) } - if !util.IsZero(spec.Name) { + if spec.Name != "" { sub = sub.And(storage.Where[string](scheme.KeyName).EQ(spec.Name)) } - if !util.IsZero(spec.Namespace) { + if spec.Namespace != "" { sub = sub.And(storage.Where[string](scheme.KeyName).EQ(spec.Namespace)) } diff --git a/pkg/scheme/unstructured.go b/pkg/scheme/unstructured.go index ce438aba..4d85743d 100644 --- a/pkg/scheme/unstructured.go +++ b/pkg/scheme/unstructured.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/oklog/ulid/v2" - "github.com/siyul-park/uniflow/internal/util" "github.com/siyul-park/uniflow/pkg/primitive" ) @@ -35,10 +34,10 @@ func NewUnstructured(doc *primitive.Map) *Unstructured { u := &Unstructured{doc: doc} - if v := u.GetID(); !util.IsZero(v) { + if v := u.GetID(); v != (ulid.ULID{}) { u.SetID(v) } - if v := u.GetLinks(); !util.IsZero(v) { + if v := u.GetLinks(); len(v) > 0 { u.SetLinks(v) } diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index d1ee8cd3..e84dc3a0 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -113,7 +113,7 @@ func (s *Storage) InsertOne(ctx context.Context, spec scheme.Spec) (ulid.ULID, e if unstructured.GetNamespace() == "" { unstructured.SetNamespace(scheme.NamespaceDefault) } - if util.IsZero(unstructured.GetID()) { + if unstructured.GetID() == (ulid.ULID{}) { unstructured.SetID(ulid.Make()) } @@ -147,7 +147,7 @@ func (s *Storage) InsertMany(ctx context.Context, objs []scheme.Spec) ([]ulid.UL if unstructured.GetNamespace() == "" { unstructured.SetNamespace(scheme.NamespaceDefault) } - if util.IsZero(unstructured.GetID()) { + if unstructured.GetID() == (ulid.ULID{}) { unstructured.SetID(ulid.Make()) } @@ -182,7 +182,7 @@ func (s *Storage) UpdateOne(ctx context.Context, spec scheme.Spec) (bool, error) if unstructured.GetNamespace() == "" { unstructured.SetNamespace(scheme.NamespaceDefault) } - if util.IsZero(unstructured.GetID()) { + if unstructured.GetID() == (ulid.ULID{}) { return false, nil } @@ -209,7 +209,7 @@ func (s *Storage) UpdateMany(ctx context.Context, objs []scheme.Spec) (int, erro if unstructured.GetNamespace() == "" { unstructured.SetNamespace(scheme.NamespaceDefault) } - if util.IsZero(unstructured.GetID()) { + if unstructured.GetID() == (ulid.ULID{}) { continue }