6 Commits

Author SHA1 Message Date
Nikita Tokarchuk 6111341a3c Check for nil interface correctly 2020-03-24 21:31:29 +01:00
Nikita Tokarchuk 9f647ca094 Allow nil query in composer 2020-03-19 00:44:48 +01:00
Nikita Tokarchuk eeb83daf4b Configure indices in struct tags 2020-03-14 19:35:49 +01:00
Nikita Tokarchuk 6e6a042a16 Return the empty context if not defined 2020-03-14 19:34:57 +01:00
Nikita Tokarchuk e0c26f770f Do not find through unexported values 2020-03-14 19:34:37 +01:00
Nikita Tokarchuk e7a05d94e1 Reexport basic mongo structs 2020-03-13 00:40:33 +01:00
16 changed files with 432 additions and 46 deletions
+2 -1
View File
@@ -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
}
+3
View File
@@ -19,6 +19,9 @@ func GetProtection(source interface{}) *protection.Key {
for i := 0; i < numField; i++ {
field := el.Field(i)
if !field.CanInterface() {
continue
}
switch field.Interface().(type) {
case *protection.Key:
+2 -2
View File
@@ -3,9 +3,9 @@ package database
import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
)
@@ -21,7 +21,7 @@ func (d *Database) Count(target interface{}, filters ...interface{}) (int64, err
opts.Skip = composed.Skipper()
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
if err == mongo.ErrNoDocuments {
if err == mongox.ErrNoDocuments {
return 0, err
}
if err != nil {
+14 -9
View File
@@ -8,7 +8,6 @@ import (
"strings"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox"
@@ -17,13 +16,13 @@ import (
// Database handler
type Database struct {
client *mongo.Client
client *mongox.Client
dbname string
ctx context.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) mongox.Database {
db := &Database{}
db.client = client
@@ -33,13 +32,19 @@ func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
}
// Client function returns a mongo client
func (d *Database) Client() *mongo.Client {
func (d *Database) Client() *mongox.Client {
return d.client
}
// Context function returns a context
func (d *Database) Context() context.Context {
return d.ctx
func (d *Database) Context() (ctx context.Context) {
ctx = d.ctx
if ctx == nil {
ctx = context.Background()
}
return
}
// Name function returns a database name
@@ -67,7 +72,7 @@ func (d *Database) New(ctx context.Context) mongox.Database {
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
// ...
// Will panic if there is no «collection» tag
func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
func (d *Database) GetCollectionOf(document interface{}) *mongox.Collection {
el := reflect.TypeOf(document).Elem()
numField := el.NumField()
@@ -86,7 +91,7 @@ func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
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)
opts := options.Find()
@@ -98,7 +103,7 @@ func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (
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)
opts := options.Aggregate()
+4 -3
View File
@@ -5,11 +5,12 @@ import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"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)})
}
@@ -38,7 +39,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
}
err := result.Decode(target)
if err == mongo.ErrNoDocuments {
if err == mongox.ErrNoDocuments {
return err
}
if err != nil {
+134
View File
@@ -0,0 +1,134 @@
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{}) 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)
}
tmpBuffer := &bytes.Buffer{}
tpl, err := template.New("").Parse(indexTag)
if err != nil {
panic(fmt.Errorf("invalid prop template, %v", indexTag))
}
err = tpl.Execute(tmpBuffer, cfg)
if err != nil {
panic(fmt.Errorf("failed to evaluate prop template, %v", indexTag))
}
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"))
}
index := primitive.M{key: 1}
opts := &options.IndexOptions{
Background: &f,
Unique: &f,
Name: &name,
}
if indexValues[0] == "-" {
index[key] = -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[compoundValue[1:]] = -1
} else {
index[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 err
}
}
return nil
}
+156
View File
@@ -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])
}
}
}
+2 -3
View File
@@ -4,8 +4,7 @@ import (
"fmt"
"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/query"
)
@@ -36,7 +35,7 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
zeroElem := reflect.Zero(targetSliceElemT)
hasPreloader, _ := composed.Preloader()
var result *mongo.Cursor
var result *mongox.Cursor
var err error
if hasPreloader {
+3 -4
View File
@@ -3,8 +3,7 @@ package database
import (
"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/query"
)
@@ -15,7 +14,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
composed := query.Compose(append(filters, query.Limit(1))...)
hasPreloader, _ := composed.Preloader()
var result *mongo.Cursor
var result *mongox.Cursor
var err error
if hasPreloader {
@@ -32,7 +31,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
return err
}
if !hasNext {
return mongo.ErrNoDocuments
return mongox.ErrNoDocuments
}
base.Reset(target)
+2 -4
View File
@@ -3,8 +3,6 @@ package database
import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
)
@@ -12,7 +10,7 @@ import (
// LoadStream function loads documents one by one into a target channel
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
var cursor *mongo.Cursor
var cursor *mongox.Cursor
var err error
composed := query.Compose(filters...)
@@ -27,7 +25,7 @@ func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongo
return nil, fmt.Errorf("can't create find result: %w", err)
}
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target}
l := &StreamLoader{cur: cursor, ctx: d.Context(), target: target}
return l, nil
}
+21 -15
View File
@@ -4,14 +4,13 @@ import (
"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"
)
// StreamLoader is a controller for a database cursor
type StreamLoader struct {
*mongo.Cursor
cur *mongox.Cursor
ctx context.Context
target interface{}
}
@@ -19,18 +18,18 @@ type StreamLoader struct {
// DecodeNext loads next documents to a target or returns an error
func (l *StreamLoader) DecodeNext() error {
hasNext := l.Cursor.Next(l.ctx)
hasNext := l.cur.Next(l.ctx)
if l.Cursor.Err() != nil {
return l.Cursor.Err()
if l.cur.Err() != nil {
return l.cur.Err()
}
if !hasNext {
return mongo.ErrNoDocuments
return mongox.ErrNoDocuments
}
base.Reset(l.target)
err := l.Cursor.Decode(l.target)
err := l.cur.Decode(l.target)
if err != nil {
return fmt.Errorf("can't decode desult: %w", err)
}
@@ -43,7 +42,7 @@ func (l *StreamLoader) Decode() error {
base.Reset(l.target)
err := l.Cursor.Decode(l.target)
err := l.cur.Decode(l.target)
if err != nil {
return fmt.Errorf("can't decode desult: %w", err)
}
@@ -54,20 +53,27 @@ func (l *StreamLoader) Decode() error {
// Next loads next documents but doesn't perform decoding
func (l *StreamLoader) Next() error {
hasNext := l.Cursor.Next(l.ctx)
hasNext := l.cur.Next(l.ctx)
if l.Cursor.Err() != nil {
return l.Cursor.Err()
if l.cur.Err() != nil {
return l.cur.Err()
}
if !hasNext {
return mongo.ErrNoDocuments
return mongox.ErrNoDocuments
}
return nil
}
func (l *StreamLoader) Cursor() *mongox.Cursor {
return l.cur
}
// Close cursor
func (l *StreamLoader) Close() error {
return l.Cursor.Close(l.ctx)
return l.cur.Close(l.ctx)
}
func (l *StreamLoader) Err() error {
return l.cur.Err()
}
+20
View File
@@ -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
)
+11 -2
View File
@@ -7,13 +7,20 @@ import (
"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
type Database interface {
Client() *mongo.Client
Client() *Client
Context() context.Context
Name() string
New(ctx context.Context) Database
GetCollectionOf(document interface{}) *mongo.Collection
GetCollectionOf(document interface{}) *Collection
Count(target interface{}, filters ...interface{}) (int64, error)
DeleteArray(target interface{}) error
DeleteOne(target interface{}, filters ...interface{}) error
@@ -21,10 +28,12 @@ type Database interface {
LoadOne(target interface{}, filters ...interface{}) error
LoadStream(target interface{}, filters ...interface{}) (StreamLoader, error)
SaveOne(source interface{}) error
IndexEnsure(cfg interface{}, document interface{}) error
}
// StreamLoader is a interface to control database cursor
type StreamLoader interface {
Cursor() *Cursor
DecodeNext() error
Decode() error
Next() error
+5 -3
View File
@@ -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,6 +27,10 @@ func Compose(filters ...interface{}) *Query {
// Push applies single filter to a query
func Push(q *Query, f interface{}) bool {
if utils.IsNil(f) {
return true
}
ok := false
ok = ok || applyBson(q, f)
ok = ok || applyLimit(q, f)
@@ -91,9 +96,6 @@ func applyProtection(q *Query, f interface{}) bool {
x = &f.X
v = &f.V
case *protection.Key:
if f == nil {
return false
}
x = &f.X
v = &f.V
+21
View File
@@ -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
}
+32
View File
@@ -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))
}
}