From 9ec82e7de13fabf1b6728395406516cd461fbd9c Mon Sep 17 00:00:00 2001 From: siyul-park Date: Fri, 24 Nov 2023 07:43:07 -0500 Subject: [PATCH] refactor: remove equal --- pkg/database/memdb/filter.go | 8 ++--- pkg/database/memdb/index.go | 2 +- pkg/primitive/binary.go | 15 -------- pkg/primitive/binary_test.go | 8 ----- pkg/primitive/bool.go | 9 ----- pkg/primitive/bool_test.go | 7 ---- pkg/primitive/float.go | 14 -------- pkg/primitive/float_test.go | 14 -------- pkg/primitive/int.go | 70 ------------------------------------ pkg/primitive/int_test.go | 28 --------------- pkg/primitive/map.go | 32 ----------------- pkg/primitive/map_test.go | 10 ------ pkg/primitive/object.go | 13 ------- pkg/primitive/slice.go | 15 -------- pkg/primitive/slice_test.go | 8 ----- pkg/primitive/string.go | 8 ----- pkg/primitive/string_test.go | 6 ---- pkg/primitive/uint.go | 70 ------------------------------------ pkg/primitive/uint_test.go | 28 --------------- 19 files changed, 5 insertions(+), 360 deletions(-) diff --git a/pkg/database/memdb/filter.go b/pkg/database/memdb/filter.go index 8529679e..070ab1a9 100644 --- a/pkg/database/memdb/filter.go +++ b/pkg/database/memdb/filter.go @@ -21,7 +21,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } else if v, ok := filter.Value.(primitive.Object); !ok { return false } else { - return primitive.Equal(o, v) + return primitive.Compare(o, v) == 0 } } case database.NE: @@ -31,7 +31,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } else if v, ok := filter.Value.(primitive.Object); !ok { return false } else { - return !primitive.Equal(o, v) + return primitive.Compare(o, v) != 0 } } case database.LT: @@ -84,7 +84,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { return false } else { for i := 0; i < v.Len(); i++ { - if primitive.Equal(o, v.Get(i)) { + if primitive.Compare(o, v.Get(i)) == 0 { return true } } @@ -101,7 +101,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { return false } else { for i := 0; i < v.Len(); i++ { - if !primitive.Equal(o, v.Get(i)) { + if primitive.Compare(o, v.Get(i)) != 0 { return true } } diff --git a/pkg/database/memdb/index.go b/pkg/database/memdb/index.go index 9af74895..8d0232f3 100644 --- a/pkg/database/memdb/index.go +++ b/pkg/database/memdb/index.go @@ -309,7 +309,7 @@ func (iv *IndexView) deleteOne(_ context.Context, doc *primitive.Map) error { return nil } } else if model.Unique { - if r, ok := curr.Get(obj); ok && primitive.Equal(id, r.(primitive.Object)) { + if r, ok := curr.Get(obj); ok && primitive.Compare(id, r.(primitive.Object)) == 0 { curr.Remove(obj) } } else { diff --git a/pkg/primitive/binary.go b/pkg/primitive/binary.go index 4aea48b6..90fe04bf 100644 --- a/pkg/primitive/binary.go +++ b/pkg/primitive/binary.go @@ -41,21 +41,6 @@ func (o Binary) Kind() Kind { return KindBinary } -func (o Binary) Equal(v Object) bool { - if r, ok := v.(Binary); !ok { - return false - } else if r.Len() != o.Len() { - return false - } else { - for i := 0; i < o.Len(); i++ { - if o.Get(i) != r.Get(i) { - return false - } - } - return true - } -} - func (o Binary) Compare(v Object) int { if r, ok := v.(Binary); !ok { if o.Kind() > v.Kind() { diff --git a/pkg/primitive/binary_test.go b/pkg/primitive/binary_test.go index 18b7d645..15e0f362 100644 --- a/pkg/primitive/binary_test.go +++ b/pkg/primitive/binary_test.go @@ -20,14 +20,6 @@ func TestBinary_Get(t *testing.T) { assert.Equal(t, byte(0), v.Get(0)) } -func TestBinary_Equal(t *testing.T) { - v1 := NewBinary([]byte{0}) - v2 := NewBinary([]byte{1}) - - assert.True(t, v1.Equal(v1)) - assert.False(t, v1.Equal(v2)) -} - func TestBinary_Compare(t *testing.T) { v1 := NewBinary([]byte{0}) v2 := NewBinary([]byte{1}) diff --git a/pkg/primitive/bool.go b/pkg/primitive/bool.go index 5dd3598c..95a964ec 100644 --- a/pkg/primitive/bool.go +++ b/pkg/primitive/bool.go @@ -32,15 +32,6 @@ func (o Bool) Bool() bool { func (o Bool) Kind() Kind { return KindBool } - -func (o Bool) Equal(v Object) bool { - if r, ok := v.(Bool); !ok { - return false - } else { - return o.Bool() == r.Bool() - } -} - func (o Bool) Compare(v Object) int { if r, ok := v.(Bool); !ok { if o.Kind() > v.Kind() { diff --git a/pkg/primitive/bool_test.go b/pkg/primitive/bool_test.go index 7f2db62b..4c241c59 100644 --- a/pkg/primitive/bool_test.go +++ b/pkg/primitive/bool_test.go @@ -20,13 +20,6 @@ func TestBool_Compare(t *testing.T) { assert.Equal(t, -1, FALSE.Compare(TRUE)) } -func TestBool_Equal(t *testing.T) { - assert.True(t, TRUE.Equal(TRUE)) - assert.True(t, FALSE.Equal(FALSE)) - assert.False(t, TRUE.Equal(FALSE)) - assert.False(t, FALSE.Equal(TRUE)) -} - func TestBool_Encode(t *testing.T) { e := NewBoolEncoder() diff --git a/pkg/primitive/float.go b/pkg/primitive/float.go index 05daa1e4..fe73a995 100644 --- a/pkg/primitive/float.go +++ b/pkg/primitive/float.go @@ -83,20 +83,6 @@ func (o Float64) Kind() Kind { return KindFloat64 } -func (o Float64) Equal(v Object) bool { - if r, ok := v.(Float); !ok { - if r, ok := v.(Integer); ok { - return o.Float() == float64(r.Int()) - } else if r, ok := v.(Uinteger); ok { - return o.Float() == float64(r.Uint()) - } else { - return false - } - } else { - return o.Float() == r.Float() - } -} - func (o Float64) Compare(v Object) int { if r, ok := v.(Float); !ok { if r, ok := v.(Integer); ok { diff --git a/pkg/primitive/float_test.go b/pkg/primitive/float_test.go index a05a8a68..26513e23 100644 --- a/pkg/primitive/float_test.go +++ b/pkg/primitive/float_test.go @@ -21,20 +21,6 @@ func TestNewFloat(t *testing.T) { }) } -func TestFloat_Equal(t *testing.T) { - t.Run("32", func(t *testing.T) { - assert.True(t, NewFloat32(0).Equal(NewFloat32(0))) - assert.True(t, NewFloat32(0).Equal(NewFloat64(0))) - assert.False(t, NewFloat32(1).Equal(NewFloat32(0))) - }) - - t.Run("64", func(t *testing.T) { - assert.True(t, NewFloat64(0).Equal(NewFloat64(0))) - assert.True(t, NewFloat64(0).Equal(NewFloat32(0))) - assert.False(t, NewFloat64(1).Equal(NewFloat64(0))) - }) -} - func TestFloat_Compare(t *testing.T) { t.Run("32", func(t *testing.T) { assert.Equal(t, 0, NewFloat32(0).Compare(NewFloat32(0))) diff --git a/pkg/primitive/int.go b/pkg/primitive/int.go index 31e78913..c9f8b7b9 100644 --- a/pkg/primitive/int.go +++ b/pkg/primitive/int.go @@ -44,20 +44,6 @@ func (o Int) Kind() Kind { return KindInt } -func (o Int) Equal(v Object) bool { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return o.Int() == int64(r.Uint()) - } else if r, ok := v.(Float); ok { - return float64(o.Int()) == r.Float() - } else { - return false - } - } else { - return o.Int() == r.Int() - } -} - func (o Int) Compare(v Object) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { @@ -92,20 +78,6 @@ func (o Int8) Kind() Kind { return KindInt8 } -func (o Int8) Equal(v Object) bool { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return o.Int() == int64(r.Uint()) - } else if r, ok := v.(Float); ok { - return float64(o.Int()) == r.Float() - } else { - return false - } - } else { - return o.Int() == r.Int() - } -} - func (o Int8) Compare(v Object) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { @@ -140,20 +112,6 @@ func (o Int16) Kind() Kind { return KindInt16 } -func (o Int16) Equal(v Object) bool { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return o.Int() == int64(r.Uint()) - } else if r, ok := v.(Float); ok { - return float64(o.Int()) == r.Float() - } else { - return false - } - } else { - return o.Int() == r.Int() - } -} - func (o Int16) Compare(v Object) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { @@ -188,20 +146,6 @@ func (o Int32) Kind() Kind { return KindInt32 } -func (o Int32) Equal(v Object) bool { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return o.Int() == int64(r.Uint()) - } else if r, ok := v.(Float); ok { - return float64(o.Int()) == r.Float() - } else { - return false - } - } else { - return o.Int() == r.Int() - } -} - func (o Int32) Compare(v Object) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { @@ -236,20 +180,6 @@ func (o Int64) Kind() Kind { return KindInt64 } -func (o Int64) Equal(v Object) bool { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return o.Int() == int64(r.Uint()) - } else if r, ok := v.(Float); ok { - return float64(o.Int()) == r.Float() - } else { - return false - } - } else { - return o.Int() == r.Int() - } -} - func (o Int64) Compare(v Object) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { diff --git a/pkg/primitive/int_test.go b/pkg/primitive/int_test.go index 18209da7..30c1e866 100644 --- a/pkg/primitive/int_test.go +++ b/pkg/primitive/int_test.go @@ -39,34 +39,6 @@ func TestNewInt(t *testing.T) { }) } -func TestInt_Equal(t *testing.T) { - t.Run("", func(t *testing.T) { - assert.True(t, NewInt(0).Equal(NewInt(0))) - assert.False(t, NewInt(0).Equal(NewInt(1))) - assert.True(t, NewInt(0).Equal(NewFloat32(0))) - }) - t.Run("8", func(t *testing.T) { - assert.True(t, NewInt8(0).Equal(NewInt(0))) - assert.False(t, NewInt8(0).Equal(NewInt(1))) - assert.True(t, NewInt8(0).Equal(NewFloat32(0))) - }) - t.Run("16", func(t *testing.T) { - assert.True(t, NewInt16(0).Equal(NewInt(0))) - assert.False(t, NewInt16(0).Equal(NewInt(1))) - assert.True(t, NewInt16(0).Equal(NewFloat32(0))) - }) - t.Run("32", func(t *testing.T) { - assert.True(t, NewInt32(0).Equal(NewInt(0))) - assert.False(t, NewInt32(0).Equal(NewInt(1))) - assert.True(t, NewInt32(0).Equal(NewFloat32(0))) - }) - t.Run("64", func(t *testing.T) { - assert.True(t, NewInt64(0).Equal(NewInt(0))) - assert.False(t, NewInt64(0).Equal(NewInt(1))) - assert.True(t, NewInt64(0).Equal(NewFloat32(0))) - }) -} - func TestInt_Compare(t *testing.T) { t.Run("", func(t *testing.T) { assert.Equal(t, 0, NewInt(0).Compare(NewInt(0))) diff --git a/pkg/primitive/map.go b/pkg/primitive/map.go index 82a213b2..06d8767e 100644 --- a/pkg/primitive/map.go +++ b/pkg/primitive/map.go @@ -105,38 +105,6 @@ func (o *Map) Kind() Kind { return KindMap } -func (o *Map) Equal(v Object) bool { - if r, ok := v.(*Map); !ok { - return false - } else if o.Len() != r.Len() { - return false - } else { - keys1 := o.Keys() - keys2 := r.Keys() - - for i, k1 := range keys1 { - k2 := keys2[i] - if !Equal(k1, k2) { - return false - } - - v1, ok1 := o.Get(k1) - v2, ok2 := o.Get(k2) - if ok1 != ok2 { - return false - } - if !ok1 || !ok2 { - continue - } - if !Equal(v1, v2) { - return false - } - } - - return true - } -} - func (o *Map) Compare(v Object) int { if r, ok := v.(*Map); !ok { if o.Kind() > v.Kind() { diff --git a/pkg/primitive/map_test.go b/pkg/primitive/map_test.go index b949bd3d..c2a867bf 100644 --- a/pkg/primitive/map_test.go +++ b/pkg/primitive/map_test.go @@ -17,16 +17,6 @@ func TestNewMap(t *testing.T) { assert.Equal(t, map[string]string{k1.String(): v1.String()}, o.Interface()) } -func TestMap_Equal(t *testing.T) { - k1 := NewString(faker.Word()) - k2 := NewString(faker.Word()) - v1 := NewString(faker.Word()) - v2 := NewString(faker.Word()) - - assert.True(t, NewMap(k1, v1).Equal(NewMap(k1, v1))) - assert.False(t, NewMap(k1, v1).Equal(NewMap(k2, v2))) -} - func TestMap_GetAndSetAndDelete(t *testing.T) { k1 := NewString(faker.Word()) v1 := NewString(faker.Word()) diff --git a/pkg/primitive/object.go b/pkg/primitive/object.go index 1bc13b02..f216b51f 100644 --- a/pkg/primitive/object.go +++ b/pkg/primitive/object.go @@ -4,7 +4,6 @@ type ( // Object is an atomic type. Object interface { Kind() Kind - Equal(v Object) bool Compare(v Object) int Interface() any } @@ -33,18 +32,6 @@ const ( KindString ) -func Equal(x, y Object) bool { - if x == nil && y == nil { - return true - } else if x == nil { - return false - } else if y == nil { - return false - } else { - return x.Equal(y) - } -} - func Compare(x, y Object) int { if x == nil && y == nil { return 0 diff --git a/pkg/primitive/slice.go b/pkg/primitive/slice.go index 2d626b5e..5f9ca066 100644 --- a/pkg/primitive/slice.go +++ b/pkg/primitive/slice.go @@ -78,21 +78,6 @@ func (o *Slice) Kind() Kind { return KindSlice } -func (o *Slice) Equal(v Object) bool { - if r, ok := v.(*Slice); !ok { - return false - } else if o.Len() != r.Len() { - return false - } else { - for i := 0; i < o.Len(); i++ { - if !Equal(o.Get(i), r.Get(i)) { - return false - } - } - return true - } -} - func (o *Slice) Compare(v Object) int { if r, ok := v.(*Slice); !ok { if o.Kind() > v.Kind() { diff --git a/pkg/primitive/slice_test.go b/pkg/primitive/slice_test.go index 0e511887..8a48381a 100644 --- a/pkg/primitive/slice_test.go +++ b/pkg/primitive/slice_test.go @@ -62,14 +62,6 @@ func TestSlice_Sub(t *testing.T) { assert.Equal(t, 1, o.Len()) } -func TestSlice_Equal(t *testing.T) { - v1 := NewString(faker.Word()) - v2 := NewString(faker.Word()) - - assert.True(t, NewSlice(v1, v2).Equal(NewSlice(v1, v2))) - assert.False(t, NewSlice(v1, v2).Equal(NewSlice(v2, v1))) -} - func TestSlice_Compare(t *testing.T) { v1 := NewString("1") v2 := NewString("2") diff --git a/pkg/primitive/string.go b/pkg/primitive/string.go index cefc2008..5153f7b8 100644 --- a/pkg/primitive/string.go +++ b/pkg/primitive/string.go @@ -40,14 +40,6 @@ func (o String) Kind() Kind { return KindString } -func (o String) Equal(v Object) bool { - if r, ok := v.(String); !ok { - return false - } else { - return o.String() == r.String() - } -} - func (o String) Compare(v Object) int { if r, ok := v.(String); !ok { if o.Kind() > v.Kind() { diff --git a/pkg/primitive/string_test.go b/pkg/primitive/string_test.go index 19212369..8f99a39a 100644 --- a/pkg/primitive/string_test.go +++ b/pkg/primitive/string_test.go @@ -21,12 +21,6 @@ func TestString_Get(t *testing.T) { assert.Equal(t, rune('A'), v.Get(0)) } -func TestString_Equal(t *testing.T) { - assert.True(t, NewString("A").Equal(NewString("A"))) - assert.True(t, NewString("").Equal(NewString(""))) - assert.False(t, NewString("A").Equal(NewString(""))) -} - func TestString_Compare(t *testing.T) { assert.Equal(t, 0, NewString("A").Compare(NewString("A"))) assert.Equal(t, 1, NewString("a").Compare(NewString("A"))) diff --git a/pkg/primitive/uint.go b/pkg/primitive/uint.go index a7602764..7e30c0fb 100644 --- a/pkg/primitive/uint.go +++ b/pkg/primitive/uint.go @@ -44,20 +44,6 @@ func (o Uint) Kind() Kind { return KindUint } -func (o Uint) Equal(v Object) bool { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return int64(o.Uint()) == r.Int() - } else if r, ok := v.(Float); ok { - return float64(o.Uint()) == r.Float() - } else { - return false - } - } else { - return o.Uint() == r.Uint() - } -} - func (o Uint) Compare(v Object) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { @@ -92,20 +78,6 @@ func (o Uint8) Kind() Kind { return KindUint8 } -func (o Uint8) Equal(v Object) bool { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return int64(o.Uint()) == r.Int() - } else if r, ok := v.(Float); ok { - return float64(o.Uint()) == r.Float() - } else { - return false - } - } else { - return o.Uint() == r.Uint() - } -} - func (o Uint8) Compare(v Object) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { @@ -140,20 +112,6 @@ func (o Uint16) Kind() Kind { return KindUint16 } -func (o Uint16) Equal(v Object) bool { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return int64(o.Uint()) == r.Int() - } else if r, ok := v.(Float); ok { - return float64(o.Uint()) == r.Float() - } else { - return false - } - } else { - return o.Uint() == r.Uint() - } -} - func (o Uint16) Compare(v Object) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { @@ -188,20 +146,6 @@ func (o Uint32) Kind() Kind { return KindUint32 } -func (o Uint32) Equal(v Object) bool { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return int64(o.Uint()) == r.Int() - } else if r, ok := v.(Float); ok { - return float64(o.Uint()) == r.Float() - } else { - return false - } - } else { - return o.Uint() == r.Uint() - } -} - func (o Uint32) Compare(v Object) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { @@ -236,20 +180,6 @@ func (o Uint64) Kind() Kind { return KindUint64 } -func (o Uint64) Equal(v Object) bool { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return int64(o.Uint()) == r.Int() - } else if r, ok := v.(Float); ok { - return float64(o.Uint()) == r.Float() - } else { - return false - } - } else { - return o.Uint() == r.Uint() - } -} - func (o Uint64) Compare(v Object) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { diff --git a/pkg/primitive/uint_test.go b/pkg/primitive/uint_test.go index 7a0e2ed0..c871acdf 100644 --- a/pkg/primitive/uint_test.go +++ b/pkg/primitive/uint_test.go @@ -39,34 +39,6 @@ func TestNewUint(t *testing.T) { }) } -func TestUint_Equal(t *testing.T) { - t.Run("", func(t *testing.T) { - assert.True(t, NewInt(0).Equal(NewUint(0))) - assert.False(t, NewUint(0).Equal(NewUint(1))) - assert.True(t, NewUint(0).Equal(NewFloat32(0))) - }) - t.Run("8", func(t *testing.T) { - assert.True(t, NewUint8(0).Equal(NewUint(0))) - assert.False(t, NewUint8(0).Equal(NewUint(1))) - assert.True(t, NewUint8(0).Equal(NewFloat32(0))) - }) - t.Run("16", func(t *testing.T) { - assert.True(t, NewUint16(0).Equal(NewUint(0))) - assert.False(t, NewUint16(0).Equal(NewUint(1))) - assert.True(t, NewUint16(0).Equal(NewFloat32(0))) - }) - t.Run("32", func(t *testing.T) { - assert.True(t, NewUint32(0).Equal(NewUint(0))) - assert.False(t, NewUint32(0).Equal(NewUint(1))) - assert.True(t, NewUint32(0).Equal(NewFloat32(0))) - }) - t.Run("64", func(t *testing.T) { - assert.True(t, NewUint64(0).Equal(NewUint(0))) - assert.False(t, NewUint64(0).Equal(NewUint(1))) - assert.True(t, NewUint64(0).Equal(NewFloat32(0))) - }) -} - func TestUint_Compare(t *testing.T) { t.Run("", func(t *testing.T) { assert.Equal(t, 0, NewUint(0).Compare(NewUint(0)))