Skip to content

Commit 7c4712a

Browse files
committed
style: adjust code
1 parent e3b6e11 commit 7c4712a

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

internal/database/init.go.mgo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
var (
13-
mdb *mgo.DB
13+
mdb *mgo.Database
1414
mdbOnce sync.Once
1515
)
1616

@@ -27,7 +27,7 @@ func InitDB() {
2727
}
2828

2929
// GetDB get db
30-
func GetDB() *mgo.DB {
30+
func GetDB() *mgo.Database {
3131
if mdb == nil {
3232
mdbOnce.Do(func() {
3333
InitDB()

internal/database/mongodb.go.mgo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var ErrRecordNotFound = mgo.ErrNoDocuments
1818

1919
// InitMongodb connect mongodb
2020
// 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 {
2222
dsn := utils.AdaptiveMongodbDsn(config.Get().Database.Mongodb.Dsn)
2323
mdb, err := mgo.Init(dsn)
2424
if err != nil {

pkg/gofile/filePath.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,28 @@ func GetFilename(filePath string) string {
3333
return name
3434
}
3535

36+
// GetFileSuffixName get file suffix name, example: ".txt"
37+
func GetFileSuffixName(filePath string) string {
38+
return filepath.Ext(filePath)
39+
}
40+
3641
// GetDir get dir, not include the last separator
3742
func GetDir(filePath string) string {
3843
return filepath.Dir(filePath)
3944
}
4045

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+
4158
// GetFileDir get dir, include the last separator
4259
func GetFileDir(filePath string) string {
4360
dir, _ := filepath.Split(filePath)

pkg/gofile/filePath_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ func TestGetFilename(t *testing.T) {
8383
name := GetFilename("./README.md")
8484
assert.Equal(t, "README.md", name)
8585

86+
name = GetFileSuffixName("./README.md")
87+
assert.Equal(t, ".md", name)
88+
8689
name = GetDir("gofile/README.md")
8790
assert.Equal(t, "gofile", name)
8891

92+
name = GetSuffixDir("gofile/")
93+
assert.Equal(t, "gofile", name)
94+
8995
name = GetFileDir("gofile/README.md")
9096
assert.Equal(t, "gofile/", name)
9197

pkg/mgo/mongo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"go.uber.org/zap"
1313
)
1414

15-
type DB = mongo.Database
15+
type Database = mongo.Database
1616

1717
var ErrNoDocuments = mongo.ErrNoDocuments
1818

0 commit comments

Comments
 (0)