Use reflect2

This commit is contained in:
Nikita Tokarchuk
2020-07-22 04:45:32 +02:00
parent b22b0f0919
commit 9fb3094b87
7 changed files with 15 additions and 61 deletions
+2 -2
View File
@@ -3,10 +3,10 @@ package base
import (
"fmt"
"github.com/modern-go/reflect2"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
)
// GetID returns source document id
@@ -63,7 +63,7 @@ func getObjectOrPanic(source mongox.JSONBased) (id primitive.D) {
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
id = source.GetID()
if !utils.IsNil(id) {
if !reflect2.IsNil(id) {
return id
}
+2 -2
View File
@@ -4,12 +4,12 @@ import (
"fmt"
"time"
"github.com/modern-go/reflect2"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
)
// DeleteOne removes a document from a database and then returns it into target
@@ -22,7 +22,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) (err er
opts.Sort = composed.Sorter()
if !utils.IsNil(target) {
if !reflect2.IsNil(target) {
composed.And(primitive.M{"_id": base.GetID(target)})
}
+2 -2
View File
@@ -3,11 +3,11 @@ package query
import (
"fmt"
"github.com/modern-go/reflect2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
)
// Compose is a function to compose filters into a single query
@@ -27,7 +27,7 @@ func Compose(filters ...interface{}) (query *Query) {
// Push applies single filter to a query
func Push(q *Query, f interface{}) (ok bool) {
if utils.IsNil(f) {
if reflect2.IsNil(f) {
return true
}
-23
View File
@@ -1,23 +0,0 @@
package utils
import (
"unsafe"
)
// IsNil function evaluates the interface value to nil
func IsNil(i interface{}) (isNil bool) {
type iface struct {
_ unsafe.Pointer
ptr unsafe.Pointer
}
unpacked := (*iface)(unsafe.Pointer(&i))
if unpacked.ptr == nil {
isNil = true
return
}
isNil = *(*unsafe.Pointer)(unpacked.ptr) == nil
return
}
-32
View File
@@ -1,32 +0,0 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsNil(t *testing.T) {
testvalues := []struct {
i interface{}
isnil bool
}{
{nil, true},
{(*string)(nil), true},
{([]string)(nil), true},
{(map[string]string)(nil), true},
{(func() bool)(nil), true},
{(chan func() bool)(nil), true},
{"", true},
{0, true},
{append(([]string)(nil), ""), false},
{[]string{}, false},
{1, false},
{"1", false},
}
for _, tt := range testvalues {
assert.Equal(t, tt.isnil, IsNil(tt.i))
}
}