mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-23 00:03:36 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9cf3551c20 | |||
| 3035d8d571 | |||
| eac50d1770 | |||
| 05ebb25e70 | |||
| fd53c66690 | |||
| 6111341a3c |
+21
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright © 2020 Nikita Tokarchuk
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetID returns source document id
|
// GetID returns source document id
|
||||||
@@ -62,7 +63,7 @@ func getObjectOrPanic(source mongox.JSONBased) (id primitive.D) {
|
|||||||
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
|
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
|
||||||
|
|
||||||
id = source.GetID()
|
id = source.GetID()
|
||||||
if id != nil {
|
if !utils.IsNil(id) {
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ func (d *Database) createAggregateLoad(target interface{}, composed *query.Query
|
|||||||
pipeline := primitive.A{}
|
pipeline := primitive.A{}
|
||||||
|
|
||||||
if !composed.Empty() {
|
if !composed.Empty() {
|
||||||
pipeline = append(pipeline, primitive.M{"$match": primitive.M{"$expr": composed.M()}})
|
pipeline = append(pipeline, primitive.M{"$match": composed.M()})
|
||||||
}
|
}
|
||||||
if composed.Sorter() != nil {
|
if composed.Sorter() != nil {
|
||||||
pipeline = append(pipeline, primitive.M{"$sort": composed.Sorter()})
|
pipeline = append(pipeline, primitive.M{"$sort": composed.Sorter()})
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"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/base"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"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
|
// 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()
|
opts.Sort = composed.Sorter()
|
||||||
|
|
||||||
if target != nil {
|
if !utils.IsNil(target) {
|
||||||
composed.And(primitive.M{"_id": base.GetID(target)})
|
composed.And(primitive.M{"_id": base.GetID(target)})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,15 +64,15 @@ func (d *Database) IndexEnsure(cfg interface{}, document interface{}) error {
|
|||||||
panic(fmt.Errorf("cannot evaluate index key"))
|
panic(fmt.Errorf("cannot evaluate index key"))
|
||||||
}
|
}
|
||||||
|
|
||||||
index := primitive.M{key: 1}
|
|
||||||
opts := &options.IndexOptions{
|
opts := &options.IndexOptions{
|
||||||
Background: &f,
|
Background: &f,
|
||||||
Unique: &f,
|
Unique: &f,
|
||||||
Name: &name,
|
Name: &name,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
index := primitive.D{{Key: key, Value: 1}}
|
||||||
if indexValues[0] == "-" {
|
if indexValues[0] == "-" {
|
||||||
index[key] = -1
|
index = primitive.D{{Key: key, Value: -1}}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, prop := range indexValues[1:] {
|
for _, prop := range indexValues[1:] {
|
||||||
@@ -114,9 +114,9 @@ func (d *Database) IndexEnsure(cfg interface{}, document interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if compoundValue[0] == '-' {
|
if compoundValue[0] == '-' {
|
||||||
index[compoundValue[1:]] = -1
|
index = append(index, primitive.E{compoundValue[1:], -1})
|
||||||
} else {
|
} else {
|
||||||
index[compoundValue] = 1
|
index = append(index, primitive.E{compoundValue, 1})
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"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/base/protection"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Compose is a function to compose filters into a single query
|
// 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
|
// Push applies single filter to a query
|
||||||
func Push(q *Query, f interface{}) bool {
|
func Push(q *Query, f interface{}) bool {
|
||||||
|
|
||||||
if f == nil {
|
if utils.IsNil(f) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package query
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Query is an enchanched bson.M map
|
// Query is an enchanched bson.M map
|
||||||
@@ -82,11 +80,7 @@ func (q *Query) Preloader() (empty bool, preloader []string) {
|
|||||||
|
|
||||||
// Empty checks the query for any content
|
// Empty checks the query for any content
|
||||||
func (q *Query) Empty() bool {
|
func (q *Query) Empty() bool {
|
||||||
|
return len(q.m) == 0
|
||||||
qv := reflect.ValueOf(q.m)
|
|
||||||
keys := qv.MapKeys()
|
|
||||||
|
|
||||||
return len(keys) == 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// M returns underlying query map
|
// M returns underlying query map
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsNil function evaluates the interface value to nil
|
||||||
|
func IsNil(i interface{}) bool {
|
||||||
|
|
||||||
|
type iface struct {
|
||||||
|
_ unsafe.Pointer
|
||||||
|
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