mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-23 00:03:36 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 08c3c5b377 | |||
| 09fa64ab0e | |||
| c019a0ea4b | |||
| ee1b0e17d5 | |||
| 22a1d7033f | |||
| 1d3e29fe10 | |||
| 72e74a65b6 | |||
| 9cf3551c20 | |||
| 3035d8d571 | |||
| eac50d1770 | |||
| 05ebb25e70 | |||
| fd53c66690 | |||
| 6111341a3c | |||
| 9f647ca094 | |||
| eeb83daf4b | |||
| 6e6a042a16 | |||
| e0c26f770f | |||
| e7a05d94e1 |
+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.
|
||||||
@@ -30,6 +30,6 @@ func NewEphemeral(URI string) (db *EphemeralDatabase, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close the connection and drop database
|
// Close the connection and drop database
|
||||||
func (e *EphemeralDatabase) Close() error {
|
func (e *EphemeralDatabase) Close() (err error) {
|
||||||
return e.Client().Database(e.Name()).Drop(e.Context())
|
return e.Client().Database(e.Name()).Drop(e.Context())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetProtection function finds protection field in the source document otherwise returns nil
|
// GetProtection function finds protection field in the source document otherwise returns nil
|
||||||
func GetProtection(source interface{}) *protection.Key {
|
func GetProtection(source interface{}) (key *protection.Key) {
|
||||||
|
|
||||||
v := reflect.ValueOf(source)
|
v := reflect.ValueOf(source)
|
||||||
if v.Kind() != reflect.Ptr || v.IsNil() {
|
if v.Kind() != reflect.Ptr || v.IsNil() {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
el := v.Elem()
|
el := v.Elem()
|
||||||
@@ -19,17 +19,22 @@ func GetProtection(source interface{}) *protection.Key {
|
|||||||
|
|
||||||
for i := 0; i < numField; i++ {
|
for i := 0; i < numField; i++ {
|
||||||
field := el.Field(i)
|
field := el.Field(i)
|
||||||
|
if !field.CanInterface() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
switch field.Interface().(type) {
|
switch field.Interface().(type) {
|
||||||
case *protection.Key:
|
case *protection.Key:
|
||||||
return field.Interface().(*protection.Key)
|
key = field.Interface().(*protection.Key)
|
||||||
case protection.Key:
|
case protection.Key:
|
||||||
ptr := field.Addr()
|
ptr := field.Addr()
|
||||||
return ptr.Interface().(*protection.Key)
|
key = ptr.Interface().(*protection.Key)
|
||||||
default:
|
default:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type Primary struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetID returns an _id
|
// GetID returns an _id
|
||||||
func (p *Primary) GetID() primitive.D {
|
func (p *Primary) GetID() (id primitive.D) {
|
||||||
return p.ID
|
return p.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type Primary struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetID returns an _id
|
// GetID returns an _id
|
||||||
func (p *Primary) GetID() primitive.ObjectID {
|
func (p *Primary) GetID() (id primitive.ObjectID) {
|
||||||
return p.ID
|
return p.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package base
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ func Reset(target interface{}) {
|
|||||||
|
|
||||||
v := reflect.ValueOf(target)
|
v := reflect.ValueOf(target)
|
||||||
if v.Kind() != reflect.Ptr {
|
if v.Kind() != reflect.Ptr {
|
||||||
panic("reset target should be a pointer")
|
panic(fmt.Errorf("reset target should be a pointer"))
|
||||||
}
|
}
|
||||||
|
|
||||||
t := v.Elem().Type()
|
t := v.Elem().Type()
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ type Primary struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetID returns an _id
|
// GetID returns an _id
|
||||||
func (p *Primary) GetID() string {
|
func (p *Primary) GetID() (id string) {
|
||||||
return p.ID
|
return p.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
|
)
|
||||||
|
|
||||||
|
func onDecode(ctx context.Context, iter interface{}, callbacks ...query.OnDecode) (err error) {
|
||||||
|
|
||||||
|
for _, cb := range callbacks {
|
||||||
|
err = cb(ctx, iter)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
@@ -11,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// Count function counts documents in the database by query
|
// Count function counts documents in the database by query
|
||||||
// target is used only to get collection by tag so it'd be better to use nil ptr here
|
// target is used only to get collection by tag so it'd be better to use nil ptr here
|
||||||
func (d *Database) Count(target interface{}, filters ...interface{}) (int64, error) {
|
func (d *Database) Count(target interface{}, filters ...interface{}) (result int64, err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(target)
|
collection := d.GetCollectionOf(target)
|
||||||
opts := options.Count()
|
opts := options.Count()
|
||||||
@@ -20,13 +17,7 @@ func (d *Database) Count(target interface{}, filters ...interface{}) (int64, err
|
|||||||
opts.Limit = composed.Limiter()
|
opts.Limit = composed.Limiter()
|
||||||
opts.Skip = composed.Skipper()
|
opts.Skip = composed.Skipper()
|
||||||
|
|
||||||
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
|
result, err = collection.CountDocuments(d.Context(), composed.M(), opts)
|
||||||
if err == mongo.ErrNoDocuments {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("can't decode desult: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-22
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
@@ -17,48 +16,57 @@ import (
|
|||||||
|
|
||||||
// Database handler
|
// Database handler
|
||||||
type Database struct {
|
type Database struct {
|
||||||
client *mongo.Client
|
client *mongox.Client
|
||||||
dbname string
|
dbname string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDatabase function creates new database instance with mongo client and empty context
|
// NewDatabase function creates new database instance with mongo client and empty context
|
||||||
func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
|
func NewDatabase(client *mongox.Client, dbname string) (db mongox.Database) {
|
||||||
|
|
||||||
db := &Database{}
|
db = &Database{
|
||||||
db.client = client
|
client: client,
|
||||||
db.dbname = dbname
|
dbname: dbname,
|
||||||
|
}
|
||||||
|
|
||||||
return db
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client function returns a mongo client
|
// Client function returns a mongo client
|
||||||
func (d *Database) Client() *mongo.Client {
|
func (d *Database) Client() (client *mongox.Client) {
|
||||||
return d.client
|
return d.client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context function returns a context
|
// Context function returns a context
|
||||||
func (d *Database) Context() context.Context {
|
func (d *Database) Context() (ctx context.Context) {
|
||||||
return d.ctx
|
|
||||||
|
ctx = d.ctx
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name function returns a database name
|
// Name function returns a database name
|
||||||
func (d *Database) Name() string {
|
func (d *Database) Name() (name string) {
|
||||||
return d.dbname
|
return d.dbname
|
||||||
}
|
}
|
||||||
|
|
||||||
// New function creates new database context with same client
|
// New function creates new database context with same client
|
||||||
func (d *Database) New(ctx context.Context) mongox.Database {
|
func (d *Database) New(ctx context.Context) (db mongox.Database) {
|
||||||
|
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Database{
|
db = &Database{
|
||||||
client: d.client,
|
client: d.client,
|
||||||
dbname: d.dbname,
|
dbname: d.dbname,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCollectionOf returns the collection object by the «collection» tag of the given document;
|
// GetCollectionOf returns the collection object by the «collection» tag of the given document;
|
||||||
@@ -67,7 +75,7 @@ func (d *Database) New(ctx context.Context) mongox.Database {
|
|||||||
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
|
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
|
||||||
// ...
|
// ...
|
||||||
// Will panic if there is no «collection» tag
|
// Will panic if there is no «collection» tag
|
||||||
func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
|
func (d *Database) GetCollectionOf(document interface{}) (collection *mongox.Collection) {
|
||||||
|
|
||||||
el := reflect.TypeOf(document).Elem()
|
el := reflect.TypeOf(document).Elem()
|
||||||
numField := el.NumField()
|
numField := el.NumField()
|
||||||
@@ -86,7 +94,7 @@ func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
|
|||||||
panic(fmt.Errorf("document %v does not have a collection tag", document))
|
panic(fmt.Errorf("document %v does not have a collection tag", document))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (cursor *mongox.Cursor, err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(target)
|
collection := d.GetCollectionOf(target)
|
||||||
opts := options.Find()
|
opts := options.Find()
|
||||||
@@ -98,7 +106,7 @@ func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (
|
|||||||
return collection.Find(d.Context(), composed.M(), opts)
|
return collection.Find(d.Context(), composed.M(), opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongox.Cursor, err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(target)
|
collection := d.GetCollectionOf(target)
|
||||||
opts := options.Aggregate()
|
opts := options.Aggregate()
|
||||||
@@ -106,7 +114,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()})
|
||||||
@@ -132,9 +140,9 @@ func (d *Database) createAggregateLoad(target interface{}, composed *query.Query
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
jsonTag, ok := tag.Lookup("json")
|
jsonTag, _ := tag.Lookup("json")
|
||||||
if jsonTag == "-" {
|
if jsonTag == "-" {
|
||||||
return nil, fmt.Errorf("preload private field is impossible")
|
panic(fmt.Errorf("preload private field is impossible"))
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData := strings.SplitN(jsonTag, ",", 2)
|
jsonData := strings.SplitN(jsonTag, ",", 2)
|
||||||
@@ -148,7 +156,7 @@ func (d *Database) createAggregateLoad(target interface{}, composed *query.Query
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(preloadData) == 1 {
|
if len(preloadData) == 1 {
|
||||||
panic("there is no foreign field")
|
panic(fmt.Errorf("there is no foreign field"))
|
||||||
}
|
}
|
||||||
|
|
||||||
localField := strings.TrimSpace(preloadData[0])
|
localField := strings.TrimSpace(preloadData[0])
|
||||||
@@ -158,7 +166,7 @@ func (d *Database) createAggregateLoad(target interface{}, composed *query.Query
|
|||||||
|
|
||||||
foreignField := strings.TrimSpace(preloadData[1])
|
foreignField := strings.TrimSpace(preloadData[1])
|
||||||
if len(foreignField) == 0 {
|
if len(foreignField) == 0 {
|
||||||
panic("there is no foreign field")
|
panic(fmt.Errorf("there is no foreign field"))
|
||||||
}
|
}
|
||||||
|
|
||||||
preloadLimiter := 100
|
preloadLimiter := 100
|
||||||
@@ -190,7 +198,7 @@ func (d *Database) createAggregateLoad(target interface{}, composed *query.Query
|
|||||||
typ = typ.Elem()
|
typ = typ.Elem()
|
||||||
}
|
}
|
||||||
if typ.Kind() != reflect.Ptr {
|
if typ.Kind() != reflect.Ptr {
|
||||||
panic("preload field should have ptr type")
|
panic(fmt.Errorf("preload field should have ptr type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
lookupCollection := d.GetCollectionOf(reflect.Zero(typ).Interface())
|
lookupCollection := d.GetCollectionOf(reflect.Zero(typ).Interface())
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// DeleteArray removes documents list from a database by their ids
|
// DeleteArray removes documents list from a database by their ids
|
||||||
func (d *Database) DeleteArray(target interface{}) error {
|
func (d *Database) DeleteArray(target interface{}) (err error) {
|
||||||
|
|
||||||
targetV := reflect.ValueOf(target)
|
targetV := reflect.ValueOf(target)
|
||||||
targetT := targetV.Type()
|
targetT := targetV.Type()
|
||||||
@@ -49,11 +49,11 @@ func (d *Database) DeleteArray(target interface{}) error {
|
|||||||
|
|
||||||
result, err := collection.DeleteMany(d.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts)
|
result, err := collection.DeleteMany(d.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't create find and delete result: %w", err)
|
return
|
||||||
}
|
}
|
||||||
if result.DeletedCount != int64(targetLen) {
|
if result.DeletedCount != int64(targetLen) {
|
||||||
return fmt.Errorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen)
|
err = fmt.Errorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
"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
|
||||||
func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
func (d *Database) DeleteOne(target interface{}, filters ...interface{}) (err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(target)
|
collection := d.GetCollectionOf(target)
|
||||||
opts := &options.FindOneAndDeleteOptions{}
|
opts := &options.FindOneAndDeleteOptions{}
|
||||||
@@ -22,7 +22,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)})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,13 +37,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
|||||||
return fmt.Errorf("can't create find one and delete result: %w", result.Err())
|
return fmt.Errorf("can't create find one and delete result: %w", result.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
err := result.Decode(target)
|
err = result.Decode(target)
|
||||||
if err == mongo.ErrNoDocuments {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("can't decode result: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IndexEnsure function ensures index in mongo collection of document
|
||||||
|
// `index:""` -- https://docs.mongodb.com/manual/indexes/#create-an-index
|
||||||
|
// `index:"-"` -- (descending)
|
||||||
|
// `index:"-,+foo,+-bar"` -- https://docs.mongodb.com/manual/core/index-compound
|
||||||
|
// `index:"-,+foo,+-bar,unique"` -- https://docs.mongodb.com/manual/core/index-unique
|
||||||
|
// `index:"-,+foo,+-bar,unique,allowNull"` -- https://docs.mongodb.com/manual/core/index-partial
|
||||||
|
// `index:"-,unique,allowNull,expireAfter=86400"` -- https://docs.mongodb.com/manual/core/index-ttl
|
||||||
|
// `index:"-,unique,allowNull,expireAfter={{.Expire}}"` -- evaluate index as a golang template with `cfg` arguments
|
||||||
|
func (d *Database) IndexEnsure(cfg interface{}, document interface{}) (err error) {
|
||||||
|
|
||||||
|
el := reflect.ValueOf(document).Elem().Type()
|
||||||
|
numField := el.NumField()
|
||||||
|
documents := d.GetCollectionOf(document)
|
||||||
|
|
||||||
|
for i := 0; i < numField; i++ {
|
||||||
|
|
||||||
|
field := el.Field(i)
|
||||||
|
tag := field.Tag
|
||||||
|
|
||||||
|
indexTag, ok := tag.Lookup("index")
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bsonTag, ok := tag.Lookup("bson")
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("bson tag is not defined for field:%v document:%v", field, document)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmpBuffer = &bytes.Buffer{}
|
||||||
|
var tpl *template.Template
|
||||||
|
|
||||||
|
tpl, err = template.New("").Parse(indexTag)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("invalid prop template %v, err:%w", indexTag, err))
|
||||||
|
}
|
||||||
|
err = tpl.Execute(tmpBuffer, cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to evaluate prop template %v, err:%w", indexTag, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
indexString := tmpBuffer.String()
|
||||||
|
indexValues := strings.Split(indexString, ",")
|
||||||
|
bsonValues := strings.Split(bsonTag, ",")
|
||||||
|
|
||||||
|
var f = false
|
||||||
|
var t = true
|
||||||
|
var key = bsonValues[0]
|
||||||
|
var name = fmt.Sprintf("%s_%s_", indexString, key)
|
||||||
|
|
||||||
|
if len(key) == 0 {
|
||||||
|
panic(fmt.Errorf("cannot evaluate index key"))
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := &options.IndexOptions{
|
||||||
|
Background: &f,
|
||||||
|
Unique: &f,
|
||||||
|
Name: &name,
|
||||||
|
}
|
||||||
|
|
||||||
|
index := primitive.D{{Key: key, Value: 1}}
|
||||||
|
if indexValues[0] == "-" {
|
||||||
|
index = primitive.D{{Key: key, Value: -1}}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, prop := range indexValues[1:] {
|
||||||
|
var left string
|
||||||
|
var right string
|
||||||
|
|
||||||
|
pair := strings.SplitN(prop, "=", 2)
|
||||||
|
left = pair[0]
|
||||||
|
if len(pair) > 1 {
|
||||||
|
right = pair[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case left == "unique":
|
||||||
|
opts.Unique = &t
|
||||||
|
|
||||||
|
case left == "allowNull":
|
||||||
|
expression, isMap := opts.PartialFilterExpression.(primitive.M)
|
||||||
|
if !isMap || expression == nil {
|
||||||
|
expression = primitive.M{}
|
||||||
|
}
|
||||||
|
|
||||||
|
expression[key] = primitive.M{"$exists": true}
|
||||||
|
opts.PartialFilterExpression = expression
|
||||||
|
|
||||||
|
case left == "expireAfter":
|
||||||
|
expireAfter, err := strconv.Atoi(right)
|
||||||
|
if err != nil || expireAfter < 1 {
|
||||||
|
panic(fmt.Errorf("invalid expireAfter value, err: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
expireAfterInt32 := int32(expireAfter)
|
||||||
|
opts.ExpireAfterSeconds = &expireAfterInt32
|
||||||
|
|
||||||
|
case len(left) > 0 && left[0] == '+':
|
||||||
|
compoundValue := left[1:]
|
||||||
|
if len(compoundValue) == 0 {
|
||||||
|
panic(fmt.Errorf("invalid compound value"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if compoundValue[0] == '-' {
|
||||||
|
index = append(index, primitive.E{compoundValue[1:], -1})
|
||||||
|
} else {
|
||||||
|
index = append(index, primitive.E{compoundValue, 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic(fmt.Errorf("unsupported flag:%q in tag:%q of type:%s", prop, tag, el))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = documents.Indexes().CreateOne(d.Context(), mongo.IndexModel{Keys: index, Options: opts})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
package database_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDatabase_Ensure(t *testing.T) {
|
||||||
|
|
||||||
|
db, err := database.NewEphemeral("mongodb://localhost")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
testvalues := []struct {
|
||||||
|
doc interface{}
|
||||||
|
settings map[string]interface{}
|
||||||
|
index map[string]interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-,unique,allowNull,expireAfter=86400"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"expireAfterSeconds": int32(86400),
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-,unique,allowNull,expireAfter=86400_foobar_",
|
||||||
|
"partialFilterExpression": map[string]interface{}{
|
||||||
|
"foobar": map[string]interface{}{"$exists": true},
|
||||||
|
},
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"2"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:",unique"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(1),
|
||||||
|
},
|
||||||
|
"name": ",unique_foobar_",
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"3"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-,+foo,+-bar,unique,allowNull"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
"foo": int32(1),
|
||||||
|
"bar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-,+foo,+-bar,unique,allowNull_foobar_",
|
||||||
|
"partialFilterExpression": map[string]interface{}{
|
||||||
|
"foobar": map[string]interface{}{"$exists": true},
|
||||||
|
},
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"4"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:""`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(1),
|
||||||
|
},
|
||||||
|
"name": "_foobar_",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"5"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-_foobar_",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-,unique,allowNull,expireAfter={{.Expire}}"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
settings: map[string]interface{}{
|
||||||
|
"Expire": 86400,
|
||||||
|
},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"expireAfterSeconds": int32(86400),
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-,unique,allowNull,expireAfter=86400_foobar_",
|
||||||
|
"partialFilterExpression": map[string]interface{}{
|
||||||
|
"foobar": map[string]interface{}{"$exists": true},
|
||||||
|
},
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testvalues {
|
||||||
|
err = db.IndexEnsure(tt.settings, tt.doc)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
indexes, _ := db.GetCollectionOf(tt.doc).Indexes().List(db.Context())
|
||||||
|
index := new(map[string]interface{})
|
||||||
|
|
||||||
|
indexes.Next(db.Context()) // skip _id_
|
||||||
|
indexes.Next(db.Context())
|
||||||
|
indexes.Decode(&index)
|
||||||
|
|
||||||
|
for k, v := range tt.index {
|
||||||
|
assert.Equal(t, v, (*index)[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,14 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadArray loads an array of documents from the database by query
|
// LoadArray loads an array of documents from the database by query
|
||||||
func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
|
func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err error) {
|
||||||
|
|
||||||
targetV := reflect.ValueOf(target)
|
targetV := reflect.ValueOf(target)
|
||||||
targetT := targetV.Type()
|
targetT := targetV.Type()
|
||||||
@@ -36,8 +35,8 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
|
|||||||
zeroElem := reflect.Zero(targetSliceElemT)
|
zeroElem := reflect.Zero(targetSliceElemT)
|
||||||
hasPreloader, _ := composed.Preloader()
|
hasPreloader, _ := composed.Preloader()
|
||||||
|
|
||||||
var result *mongo.Cursor
|
var result *mongox.Cursor
|
||||||
var err error
|
var i int
|
||||||
|
|
||||||
if hasPreloader {
|
if hasPreloader {
|
||||||
result, err = d.createAggregateLoad(zeroElem.Interface(), composed)
|
result, err = d.createAggregateLoad(zeroElem.Interface(), composed)
|
||||||
@@ -45,25 +44,35 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
|
|||||||
result, err = d.createSimpleLoad(zeroElem.Interface(), composed)
|
result, err = d.createSimpleLoad(zeroElem.Interface(), composed)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't create find result: %w", err)
|
err = fmt.Errorf("can't create find result: %w", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var i int
|
|
||||||
|
|
||||||
for i = 0; result.Next(d.Context()); {
|
for i = 0; result.Next(d.Context()); {
|
||||||
|
|
||||||
|
var elem interface{}
|
||||||
|
|
||||||
if targetSliceV.Len() == i {
|
if targetSliceV.Len() == i {
|
||||||
elem := reflect.New(targetSliceElemT.Elem())
|
value := reflect.New(targetSliceElemT.Elem())
|
||||||
if err = result.Decode(elem.Interface()); err == nil {
|
err = result.Decode(value.Interface())
|
||||||
targetSliceV = reflect.Append(targetSliceV, elem)
|
elem = value.Interface()
|
||||||
} else {
|
if err == nil {
|
||||||
continue
|
targetSliceV = reflect.Append(targetSliceV, value)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
elem := targetSliceV.Index(i).Interface()
|
elem = targetSliceV.Index(i).Interface()
|
||||||
base.Reset(elem)
|
base.Reset(elem)
|
||||||
if err = result.Decode(elem); err != nil {
|
err = result.Decode(elem)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
_ = result.Close(d.Context())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = onDecode(d.ctx, elem, composed.OnDecode()...)
|
||||||
|
if err != nil {
|
||||||
|
_ = result.Close(d.Context())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i++
|
i++
|
||||||
|
|||||||
@@ -3,20 +3,18 @@ package database
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadOne function loads a first single target document by a query
|
// LoadOne function loads a first single target document by a query
|
||||||
func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
|
func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err error) {
|
||||||
|
|
||||||
composed := query.Compose(append(filters, query.Limit(1))...)
|
composed := query.Compose(append(filters, query.Limit(1))...)
|
||||||
hasPreloader, _ := composed.Preloader()
|
hasPreloader, _ := composed.Preloader()
|
||||||
|
|
||||||
var result *mongo.Cursor
|
var result *mongox.Cursor
|
||||||
var err error
|
|
||||||
|
|
||||||
if hasPreloader {
|
if hasPreloader {
|
||||||
result, err = d.createAggregateLoad(target, composed)
|
result, err = d.createAggregateLoad(target, composed)
|
||||||
@@ -32,10 +30,20 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !hasNext {
|
if !hasNext {
|
||||||
return mongo.ErrNoDocuments
|
return mongox.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Reset(target)
|
base.Reset(target)
|
||||||
|
|
||||||
return result.Decode(target)
|
err = result.Decode(target)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = onDecode(d.ctx, target, composed.OnDecode()...)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,14 @@ package database
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadStream function loads documents one by one into a target channel
|
// LoadStream function loads documents one by one into a target channel
|
||||||
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
|
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (loader mongox.StreamLoader, err error) {
|
||||||
|
|
||||||
var cursor *mongo.Cursor
|
var cursor *mongox.Cursor
|
||||||
var err error
|
|
||||||
|
|
||||||
composed := query.Compose(filters...)
|
composed := query.Compose(filters...)
|
||||||
hasPreloader, _ := composed.Preloader()
|
hasPreloader, _ := composed.Preloader()
|
||||||
@@ -24,10 +21,11 @@ func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongo
|
|||||||
cursor, err = d.createSimpleLoad(target, composed)
|
cursor, err = d.createSimpleLoad(target, composed)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't create find result: %w", err)
|
err = fmt.Errorf("can't create find result: %w", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target}
|
loader = &StreamLoader{cur: cursor, ctx: d.Context(), target: target, query: composed}
|
||||||
|
|
||||||
return l, nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// SaveOne saves a single source document to the database
|
// SaveOne saves a single source document to the database
|
||||||
func (d *Database) SaveOne(source interface{}) error {
|
func (d *Database) SaveOne(source interface{}) (err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(source)
|
collection := d.GetCollectionOf(source)
|
||||||
opts := options.FindOneAndReplace()
|
opts := options.FindOneAndReplace()
|
||||||
|
|||||||
@@ -2,72 +2,79 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
|
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StreamLoader is a controller for a database cursor
|
// StreamLoader is a controller for a database cursor
|
||||||
type StreamLoader struct {
|
type StreamLoader struct {
|
||||||
*mongo.Cursor
|
cur *mongox.Cursor
|
||||||
|
query *query.Query
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
target interface{}
|
target interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeNext loads next documents to a target or returns an error
|
// DecodeNext loads next documents to a target or returns an error
|
||||||
func (l *StreamLoader) DecodeNext() error {
|
func (l *StreamLoader) DecodeNext() (err error) {
|
||||||
|
|
||||||
hasNext := l.Cursor.Next(l.ctx)
|
err = l.Next()
|
||||||
|
|
||||||
if l.Cursor.Err() != nil {
|
|
||||||
return l.Cursor.Err()
|
|
||||||
}
|
|
||||||
if !hasNext {
|
|
||||||
return mongo.ErrNoDocuments
|
|
||||||
}
|
|
||||||
|
|
||||||
base.Reset(l.target)
|
|
||||||
|
|
||||||
err := l.Cursor.Decode(l.target)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't decode desult: %w", err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
err = l.Decode()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode function decodes the current cursor document into the target
|
// Decode function decodes the current cursor document into the target
|
||||||
func (l *StreamLoader) Decode() error {
|
func (l *StreamLoader) Decode() (err error) {
|
||||||
|
|
||||||
base.Reset(l.target)
|
base.Reset(l.target)
|
||||||
|
|
||||||
err := l.Cursor.Decode(l.target)
|
err = l.cur.Decode(l.target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't decode desult: %w", err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
err = onDecode(l.ctx, l.target, l.query.OnDecode()...)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next loads next documents but doesn't perform decoding
|
// Next loads next documents but doesn't perform decoding
|
||||||
func (l *StreamLoader) Next() error {
|
func (l *StreamLoader) Next() (err error) {
|
||||||
|
|
||||||
hasNext := l.Cursor.Next(l.ctx)
|
hasNext := l.cur.Next(l.ctx)
|
||||||
|
err = l.cur.Err()
|
||||||
|
|
||||||
if l.Cursor.Err() != nil {
|
if err != nil {
|
||||||
return l.Cursor.Err()
|
return
|
||||||
}
|
}
|
||||||
if !hasNext {
|
if !hasNext {
|
||||||
return mongo.ErrNoDocuments
|
err = mongox.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *StreamLoader) Cursor() (cursor *mongox.Cursor) {
|
||||||
|
return l.cur
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close cursor
|
// Close cursor
|
||||||
func (l *StreamLoader) Close() error {
|
func (l *StreamLoader) Close() (err error) {
|
||||||
|
return l.cur.Close(l.ctx)
|
||||||
return l.Cursor.Close(l.ctx)
|
}
|
||||||
|
|
||||||
|
func (l *StreamLoader) Err() (err error) {
|
||||||
|
return l.cur.Err()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package mongox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reexported mongo errors
|
||||||
|
var (
|
||||||
|
ErrMissingResumeToken = mongo.ErrMissingResumeToken
|
||||||
|
ErrNilCursor = mongo.ErrNilCursor
|
||||||
|
ErrUnacknowledgedWrite = mongo.ErrUnacknowledgedWrite
|
||||||
|
ErrClientDisconnected = mongo.ErrClientDisconnected
|
||||||
|
ErrNilDocument = mongo.ErrNilDocument
|
||||||
|
ErrEmptySlice = mongo.ErrEmptySlice
|
||||||
|
ErrInvalidIndexValue = mongo.ErrInvalidIndexValue
|
||||||
|
ErrNonStringIndexName = mongo.ErrNonStringIndexName
|
||||||
|
ErrMultipleIndexDrop = mongo.ErrMultipleIndexDrop
|
||||||
|
ErrWrongClient = mongo.ErrWrongClient
|
||||||
|
ErrNoDocuments = mongo.ErrNoDocuments
|
||||||
|
)
|
||||||
+30
-21
@@ -7,51 +7,60 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Reexport basic mongo structs
|
||||||
|
type (
|
||||||
|
Cursor = mongo.Cursor
|
||||||
|
Client = mongo.Client
|
||||||
|
Collection = mongo.Collection
|
||||||
|
)
|
||||||
|
|
||||||
// Database is the mongox database interface
|
// Database is the mongox database interface
|
||||||
type Database interface {
|
type Database interface {
|
||||||
Client() *mongo.Client
|
Client() (client *Client)
|
||||||
Context() context.Context
|
Context() (context context.Context)
|
||||||
Name() string
|
Name() (name string)
|
||||||
New(ctx context.Context) Database
|
New(ctx context.Context) (db Database)
|
||||||
GetCollectionOf(document interface{}) *mongo.Collection
|
GetCollectionOf(document interface{}) (collection *Collection)
|
||||||
Count(target interface{}, filters ...interface{}) (int64, error)
|
Count(target interface{}, filters ...interface{}) (count int64, err error)
|
||||||
DeleteArray(target interface{}) error
|
DeleteArray(target interface{}) (err error)
|
||||||
DeleteOne(target interface{}, filters ...interface{}) error
|
DeleteOne(target interface{}, filters ...interface{}) (err error)
|
||||||
LoadArray(target interface{}, filters ...interface{}) error
|
LoadArray(target interface{}, filters ...interface{}) (err error)
|
||||||
LoadOne(target interface{}, filters ...interface{}) error
|
LoadOne(target interface{}, filters ...interface{}) (err error)
|
||||||
LoadStream(target interface{}, filters ...interface{}) (StreamLoader, error)
|
LoadStream(target interface{}, filters ...interface{}) (loader StreamLoader, err error)
|
||||||
SaveOne(source interface{}) error
|
SaveOne(source interface{}) (err error)
|
||||||
|
IndexEnsure(cfg interface{}, document interface{}) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamLoader is a interface to control database cursor
|
// StreamLoader is a interface to control database cursor
|
||||||
type StreamLoader interface {
|
type StreamLoader interface {
|
||||||
DecodeNext() error
|
Cursor() (cursor *Cursor)
|
||||||
Decode() error
|
DecodeNext() (err error)
|
||||||
Next() error
|
Decode() (err error)
|
||||||
Close() error
|
Next() (err error)
|
||||||
Err() error
|
Close() (err error)
|
||||||
|
Err() (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OIDBased is an interface for documents that have objectId type for the _id field
|
// OIDBased is an interface for documents that have objectId type for the _id field
|
||||||
type OIDBased interface {
|
type OIDBased interface {
|
||||||
GetID() primitive.ObjectID
|
GetID() (id primitive.ObjectID)
|
||||||
SetID(id primitive.ObjectID)
|
SetID(id primitive.ObjectID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringBased is an interface for documents that have string type for the _id field
|
// StringBased is an interface for documents that have string type for the _id field
|
||||||
type StringBased interface {
|
type StringBased interface {
|
||||||
GetID() string
|
GetID() (id string)
|
||||||
SetID(id string)
|
SetID(id string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSONBased is an interface for documents that have object type for the _id field
|
// JSONBased is an interface for documents that have object type for the _id field
|
||||||
type JSONBased interface {
|
type JSONBased interface {
|
||||||
GetID() primitive.D
|
GetID() (id primitive.D)
|
||||||
SetID(id primitive.D)
|
SetID(id primitive.D)
|
||||||
}
|
}
|
||||||
|
|
||||||
// InterfaceBased is an interface for documents that have custom declated type for the _id field
|
// InterfaceBased is an interface for documents that have custom declated type for the _id field
|
||||||
type InterfaceBased interface {
|
type InterfaceBased interface {
|
||||||
GetID() interface{}
|
GetID() (id interface{})
|
||||||
SetID(id interface{})
|
SetID(id interface{})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package query
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OnDecode func(ctx context.Context, iter interface{}) (err error)
|
||||||
+29
-15
@@ -7,38 +7,44 @@ 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
|
||||||
func Compose(filters ...interface{}) *Query {
|
func Compose(filters ...interface{}) (query *Query) {
|
||||||
|
|
||||||
q := &Query{}
|
query = &Query{}
|
||||||
|
|
||||||
for _, f := range filters {
|
for _, f := range filters {
|
||||||
if !Push(q, f) {
|
if !Push(query, f) {
|
||||||
panic(fmt.Errorf("unknown filter %v", f))
|
panic(fmt.Errorf("unknown filter %v", f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return q
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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{}) (ok bool) {
|
||||||
|
|
||||||
ok := false
|
if utils.IsNil(f) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = false
|
||||||
ok = ok || applyBson(q, f)
|
ok = ok || applyBson(q, f)
|
||||||
ok = ok || applyLimit(q, f)
|
ok = ok || applyLimit(q, f)
|
||||||
ok = ok || applySort(q, f)
|
ok = ok || applySort(q, f)
|
||||||
ok = ok || applySkip(q, f)
|
ok = ok || applySkip(q, f)
|
||||||
ok = ok || applyProtection(q, f)
|
ok = ok || applyProtection(q, f)
|
||||||
ok = ok || applyPreloader(q, f)
|
ok = ok || applyPreloader(q, f)
|
||||||
|
ok = ok || applyCallbacks(q, f)
|
||||||
|
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyBson is a fallback for a custom bson.M
|
// applyBson is a fallback for a custom bson.M
|
||||||
func applyBson(q *Query, f interface{}) bool {
|
func applyBson(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
if f, ok := f.(bson.M); ok {
|
if f, ok := f.(bson.M); ok {
|
||||||
q.And(f)
|
q.And(f)
|
||||||
@@ -49,7 +55,7 @@ func applyBson(q *Query, f interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// applyLimits extends query with a limiter
|
// applyLimits extends query with a limiter
|
||||||
func applyLimit(q *Query, f interface{}) bool {
|
func applyLimit(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
if f, ok := f.(Limiter); ok {
|
if f, ok := f.(Limiter); ok {
|
||||||
q.limiter = f
|
q.limiter = f
|
||||||
@@ -60,7 +66,7 @@ func applyLimit(q *Query, f interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// applySort extends query with a sort rule
|
// applySort extends query with a sort rule
|
||||||
func applySort(q *Query, f interface{}) bool {
|
func applySort(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
if f, ok := f.(Sorter); ok {
|
if f, ok := f.(Sorter); ok {
|
||||||
q.sorter = f
|
q.sorter = f
|
||||||
@@ -71,7 +77,7 @@ func applySort(q *Query, f interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// applySkip extends query with a skip number
|
// applySkip extends query with a skip number
|
||||||
func applySkip(q *Query, f interface{}) bool {
|
func applySkip(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
if f, ok := f.(Skipper); ok {
|
if f, ok := f.(Skipper); ok {
|
||||||
q.skipper = f
|
q.skipper = f
|
||||||
@@ -81,7 +87,7 @@ func applySkip(q *Query, f interface{}) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyProtection(q *Query, f interface{}) bool {
|
func applyProtection(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
var x *primitive.ObjectID
|
var x *primitive.ObjectID
|
||||||
var v *int64
|
var v *int64
|
||||||
@@ -91,9 +97,6 @@ func applyProtection(q *Query, f interface{}) bool {
|
|||||||
x = &f.X
|
x = &f.X
|
||||||
v = &f.V
|
v = &f.V
|
||||||
case *protection.Key:
|
case *protection.Key:
|
||||||
if f == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
x = &f.X
|
x = &f.X
|
||||||
v = &f.V
|
v = &f.V
|
||||||
|
|
||||||
@@ -112,7 +115,7 @@ func applyProtection(q *Query, f interface{}) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyPreloader(q *Query, f interface{}) bool {
|
func applyPreloader(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
if f, ok := f.(Preloader); ok {
|
if f, ok := f.(Preloader); ok {
|
||||||
q.preloader = f
|
q.preloader = f
|
||||||
@@ -121,3 +124,14 @@ func applyPreloader(q *Query, f interface{}) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applyCallbacks(q *Query, f interface{}) (ok bool) {
|
||||||
|
|
||||||
|
switch cb := f.(type) {
|
||||||
|
case OnDecode:
|
||||||
|
q.ondecode = append(q.ondecode, cb)
|
||||||
|
ok = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package query
|
|||||||
|
|
||||||
// Limiter is a filter to limit the result
|
// Limiter is a filter to limit the result
|
||||||
type Limiter interface {
|
type Limiter interface {
|
||||||
Limit() *int64
|
Limit() (limit *int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limit is a simple implementation of the Limiter filter
|
// Limit is a simple implementation of the Limiter filter
|
||||||
@@ -11,12 +11,14 @@ type Limit int64
|
|||||||
var _ Limiter = Limit(0)
|
var _ Limiter = Limit(0)
|
||||||
|
|
||||||
// Limit returns a limit
|
// Limit returns a limit
|
||||||
func (l Limit) Limit() *int64 {
|
func (l Limit) Limit() (limit *int64) {
|
||||||
|
|
||||||
lim := int64(l)
|
if l <= 0 {
|
||||||
if lim <= 0 {
|
return
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &lim
|
limit = new(int64)
|
||||||
|
*limit = int64(l)
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package query
|
|||||||
|
|
||||||
// Preloader is a filter to skip the result
|
// Preloader is a filter to skip the result
|
||||||
type Preloader interface {
|
type Preloader interface {
|
||||||
Preload() []string
|
Preload() (preloads []string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preload is a simple implementation of the Skipper filter
|
// Preload is a simple implementation of the Skipper filter
|
||||||
@@ -11,6 +11,6 @@ type Preload []string
|
|||||||
var _ Preloader = Preload{}
|
var _ Preloader = Preload{}
|
||||||
|
|
||||||
// Preload returns a preload list
|
// Preload returns a preload list
|
||||||
func (l Preload) Preload() []string {
|
func (l Preload) Preload() (preloads []string) {
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-21
@@ -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
|
||||||
@@ -13,10 +11,11 @@ type Query struct {
|
|||||||
sorter Sorter
|
sorter Sorter
|
||||||
skipper Skipper
|
skipper Skipper
|
||||||
preloader Preloader
|
preloader Preloader
|
||||||
|
ondecode []OnDecode
|
||||||
}
|
}
|
||||||
|
|
||||||
// And function pushes the elem query to the $and array of the query
|
// And function pushes the elem query to the $and array of the query
|
||||||
func (q *Query) And(elem bson.M) *Query {
|
func (q *Query) And(elem bson.M) (query *Query) {
|
||||||
|
|
||||||
if q.m == nil {
|
if q.m == nil {
|
||||||
q.m = bson.M{}
|
q.m = bson.M{}
|
||||||
@@ -35,61 +34,59 @@ func (q *Query) And(elem bson.M) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Limiter returns limiter value or nil
|
// Limiter returns limiter value or nil
|
||||||
func (q *Query) Limiter() *int64 {
|
func (q *Query) Limiter() (limit *int64) {
|
||||||
|
|
||||||
if q.limiter == nil {
|
if q.limiter == nil {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return q.limiter.Limit()
|
return q.limiter.Limit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sorter is a sort rule for a query
|
// Sorter is a sort rule for a query
|
||||||
func (q *Query) Sorter() interface{} {
|
func (q *Query) Sorter() (sort interface{}) {
|
||||||
|
|
||||||
if q.sorter == nil {
|
if q.sorter == nil {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return q.sorter.Sort()
|
return q.sorter.Sort()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skipper is a skipper for a query
|
// Skipper is a skipper for a query
|
||||||
func (q *Query) Skipper() *int64 {
|
func (q *Query) Skipper() (skip *int64) {
|
||||||
|
|
||||||
if q.skipper == nil {
|
if q.skipper == nil {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return q.skipper.Skip()
|
return q.skipper.Skip()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preloader is a preloader list for a query
|
// Preloader is a preloader list for a query
|
||||||
func (q *Query) Preloader() (empty bool, preloader []string) {
|
func (q *Query) Preloader() (ok bool, preloads []string) {
|
||||||
|
|
||||||
if q.preloader == nil {
|
if q.preloader == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
preloader = q.preloader.Preload()
|
preloads = q.preloader.Preload()
|
||||||
|
ok = len(preloads) > 0
|
||||||
|
|
||||||
if len(preloader) == 0 {
|
return
|
||||||
return false, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, preloader
|
// OnDecode callback is called after the mongo decode function
|
||||||
|
func (q *Query) OnDecode() (callbacks []OnDecode) {
|
||||||
|
return q.ondecode
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty checks the query for any content
|
// Empty checks the query for any content
|
||||||
func (q *Query) Empty() bool {
|
func (q *Query) Empty() (isEmpty 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
|
||||||
func (q *Query) M() bson.M {
|
func (q *Query) M() (m bson.M) {
|
||||||
return q.m
|
return q.m
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package query
|
|||||||
|
|
||||||
// Skipper is a filter to skip the result
|
// Skipper is a filter to skip the result
|
||||||
type Skipper interface {
|
type Skipper interface {
|
||||||
Skip() *int64
|
Skip() (skip *int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip is a simple implementation of the Skipper filter
|
// Skip is a simple implementation of the Skipper filter
|
||||||
@@ -11,12 +11,14 @@ type Skip int64
|
|||||||
var _ Skipper = Skip(0)
|
var _ Skipper = Skip(0)
|
||||||
|
|
||||||
// Skip returns a skip number
|
// Skip returns a skip number
|
||||||
func (l Skip) Skip() *int64 {
|
func (l Skip) Skip() (skip *int64) {
|
||||||
|
|
||||||
lim := int64(l)
|
if l <= 0 {
|
||||||
if lim <= 0 {
|
return
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &lim
|
skip = new(int64)
|
||||||
|
*skip = int64(l)
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
// Sorter is a filter to sort the data before query
|
// Sorter is a filter to sort the data before query
|
||||||
type Sorter interface {
|
type Sorter interface {
|
||||||
Sort() bson.M
|
Sort() (sort bson.M)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort is a simple implementations of the Sorter filter
|
// Sort is a simple implementations of the Sorter filter
|
||||||
@@ -15,6 +15,6 @@ type Sort bson.M
|
|||||||
var _ Sorter = &Sort{}
|
var _ Sorter = &Sort{}
|
||||||
|
|
||||||
// Sort returns a slice of fields which have to be sorted
|
// Sort returns a slice of fields which have to be sorted
|
||||||
func (f Sort) Sort() bson.M {
|
func (f Sort) Sort() (sort bson.M) {
|
||||||
return bson.M(f)
|
return bson.M(f)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -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