Skip to content

Commit

Permalink
refactor: remove util.IsZero
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
1 parent 5c3505b commit 41f5a0e
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cmd/uniflow/apply/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 0 additions & 7 deletions internal/util/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions internal/util/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions pkg/node/onetomany.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/node/onetoone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/controllx/switch.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/plugin/networkx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/plugin/systemx/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down Expand Up @@ -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))
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/scheme/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 41f5a0e

Please sign in to comment.