11package config
22
3- type DB struct {
4- Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"`
5- Type string `mapstructure:"type" json:"type" yaml:"type"`
6- AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
3+ type DsnProvider interface {
4+ Dsn () string
5+ }
6+
7+ // Embeded 结构体可以压平到上一层,从而保持 config 文件的结构和原来一样
8+ // 见 playground: https://go.dev/play/p/KIcuhqEoxmY
9+
10+ // GeneralDB 也被 Pgsql 和 Mysql 原样使用
11+ type GeneralDB struct {
712 Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
813 Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
914 Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
@@ -13,9 +18,12 @@ type DB struct {
1318 MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
1419 MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
1520 LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
16- LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"`
21+ LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
1722}
1823
19- func (m * DB ) Dsn () string {
20- return m .Username + ":" + m .Password + "@tcp(" + m .Path + ":" + m .Port + ")/" + m .Dbname + "?" + m .Config
24+ type SpecializedDB struct {
25+ Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"`
26+ Type string `mapstructure:"type" json:"type" yaml:"type"`
27+ AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
28+ GeneralDB `yaml:",inline" mapstructure:",squash"`
2129}
0 commit comments