Skip to content

Commit

Permalink
Merge pull request #33 from jiangz222/lineerror
Browse files Browse the repository at this point in the history
fix line error
  • Loading branch information
jiangz222 authored Aug 10, 2020
2 parents c61efae + 7e208b6 commit dadc78f
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 57 deletions.
4 changes: 3 additions & 1 deletion aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

// Pipeline define the pipeline for aggregate
type Pipeline []bson.D

// Aggregate is a handle to a aggregate
type Aggregate struct {
ctx context.Context
pipeline interface{}
Expand Down Expand Up @@ -37,7 +39,7 @@ func (a *Aggregate) One(result interface{}) error {
}
defer cr.Close()
if !cr.Next(result) {
return ERR_NO_SUCH_RECORD
return ErrNoSuchDocuments
}
return err
}
Expand Down
11 changes: 6 additions & 5 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package qmgo

import (
"strings"
"time"
)

// QmgoConfig for initial mongodb instance
// Config for initial mongodb instance
type Config struct {
// URI example: [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
// URI Reference: https://docs.mongodb.com/manual/reference/connection-string/
Expand All @@ -30,10 +31,10 @@ func IsDup(err error) bool {
return strings.Contains(err.Error(), "E11000")
}

//// Now return Millisecond current time
//func Now() time.Time {
// return time.Unix(0, time.Now().UnixNano()/1e6*1e6)
//}
// Now return Millisecond current time
func Now() time.Time {
return time.Unix(0, time.Now().UnixNano()/1e6*1e6)
}

// SplitSortField handle sort symbol: "+"/"-" in front of field
// if "+", return sort as 1
Expand Down
13 changes: 13 additions & 0 deletions base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package qmgo

import (
"fmt"
"testing"
"time"
)

func TestNow(t *testing.T) {
t1 := time.Unix(0, time.Now().UnixNano()/1e6*1e6)
t2 := Now()
fmt.Println(t1, t2)
}
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/readpref"
)

// Client specifies the instance to operate mongoDB
// QmgoClient specifies the instance to operate mongoDB
type QmgoClient struct {
*Collection
*Database
Expand Down
8 changes: 4 additions & 4 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.mongodb.org/mongo-driver/x/bsonx"
)

// Collection
// Collection is a handle to a MongoDB collection
type Collection struct {
collection *mongo.Collection
}
Expand All @@ -24,7 +24,7 @@ func (c *Collection) Find(ctx context.Context, filter interface{}) QueryI {
}
}

// Insert insert one document into the collection
// InsertOne insert one document into the collection
// Reference: https://docs.mongodb.com/manual/reference/command/insert/
func (c *Collection) InsertOne(ctx context.Context, doc interface{}) (result *InsertOneResult, err error) {
res, err := c.collection.InsertOne(ctx, doc)
Expand Down Expand Up @@ -68,7 +68,7 @@ func (c *Collection) Update(ctx context.Context, filter interface{}, update inte
}

if res.MatchedCount == 0 {
err = ERR_NO_SUCH_RECORD
err = ErrNoSuchDocuments
}

return err
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *Collection) Remove(ctx context.Context, filter interface{}) (err error)
return err
}
if res.DeletedCount == 0 {
err = ERR_NO_SUCH_RECORD
err = ErrNoSuchDocuments
}

return err
Expand Down
6 changes: 4 additions & 2 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestCollection_Update(t *testing.T) {
},
}
err = cli.Update(context.Background(), filter2, update2)
ast.Equal(err, ERR_NO_SUCH_RECORD)
ast.Equal(err, ErrNoSuchDocuments)

