Skip to content

Commit

Permalink
refactor: remove equal
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
1 parent 03f2515 commit 9ec82e7
Show file tree
Hide file tree
Showing 19 changed files with 5 additions and 360 deletions.
8 changes: 4 additions & 4 deletions pkg/database/memdb/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/memdb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 0 additions & 15 deletions pkg/primitive/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 0 additions & 8 deletions pkg/primitive/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
9 changes: 0 additions & 9 deletions pkg/primitive/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 0 additions & 7 deletions pkg/primitive/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
14 changes: 0 additions & 14 deletions pkg/primitive/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions pkg/primitive/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
70 changes: 0 additions & 70 deletions pkg/primitive/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 0 additions & 28 deletions pkg/primitive/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
32 changes: 0 additions & 32 deletions pkg/primitive/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 0 additions & 10 deletions pkg/primitive/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
13 changes: 0 additions & 13 deletions pkg/primitive/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ type (
// Object is an atomic type.
Object interface {
Kind() Kind
Equal(v Object) bool
Compare(v Object) int
Interface() any
}
Expand Down Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions pkg/primitive/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 9ec82e7

Please sign in to comment.