option初始化需要赋予部分默认值

This commit is contained in:
yun
2024-03-14 23:12:37 +08:00
parent 3543f629c6
commit 92d79d2733
+10 -1
View File
@@ -5,7 +5,7 @@ type clientOptions struct {
Mysql *MysqlOptions Mysql *MysqlOptions
Sqlite *SqliteOptions Sqlite *SqliteOptions
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles` Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article` SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
} }
type MysqlOptions struct { type MysqlOptions struct {
@@ -38,18 +38,27 @@ func SetMysql(user, password, host, database string, port int) Option {
Database: database, Database: database,
Password: password, Password: password,
} }
if o.Mysql.Port == 0 {
o.Mysql.Port = 3306
}
} }
} }
// 使用sqlite数据库
func SetSqlite(dbPath string) Option { func SetSqlite(dbPath string) Option {
return func(o *clientOptions) { return func(o *clientOptions) {
o.Db = "sqlite" o.Db = "sqlite"
o.Sqlite = &SqliteOptions{ o.Sqlite = &SqliteOptions{
DbPath: dbPath, DbPath: dbPath,
} }
if o.Sqlite.DbPath == "" {
o.Sqlite.DbPath = "sqlite.db"
}
} }
} }
// 表名前缀
func SetTablePrefix(perfix string) Option { func SetTablePrefix(perfix string) Option {
return func(o *clientOptions) { return func(o *clientOptions) {
o.Prefix = perfix o.Prefix = perfix