File tree Expand file tree Collapse file tree 5 files changed +27
-4
lines changed Expand file tree Collapse file tree 5 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import (
10
10
)
11
11
12
12
var (
13
- mdb *mgo.DB
13
+ mdb *mgo.Database
14
14
mdbOnce sync.Once
15
15
)
16
16
@@ -27,7 +27,7 @@ func InitDB() {
27
27
}
28
28
29
29
// GetDB get db
30
- func GetDB() *mgo.DB {
30
+ func GetDB() *mgo.Database {
31
31
if mdb == nil {
32
32
mdbOnce.Do(func() {
33
33
InitDB()
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ var ErrRecordNotFound = mgo.ErrNoDocuments
18
18
19
19
// InitMongodb connect mongodb
20
20
// For more information on connecting to mongodb, see https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Connect
21
- func InitMongodb() *mgo.DB {
21
+ func InitMongodb() *mgo.Database {
22
22
dsn := utils.AdaptiveMongodbDsn(config.Get().Database.Mongodb.Dsn)
23
23
mdb, err := mgo.Init(dsn)
24
24
if err != nil {
Original file line number Diff line number Diff line change @@ -33,11 +33,28 @@ func GetFilename(filePath string) string {
33
33
return name
34
34
}
35
35
36
+ // GetFileSuffixName get file suffix name, example: ".txt"
37
+ func GetFileSuffixName (filePath string ) string {
38
+ return filepath .Ext (filePath )
39
+ }
40
+
36
41
// GetDir get dir, not include the last separator
37
42
func GetDir (filePath string ) string {
38
43
return filepath .Dir (filePath )
39
44
}
40
45
46
+ // GetSuffixDir get suffix dir, not include the last separator
47
+ func GetSuffixDir (filePath string ) string {
48
+ fileInfo , err := os .Stat (filePath )
49
+ if err != nil {
50
+ return filepath .Base (filePath )
51
+ }
52
+ if ! fileInfo .IsDir () {
53
+ filePath = strings .TrimSuffix (filePath , fileInfo .Name ())
54
+ }
55
+ return filepath .Base (filePath )
56
+ }
57
+
41
58
// GetFileDir get dir, include the last separator
42
59
func GetFileDir (filePath string ) string {
43
60
dir , _ := filepath .Split (filePath )
Original file line number Diff line number Diff line change @@ -83,9 +83,15 @@ func TestGetFilename(t *testing.T) {
83
83
name := GetFilename ("./README.md" )
84
84
assert .Equal (t , "README.md" , name )
85
85
86
+ name = GetFileSuffixName ("./README.md" )
87
+ assert .Equal (t , ".md" , name )
88
+
86
89
name = GetDir ("gofile/README.md" )
87
90
assert .Equal (t , "gofile" , name )
88
91
92
+ name = GetSuffixDir ("gofile/" )
93
+ assert .Equal (t , "gofile" , name )
94
+
89
95
name = GetFileDir ("gofile/README.md" )
90
96
assert .Equal (t , "gofile/" , name )
91
97
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import (
12
12
"go.uber.org/zap"
13
13
)
14
14
15
- type DB = mongo.Database
15
+ type Database = mongo.Database
16
16
17
17
var ErrNoDocuments = mongo .ErrNoDocuments
18
18
You can’t perform that action at this time.
0 commit comments