diff --git a/const.go b/const.go index b3e833f..8c6e61e 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package dorm -const Version = "1.0.34" +const Version = "1.0.35" diff --git a/go.mod b/go.mod index 1547bb4..87bbf63 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect - github.com/go-playground/validator/v10 v10.11.0 // indirect + github.com/go-playground/validator/v10 v10.11.1 // indirect github.com/goccy/go-json v0.9.11 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect diff --git a/go.sum b/go.sum index 4ee757d..cce471b 100644 --- a/go.sum +++ b/go.sum @@ -93,8 +93,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= -github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw= -github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= +github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= +github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= github.com/go-redis/redis/v9 v9.0.0-beta.2 h1:ZSr84TsnQyKMAg8gnV+oawuQezeJR11/09THcWCQzr4= github.com/go-redis/redis/v9 v9.0.0-beta.2/go.mod h1:Bldcd/M/bm9HbnNPi/LUtYBSD8ttcZYBMupwMXhdU0o= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= diff --git a/mongo.go b/mongo.go index af8003f..1b33b36 100644 --- a/mongo.go +++ b/mongo.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -16,36 +15,31 @@ type ConfigMongoClient struct { } type MongoClient struct { - Db *mongo.Client // 驱动 - config *ConfigMongoClient // 配置 - databaseName string // 库名 - collectionName string // 表名 - //filterArr []queryFilter // 查询条件数组 - filter bson.D // 查询条件 + Db *mongo.Client // 驱动 + config *ConfigMongoClient // 配置 } func NewMongoClient(config *ConfigMongoClient) (*MongoClient, error) { + var ctx = context.Background() var err error c := &MongoClient{config: config} - c.databaseName = c.config.DatabaseName - // 连接到MongoDB if c.config.Dns != "" { - c.Db, err = mongo.Connect(context.Background(), options.Client().ApplyURI(c.config.Dns)) + c.Db, err = mongo.Connect(ctx, options.Client().ApplyURI(c.config.Dns)) if err != nil { return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) } } else { - c.Db, err = mongo.Connect(context.Background(), c.config.Opts) + c.Db, err = mongo.Connect(ctx, c.config.Opts) if err != nil { return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) } } // 检查连接 - err = c.Db.Ping(context.TODO(), nil) + err = c.Db.Ping(ctx, nil) if err != nil { return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) } @@ -54,10 +48,6 @@ func NewMongoClient(config *ConfigMongoClient) (*MongoClient, error) { } // Close 关闭 -func (c *MongoClient) Close() error { - err := c.Db.Disconnect(context.TODO()) - if err != nil { - return errors.New(fmt.Sprintf("关闭失败:%v", err)) - } - return nil +func (c *MongoClient) Close(ctx context.Context) error { + return c.Db.Disconnect(ctx) } diff --git a/mongo_collection.go b/mongo_collection.go index ff67aec..a84bfb6 100644 --- a/mongo_collection.go +++ b/mongo_collection.go @@ -8,7 +8,7 @@ import ( ) type MongoCollectionOptions struct { - dbCollection *mongo.Collection + dbCollection *mongo.Collection // 集合 } // Collection 选择集合 diff --git a/mongo_database.go b/mongo_database.go index 4277b09..75749f4 100644 --- a/mongo_database.go +++ b/mongo_database.go @@ -7,7 +7,7 @@ import ( ) type MongoDatabaseOptions struct { - dbDatabase *mongo.Database + dbDatabase *mongo.Database // 数据库 } // Database 选择数据库 diff --git a/mongo_get.go b/mongo_get.go index 8ccd7f6..eaa58b5 100644 --- a/mongo_get.go +++ b/mongo_get.go @@ -6,13 +6,3 @@ import "go.mongodb.org/mongo-driver/mongo" func (c *MongoClient) GetDb() *mongo.Client { return c.Db } - -// 获取库名 -func (c *MongoClient) getDatabaseName() string { - return c.databaseName -} - -// 获取表名 -func (c *MongoClient) getCollectionName() string { - return c.collectionName -} diff --git a/mongo_session.go b/mongo_session.go new file mode 100644 index 0000000..b66ded2 --- /dev/null +++ b/mongo_session.go @@ -0,0 +1,50 @@ +package dorm + +import ( + "context" + "errors" + "fmt" + "go.mongodb.org/mongo-driver/mongo" +) + +type MongoSessionOptions struct { + Session mongo.SessionContext // 会话 + Db *mongo.Client // 驱动 + startSession mongo.Session // 开始会话 +} + +// Begin 开始事务,会同时创建开始会话需要在退出时关闭会话 +func (c *MongoClient) Begin() (ms *MongoSessionOptions, err error) { + + var ctx = context.Background() + + ms.Db = c.Db + + // 开始会话 + ms.startSession, err = ms.Db.StartSession() + if err != nil { + return nil, errors.New(fmt.Sprintf("开始会话失败:%v", err)) + } + + // 会话上下文 + ms.Session = mongo.NewSessionContext(ctx, ms.startSession) + + // 会话开启事务 + err = ms.startSession.StartTransaction() + return ms, err +} + +// Close 关闭会话 +func (cs *MongoSessionOptions) Close(ctx context.Context) { + cs.startSession.EndSession(ctx) +} + +// Rollback 回滚事务 +func (cs *MongoSessionOptions) Rollback(ctx context.Context) error { + return cs.startSession.AbortTransaction(ctx) +} + +// Commit 提交事务 +func (cs *MongoSessionOptions) Commit(ctx context.Context) error { + return cs.startSession.CommitTransaction(ctx) +} diff --git a/mongo_session_collection.go b/mongo_session_collection.go new file mode 100644 index 0000000..31e3905 --- /dev/null +++ b/mongo_session_collection.go @@ -0,0 +1,19 @@ +package dorm + +import ( + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type MongoSessionCollectionOptions struct { + session mongo.SessionContext // 会话 + dbCollection *mongo.Collection // 集合 +} + +// Collection 选择集合 +func (csd *MongoSessionDatabaseOptions) Collection(name string, opts ...*options.CollectionOptions) *MongoSessionCollectionOptions { + return &MongoSessionCollectionOptions{ + session: csd.session, // 会话 + dbCollection: csd.dbDatabase.Collection(name, opts...), // 集合 + } +} diff --git a/mongo_session_collection_curd.go b/mongo_session_collection_curd.go new file mode 100644 index 0000000..7bca5a7 --- /dev/null +++ b/mongo_session_collection_curd.go @@ -0,0 +1,46 @@ +package dorm + +import ( + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +// InsertOne 插入一个文档 +func (csc *MongoSessionCollectionOptions) InsertOne(document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) { + return csc.dbCollection.InsertOne(csc.session, document, opts...) +} + +// InsertMany 插入多个文档 +func (csc *MongoSessionCollectionOptions) InsertMany(document []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) { + return csc.dbCollection.InsertMany(csc.session, document, opts...) +} + +// DeleteOne 删除一个文档 +func (csc *MongoSessionCollectionOptions) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { + return csc.dbCollection.DeleteOne(csc.session, filter, opts...) +} + +// DeleteMany 删除多个文档 +func (csc *MongoSessionCollectionOptions) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { + return csc.dbCollection.DeleteMany(csc.session, filter, opts...) +} + +// UpdateOne 更新一个文档 +func (csc *MongoSessionCollectionOptions) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { + return csc.dbCollection.UpdateOne(csc.session, filter, update, opts...) +} + +// UpdateMany 更新多个文档 +func (csc *MongoSessionCollectionOptions) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { + return csc.dbCollection.UpdateMany(csc.session, filter, update, opts...) +} + +// FindOne 查询一个文档 +func (csc *MongoSessionCollectionOptions) FindOne(filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult { + return csc.dbCollection.FindOne(csc.session, filter, opts...) +} + +// Find 查询多个文档 +func (csc *MongoSessionCollectionOptions) Find(filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) { + return csc.dbCollection.Find(csc.session, filter, opts...) +} diff --git a/mongo_session_database.go b/mongo_session_database.go new file mode 100644 index 0000000..1d2952c --- /dev/null +++ b/mongo_session_database.go @@ -0,0 +1,19 @@ +package dorm + +import ( + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type MongoSessionDatabaseOptions struct { + session mongo.SessionContext // 会话 + dbDatabase *mongo.Database // 数据库 +} + +// Database 选择数据库 +func (cs *MongoSessionOptions) Database(name string, opts ...*options.DatabaseOptions) *MongoSessionDatabaseOptions { + return &MongoSessionDatabaseOptions{ + session: cs.Session, // 会话 + dbDatabase: cs.Db.Database(name, opts...), // 数据库 + } +} diff --git a/mongo_time.go b/mongo_time.go index b51646c..4bce39d 100644 --- a/mongo_time.go +++ b/mongo_time.go @@ -7,44 +7,74 @@ import ( "time" ) -// BsonTime 类型 +// BsonTime 时间类型 type BsonTime time.Time -// Value 时间类型 -func (t BsonTime) Value() string { - return gotime.SetCurrent(time.Time(t)).Bson() -} - // MarshalJSON 实现json序列化 -func (t BsonTime) MarshalJSON() ([]byte, error) { - //log.Println("MarshalJSON") +func (bt BsonTime) MarshalJSON() ([]byte, error) { + b := make([]byte, 0) - b = append(b, gotime.SetCurrent(time.Time(t)).Bson()...) + + b = append(b, gotime.SetCurrent(time.Time(bt)).Bson()...) + return b, nil } // UnmarshalJSON 实现json反序列化 -func (t *BsonTime) UnmarshalJSON(data []byte) (err error) { - //log.Println("UnmarshalJSON") - t1 := gotime.SetCurrentParse(string(data)) - *t = BsonTime(t1.Time) - return +func (bt *BsonTime) UnmarshalJSON(data []byte) (err error) { + + if string(data) == "null" { + return nil + } + + bsonTime := gotime.SetCurrentParse(string(data)) + + *bt = BsonTime(bsonTime.Time) + + return nil +} + +func (bt BsonTime) Time() time.Time { + return gotime.SetCurrent(time.Time(bt)).Time +} + +func (bt BsonTime) Format() string { + return gotime.SetCurrent(time.Time(bt)).Format() +} + +func (bt BsonTime) TimePro() gotime.Pro { + return gotime.SetCurrent(time.Time(bt)) +} + +// NewBsonTimeFromTime 创建 +func NewBsonTimeFromTime(t time.Time) BsonTime { + return BsonTime(t) +} + +// NewBsonTimeFromString 创建 +func NewBsonTimeFromString(t string) BsonTime { + return BsonTime(gotime.SetCurrentParse(t).Time) +} + +// Value 时间类型 +func (bt BsonTime) Value() string { + return gotime.SetCurrent(time.Time(bt)).Bson() } // MarshalBSONValue 实现bson序列化 -func (t BsonTime) MarshalBSONValue() (bsontype.Type, []byte, error) { +func (bt BsonTime) MarshalBSONValue() (bsontype.Type, []byte, error) { //log.Println("MarshalBSONValue") - targetTime := gotime.SetCurrent(time.Time(t)).Bson() + targetTime := gotime.SetCurrent(time.Time(bt)).Bson() return bson.MarshalValue(targetTime) } // UnmarshalBSONValue 实现bson反序列化 -func (t *BsonTime) UnmarshalBSONValue(t2 bsontype.Type, data []byte) error { +func (bt *BsonTime) UnmarshalBSONValue(t2 bsontype.Type, data []byte) error { //log.Println("UnmarshalBSONValue") t1 := gotime.SetCurrentParse(string(data)) //if string(data) == "" { // return errors.New(fmt.Sprintf("%s, %s, %s", "读取数据失败:", t2, data)) //} - *t = BsonTime(t1.Time) + *bt = BsonTime(t1.Time) return nil } diff --git a/mongo_transaction.go b/mongo_transaction.go deleted file mode 100644 index bec0430..0000000 --- a/mongo_transaction.go +++ /dev/null @@ -1,48 +0,0 @@ -package dorm - -import ( - "context" - "go.mongodb.org/mongo-driver/mongo" -) - -type MongoTransaction struct { - startSession mongo.Session - Session mongo.SessionContext - db *mongo.Client // 驱动 - databaseName string // 库名 - collectionName string // 表名 -} - -// Begin 开始事务,会同时创建开始会话需要在退出时关闭会话 -func (c *MongoClient) Begin() (ms MongoTransaction, err error) { - - ms.db = c.Db - - // 开始会话 - ms.startSession, err = ms.db.StartSession() - if err != nil { - panic(err) - } - - // 会话上下文 - ms.Session = mongo.NewSessionContext(context.Background(), ms.startSession) - - // 会话开启事务 - err = ms.startSession.StartTransaction() - return ms, err -} - -// Close 关闭会话 -func (ms *MongoTransaction) Close() { - ms.startSession.EndSession(context.TODO()) -} - -// Rollback 回滚事务 -func (ms *MongoTransaction) Rollback() error { - return ms.startSession.AbortTransaction(context.Background()) -} - -// Commit 提交事务 -func (ms *MongoTransaction) Commit() error { - return ms.startSession.CommitTransaction(context.Background()) -} diff --git a/mongo_transaction_curd.go b/mongo_transaction_curd.go deleted file mode 100644 index e7cfd81..0000000 --- a/mongo_transaction_curd.go +++ /dev/null @@ -1,174 +0,0 @@ -package dorm - -import ( - "context" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -// InsertOne 插入一个文档 -func (ms *MongoTransaction) InsertOne(document interface{}) (result *mongo.InsertOneResult, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - result, err = collection.InsertOne(ms.Session, document) - return -} - -// InsertMany 插入多个文档 -func (ms *MongoTransaction) InsertMany(documents []interface{}) (result *mongo.InsertManyResult, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - result, err = collection.InsertMany(ms.Session, documents) - return -} - -// Delete 删除文档 -func (ms *MongoTransaction) Delete(filter interface{}) (err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - _, err = collection.DeleteOne(ms.Session, filter) - return -} - -// DeleteId 删除文档 -func (ms *MongoTransaction) DeleteId(id interface{}) (err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - _, err = collection.DeleteOne(ms.Session, bson.M{"_id": id}) - return -} - -// DeleteMany 删除多个文档 -func (ms *MongoTransaction) DeleteMany(key string, value interface{}) (result *mongo.DeleteResult, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - filter := bson.D{{key, value}} - result, err = collection.DeleteMany(ms.Session, filter) - return -} - -// UpdateOne 更新单个文档 -// 修改字段的值($set) -// 字段增加值 inc($inc) -// 从数组中增加一个元素 push($push) -// 从数组中删除一个元素 pull($pull) -func (ms *MongoTransaction) UpdateOne(filter interface{}, update interface{}) (err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - _, err = collection.UpdateOne(ms.Session, filter, update) - return -} - -// UpdateId 更新单个文档 -// 修改字段的值($set) -// 字段增加值 inc($inc) -// 从数组中增加一个元素 push($push) -// 从数组中删除一个元素 pull($pull) -func (ms *MongoTransaction) UpdateId(id interface{}, update interface{}) (err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - _, err = collection.UpdateOne(context.TODO(), bson.M{"_id": id}, update) - return -} - -// UpdateMany 更新多个文档 -// 修改字段的值($set) -// 字段增加值 inc($inc) -// 从数组中增加一个元素 push($push) -// 从数组中删除一个元素 pull($pull) -func (ms *MongoTransaction) UpdateMany(filter interface{}, update interface{}) (result *mongo.UpdateResult, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - result, err = collection.UpdateMany(ms.Session, filter, update) - return -} - -// Find 查询 -func (ms *MongoTransaction) Find(filter interface{}) (*mongo.Cursor, error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - return collection.Find(ms.Session, filter) -} - -// FindOne 查询单个文档 -func (ms *MongoTransaction) FindOne(filter interface{}) *mongo.SingleResult { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - return collection.FindOne(ms.Session, filter) -} - -// FindMany 查询多个文档 -func (ms *MongoTransaction) FindMany(filter interface{}) (*mongo.Cursor, error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - return collection.Find(ms.Session, filter) -} - -// FindManyByFilters 多条件查询 -func (ms *MongoTransaction) FindManyByFilters(filter interface{}) (result *mongo.Cursor, err error) { - collection, err := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName).Clone() - result, err = collection.Find(ms.Session, bson.M{"$and": filter}) - return result, err -} - -// FindManyByFiltersSort 多条件查询支持排序 -func (ms *MongoTransaction) FindManyByFiltersSort(filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) { - collection, err := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName).Clone() - findOptions := options.Find() - findOptions.SetSort(Sort) - result, err = collection.Find(ms.Session, filter, findOptions) - return result, err -} - -// FindCollection 查询集合文档 -func (ms *MongoTransaction) FindCollection(Limit int64) (result *mongo.Cursor, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - findOptions := options.Find() - findOptions.SetLimit(Limit) - result, err = collection.Find(ms.Session, bson.D{{}}, findOptions) - return result, err -} - -// FindCollectionSort 查询集合文档支持排序 -func (ms *MongoTransaction) FindCollectionSort(Sort interface{}, Limit int64) (result *mongo.Cursor, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - findOptions := options.Find() - findOptions.SetSort(Sort) - findOptions.SetLimit(Limit) - result, err = collection.Find(ms.Session, bson.D{{}}, findOptions) - return result, err -} - -// FindManyCollectionSort 查询集合文档支持排序支持条件 -func (ms *MongoTransaction) FindManyCollectionSort(filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - findOptions := options.Find() - findOptions.SetSort(Sort) - result, err = collection.Find(ms.Session, filter, findOptions) - return result, err -} - -// CollectionCount 查询集合里有多少数据 -func (ms *MongoTransaction) CollectionCount(ctx context.Context) (name string, size int64) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - name = collection.Name() - size, _ = collection.EstimatedDocumentCount(ctx) - return name, size -} - -// CollectionDocuments 按选项查询集合 -// Skip 跳过 -// Limit 读取数量 -// sort 1 ,-1 . 1 为升序 , -1 为降序 -func (ms *MongoTransaction) CollectionDocuments(Skip, Limit int64, sort int, key string, value interface{}) (result *mongo.Cursor, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - SORT := bson.D{{"_id", sort}} - filter := bson.D{{key, value}} - findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) - result, err = collection.Find(ms.Session, filter, findOptions) - return result, err -} - -// AggregateByFiltersSort 统计分析 -func (ms *MongoTransaction) AggregateByFiltersSort(pipeline interface{}) (result *mongo.Cursor, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - result, err = collection.Aggregate(ms.Session, pipeline) - return result, err -} - -// CountDocumentsByFilters 统计数量 -func (ms *MongoTransaction) CountDocumentsByFilters(filter interface{}) (count int64, err error) { - collection := ms.db.Database(ms.getDatabaseName()).Collection(ms.collectionName) - count, err = collection.CountDocuments(ms.Session, filter) - return count, err -} diff --git a/mongo_transaction_get.go b/mongo_transaction_get.go deleted file mode 100644 index 3926370..0000000 --- a/mongo_transaction_get.go +++ /dev/null @@ -1,11 +0,0 @@ -package dorm - -// 获取库名 -func (ms *MongoTransaction) getDatabaseName() string { - return ms.databaseName -} - -// 获取表名 -func (ms *MongoTransaction) getCollectionName() string { - return ms.collectionName -} diff --git a/mongo_transaction_set.go b/mongo_transaction_set.go deleted file mode 100644 index e36daa6..0000000 --- a/mongo_transaction_set.go +++ /dev/null @@ -1,28 +0,0 @@ -package dorm - -import "reflect" - -// Database 设置库名 -func (ms *MongoTransaction) Database(databaseName string) *MongoTransaction { - ms.databaseName = databaseName - return ms -} - -// Collection 设置表名 -func (ms *MongoTransaction) Collection(collectionName string) *MongoTransaction { - ms.collectionName = collectionName - return ms -} - -// Model 传入模型自动获取库名和表名 -func (ms *MongoTransaction) Model(value interface{}) *MongoTransaction { - // https://studygolang.com/articles/896 - val := reflect.ValueOf(value) - if methodValue := val.MethodByName("Database"); methodValue.IsValid() { - ms.databaseName = methodValue.Call(nil)[0].String() - } - if methodValue := val.MethodByName("TableName"); methodValue.IsValid() { - ms.collectionName = methodValue.Call(nil)[0].String() - } - return ms -} diff --git a/vendor/github.com/go-playground/validator/v10/README.md b/vendor/github.com/go-playground/validator/v10/README.md index 8b730b6..9d0a79e 100644 --- a/vendor/github.com/go-playground/validator/v10/README.md +++ b/vendor/github.com/go-playground/validator/v10/README.md @@ -1,7 +1,7 @@ Package validator ================= [![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -![Project status](https://img.shields.io/badge/version-10.11.0-green.svg) +![Project status](https://img.shields.io/badge/version-10.11.1-green.svg) [![Build Status](https://travis-ci.org/go-playground/validator.svg?branch=master)](https://travis-ci.org/go-playground/validator) [![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-playground/validator?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator) diff --git a/vendor/github.com/go-playground/validator/v10/baked_in.go b/vendor/github.com/go-playground/validator/v10/baked_in.go index f2f0939..c9b1db4 100644 --- a/vendor/github.com/go-playground/validator/v10/baked_in.go +++ b/vendor/github.com/go-playground/validator/v10/baked_in.go @@ -1484,10 +1484,15 @@ func isAlphaUnicode(fl FieldLevel) bool { return alphaUnicodeRegex.MatchString(fl.Field().String()) } -// isBoolean is the validation function for validating if the current field's value can be safely converted to a boolean. +// isBoolean is the validation function for validating if the current field's value is a valid boolean value or can be safely converted to a boolean value. func isBoolean(fl FieldLevel) bool { - _, err := strconv.ParseBool(fl.Field().String()) - return err == nil + switch fl.Field().Kind() { + case reflect.Bool: + return true + default: + _, err := strconv.ParseBool(fl.Field().String()) + return err == nil + } } // isDefault is the opposite of required aka hasValue