// filter is nil or wrong BSON Document format
update3 := bson.M{
Expand Down Expand Up @@ -374,17 +374,19 @@ func TestCollection_Remove(t *testing.T) {
"name": "Lily",
}
err = cli.Remove(context.Background(), filter2)
ast.Equal(err, ERR_NO_SUCH_RECORD)
ast.Equal(err, ErrNoSuchDocuments)

// filter is bson.M{},delete one document
filter3 := bson.M{}
preCnt, err := cli.Find(context.Background(), filter3).Count()
ast.NoError(err)
ast.Equal(int64(2), preCnt)

err = cli.Remove(context.Background(), filter3)
ast.NoError(err)

afterCnt, err := cli.Find(context.Background(), filter3).Count()
ast.NoError(err)
ast.Equal(preCnt-1, afterCnt)

// filter is nil or wrong BSON Document format
Expand Down
18 changes: 1 addition & 17 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@ package qmgo

import (
"context"
"fmt"

"go.mongodb.org/mongo-driver/mongo"
)

// Database is a handle to a MongoDB database
type Database struct {
database *mongo.Database
}

// NewDatabase creates new database connection
func NewDatabase(ctx context.Context, conf *Config) (cli *Database, err error) {
client, err := client(ctx, conf)
if err != nil {
fmt.Println("new client fail", err)
return
}
db := client.Database(conf.Database)

cli = &Database{
database: db,
}
return
}

// Collection gets collection from database
func (d *Database) Collection(name string) *Collection {
var cp *mongo.Collection
Expand Down
17 changes: 4 additions & 13 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,8 @@ func TestDatabase(t *testing.T) {
var maxPoolSize uint64 = 3000
collName := "testopen"
dbName := "mongoxtest"
cfg := Config{
Uri: "://localhost:27017",
Database: dbName,
Coll: collName,
ConnectTimeoutMS: &cTimeout,
SocketTimeoutMS: &sTimeout,
MaxPoolSize: &maxPoolSize,
}

cli, err := NewDatabase(context.Background(), &cfg)
ast.NotNil(err)

cfg = Config{
cfg := Config{
Uri: "mongodb://localhost:27017",
Database: dbName,
Coll: collName,
Expand All @@ -36,7 +25,9 @@ func TestDatabase(t *testing.T) {
MaxPoolSize: &maxPoolSize,
}

cli, err = NewDatabase(context.Background(), &cfg)
c, err := NewClient(context.Background(), &cfg)
ast.NoError(err)
cli := c.Database(cfg.Database)
ast.Nil(err)
ast.Equal(dbName, cli.GetDatabaseName())
coll := cli.Collection(collName)
Expand Down
21 changes: 15 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package qmgo

import "errors"
import (
"errors"

"go.mongodb.org/mongo-driver/mongo"
)

var (
ERR_QUERY_NOT_SLICE_POINTER = errors.New("result argument must be a pointer to a slice")
ERR_QUERY_NOT_SLICE_TYPE = errors.New("result argument must be a slice address")
ERR_QUERY_RESULT_TYPE_INCONSISTEN = errors.New("result type is not equal mongodb value type")
ERR_QUERY_RESULT_VAL_CAN_NOT_CHANGE = errors.New("the value of result can not be changed")
ERR_NO_SUCH_RECORD = errors.New("no such record")
// ErrQueryNotSlicePointer return if result argument is not a pointer to a slice
ErrQueryNotSlicePointer = errors.New("result argument must be a pointer to a slice")
// ErrQueryNotSliceType return if result argument is not slice address
ErrQueryNotSliceType = errors.New("result argument must be a slice address")
// ErrQueryResultTypeInconsistent return if result type is not equal mongodb value type
ErrQueryResultTypeInconsistent = errors.New("result type is not equal mongodb value type")
// ErrQueryResultValCanNotChange return if the value of result can not be changed
ErrQueryResultValCanNotChange = errors.New("the value of result can not be changed")
// ErrNoSuchDocuments return if no document found
ErrNoSuchDocuments = errors.New(mongo.ErrNoDocuments.Error())
)
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type QueryI interface {
Cursor() CursorI
}

// AggregateI define the interface of aggregate
type AggregateI interface {
All(results interface{}) error
One(result interface{}) error
Expand Down
10 changes: 5 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// Query
// Query struct definition
type Query struct {
ctx context.Context
collection *mongo.Collection
Expand Down Expand Up @@ -167,19 +167,19 @@ func (q *Query) Distinct(key string, result interface{}) error {
resultVal := reflect.ValueOf(result)

if resultVal.Kind() != reflect.Ptr {
return ERR_QUERY_NOT_SLICE_POINTER
return ErrQueryNotSlicePointer
}

sliceVal := resultVal.Elem()
if sliceVal.Kind() == reflect.Interface {
sliceVal = sliceVal.Elem()
}
if sliceVal.Kind() != reflect.Slice {
return ERR_QUERY_NOT_SLICE_TYPE
return ErrQueryNotSliceType
}

if !resultVal.Elem().CanSet() {
return ERR_QUERY_RESULT_VAL_CAN_NOT_CHANGE
return ErrQueryResultValCanNotChange
}

sliceVal = sliceVal.Slice(0, 0)
Expand All @@ -197,7 +197,7 @@ func (q *Query) Distinct(key string, result interface{}) error {

if vType != elementType {
fmt.Printf("mongo type: %s, result type: %s\n", vType.Name(), elementType.Name())
return ERR_QUERY_RESULT_TYPE_INCONSISTEN
return ErrQueryResultTypeInconsistent
}
sliceVal = reflect.Append(sliceVal, vValue)
}
Expand Down
6 changes: 3 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,17 @@ func TestQuery_Distinct(t *testing.T) {
var res3 []int32

err = cli.Find(context.Background(), filter2).Distinct("age", res3)
ast.EqualError(err, ERR_QUERY_NOT_SLICE_POINTER.Error())
ast.EqualError(err, ErrQueryNotSlicePointer.Error())

var res4 int

err = cli.Find(context.Background(), filter2).Distinct("age", &res4)
ast.EqualError(err, ERR_QUERY_NOT_SLICE_TYPE.Error())
ast.EqualError(err, ErrQueryNotSliceType.Error())

var res5 []string

err = cli.Find(context.Background(), filter2).Distinct("age", &res5)
ast.EqualError(err, ERR_QUERY_RESULT_TYPE_INCONSISTEN.Error())
ast.EqualError(err, ErrQueryResultTypeInconsistent.Error())

var res6 []int32

Expand Down

0 comments on commit dadc78f

Please sign in to comment.