From efdbb35412c867aae0d7c1886539217a22178203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 15 Jun 2022 16:40:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 3b85a57..1a8dbd1 100644 --- a/client.go +++ b/client.go @@ -10,6 +10,7 @@ package redis import ( "encoding/json" "fmt" + "path/filepath" "strings" "sync" "time" @@ -30,7 +31,7 @@ func init() { Client = &OwnClient{ lock: &sync.RWMutex{}, instanceTable: make(map[string]*RealClient), - confTable: make(map[string]Options), + confTable: make(map[string]*FullConfig), parseErrorFunc: defaultParseError, } } @@ -83,7 +84,7 @@ type RealClient struct { // Author : go_developer@163.com<白茶清欢> // // Date : 5:05 下午 2021/2/27 -func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) { +func NewClient(config map[string]*FullConfig, parseErrorFunc func(err error) error) (ClientInterface, error) { c := &OwnClient{ instanceTable: make(map[string]*RealClient), loggerTable: make(map[string]*zap.Logger), @@ -105,7 +106,7 @@ type OwnClient struct { lock *sync.RWMutex // 锁 loggerTable map[string]*zap.Logger // 日志实例 instanceTable map[string]*RealClient // redis 实例 - confTable map[string]Options // redis 配置 + confTable map[string]*FullConfig // redis 配置 parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis: } @@ -120,9 +121,37 @@ func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) { cfg FullConfig ) - if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err { - return nil, err + filePathArr := strings.Split(cfgPath, string(filepath.Separator)) + if len(filePathArr) == 0 { + return nil, CfgFilePathError() } + fileName := strings.ToLower(filePathArr[len(filePathArr)-1]) + fileArr := strings.Split(fileName, ".") + if len(filePathArr) < 2 { + return nil, CfgFileFormatErr("未知") + } + flag := strings.Trim( + strings.Trim( + strings.Trim(fileName, ".json"), + ".yaml"), ".yml") + switch strings.ToLower(fileArr[len(fileArr)-1]) { + case "json": + if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err { + return nil, err + } + flag = strings.Join(fileArr[0:len(fileArr)-1], ".") + case "yml": + fallthrough + case "yaml": + if err = util.File.ReadYmlContent(cfgPath, &cfg); nil != err { + return nil, err + } + flag = strings.Join(fileArr[0:len(fileArr)-1], ".") + default: + return nil, CfgFileFormatErr(fileArr[len(fileArr)-1]) + } + + c.confTable[flag] = &cfg return &cfg, nil } @@ -133,12 +162,12 @@ func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) { // Date : 5:31 下午 2021/2/27 func (c *OwnClient) init() error { - for flag, conf := range c.confTable { + for flag, _ := range c.confTable { c.instanceTable[flag] = &RealClient{ - Flag: flag, - Instance: redisInstance.NewClient(conf.Conf), - Logger: nil, - LoggerFieldConfig: conf.LoggerFieldConfig, + Flag: flag, + //Instance: redisInstance.NewClient(conf.Conf), + Logger: nil, + //LoggerFieldConfig: conf.LoggerFieldConfig, } if nil == c.instanceTable[flag].LoggerFieldConfig { c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{