-
Notifications
You must be signed in to change notification settings - Fork 85
/
config.go
76 lines (63 loc) · 2.2 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package db
import (
"fmt"
"github.com/google/wire"
"github.com/goravel/framework/contracts/config"
databasecontract "github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/contracts/database/orm"
)
var ConfigSet = wire.NewSet(NewConfigImpl, wire.Bind(new(Config), new(*ConfigImpl)))
var _ Config = &ConfigImpl{}
type Config interface {
Reads() []databasecontract.Config
Writes() []databasecontract.Config
}
type ConfigImpl struct {
config config.Config
connection string
}
func NewConfigImpl(config config.Config, connection string) *ConfigImpl {
return &ConfigImpl{
config: config,
connection: connection,
}
}
func (c *ConfigImpl) Reads() []databasecontract.Config {
configs := c.config.Get(fmt.Sprintf("database.connections.%s.read", c.connection))
if configs, ok := configs.([]databasecontract.Config); ok {
return c.fillDefault(configs)
}
return []databasecontract.Config{}
}
func (c *ConfigImpl) Writes() []databasecontract.Config {
configs := c.config.Get(fmt.Sprintf("database.connections.%s.write", c.connection))
if configs, ok := configs.([]databasecontract.Config); ok {
return c.fillDefault(configs)
}
return c.fillDefault([]databasecontract.Config{{}})
}
func (c *ConfigImpl) fillDefault(configs []databasecontract.Config) []databasecontract.Config {
var newConfigs []databasecontract.Config
driver := c.config.GetString(fmt.Sprintf("database.connections.%s.driver", c.connection))
for _, item := range configs {
if driver != orm.DriverSqlite.String() {
if item.Host == "" {
item.Host = c.config.GetString(fmt.Sprintf("database.connections.%s.host", c.connection))
}
if item.Port == 0 {
item.Port = c.config.GetInt(fmt.Sprintf("database.connections.%s.port", c.connection))
}
if item.Username == "" {
item.Username = c.config.GetString(fmt.Sprintf("database.connections.%s.username", c.connection))
}
if item.Password == "" {
item.Password = c.config.GetString(fmt.Sprintf("database.connections.%s.password", c.connection))
}
}
if item.Database == "" {
item.Database = c.config.GetString(fmt.Sprintf("database.connections.%s.database", c.connection))
}
newConfigs = append(newConfigs, item)
}
return newConfigs
}