mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Check for nil interface correctly
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"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
|
||||
@@ -62,7 +63,7 @@ func getObjectOrPanic(source mongox.JSONBased) (id primitive.D) {
|
||||
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != nil {
|
||||
if !utils.IsNil(id) {
|
||||
return id
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"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 +23,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
||||
|
||||
opts.Sort = composed.Sorter()
|
||||
|
||||
if target != nil {
|
||||
if !utils.IsNil(target) {
|
||||
composed.And(primitive.M{"_id": base.GetID(target)})
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"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
|
||||
@@ -26,7 +27,7 @@ func Compose(filters ...interface{}) *Query {
|
||||
// Push applies single filter to a query
|
||||
func Push(q *Query, f interface{}) bool {
|
||||
|
||||
if f == nil {
|
||||
if utils.IsNil(f) {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// IsNil function evaluates the interface value to nil
|
||||
func IsNil(i interface{}) bool {
|
||||
|
||||
type iface struct {
|
||||
_ *interface{}
|
||||
ptr unsafe.Pointer
|
||||
}
|
||||
|
||||
unpacked := (*iface)(unsafe.Pointer(&i))
|
||||
if unpacked.ptr == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return *(*unsafe.Pointer)(unpacked.ptr) == nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user