improve database configuration parsing for package gins/gdb

This commit is contained in:
John Guo
2022-06-20 12:07:51 +08:00
parent bb5cd3e224
commit 1b327b8abd
3 changed files with 25 additions and 23 deletions

View File

@ -22,8 +22,6 @@ import (
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/grand"
)
@ -398,8 +396,7 @@ func doNewByNode(node ConfigNode, group string) (db DB, err error) {
config: &node,
}
if v, ok := driverMap[node.Type]; ok {
c.db, err = v.New(c, &node)
if err != nil {
if c.db, err = v.New(c, &node); err != nil {
return nil, err
}
return c.db, nil
@ -527,14 +524,6 @@ func (c *Core) getSqlDb(master bool, schema ...string) (sqlDb *sql.DB, err error
} else {
node = c.config
}
// Parse `Link` configuration syntax.
if node.Link != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.Link)
if len(match) == 3 {
node.Type = gstr.Trim(match[1])
node.Link = gstr.Trim(match[2])
}
}
// Default value checks.
if node.Charset == "" {
node.Charset = defaultCharset

View File

@ -13,6 +13,8 @@ import (
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
// Config is the configuration management object.
@ -72,6 +74,12 @@ func SetConfig(config Config) {
defer instances.Clear()
configs.Lock()
defer configs.Unlock()
for k, nodes := range config {
for i, node := range nodes {
nodes[i] = parseConfigNode(node)
}
config[k] = nodes
}
configs.config = config
}
@ -80,6 +88,9 @@ func SetConfigGroup(group string, nodes ConfigGroup) {
defer instances.Clear()
configs.Lock()
defer configs.Unlock()
for i, node := range nodes {
nodes[i] = parseConfigNode(node)
}
configs.config[group] = nodes
}
@ -88,7 +99,19 @@ func AddConfigNode(group string, node ConfigNode) {
defer instances.Clear()
configs.Lock()
defer configs.Unlock()
configs.config[group] = append(configs.config[group], node)
configs.config[group] = append(configs.config[group], parseConfigNode(node))
}
// parseConfigNode parses `Link` configuration syntax.
func parseConfigNode(node ConfigNode) ConfigNode {
if node.Link != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.Link)
if len(match) == 3 {
node.Type = gstr.Trim(match[1])
node.Link = gstr.Trim(match[2])
}
}
return node
}
// AddDefaultConfigNode adds one node configuration to configuration of default group.

View File

@ -16,8 +16,6 @@ import (
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
@ -173,13 +171,5 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
if _, v := gutil.MapPossibleItemByKey(nodeMap, "Link"); v != nil {
node.Link = gconv.String(v)
}
// Parse `Link` configuration syntax.
if node.Link != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.Link)
if len(match) == 3 {
node.Type = gstr.Trim(match[1])
node.Link = gstr.Trim(match[2])
}
}
return node
}