From 6ad8734375a5ea0059211b18e469071248a44fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 8 Nov 2024 14:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=A7=A3=E6=9E=90=20+=20=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.go | 118 +++++++++++++++----------------------------------------- 1 file changed, 32 insertions(+), 86 deletions(-) diff --git a/init.go b/init.go index 36ba5de..145c5bd 100644 --- a/init.go +++ b/init.go @@ -9,43 +9,13 @@ package config import ( "errors" - "fmt" "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/caarlos0/env/v9" "github.com/jessevdk/go-flags" "github.com/joho/godotenv" - "github.com/spf13/viper" - "reflect" + "path/filepath" ) -// InitByViper 通过viper库初始化配置 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:44 2024/11/7 -// -// 配置解析优先级 : 显示的值设置 > 命令行标记 > 环境变量 > 配置文件 > 键值存储 > 默认值 -// -// 参数: -// - configReceiver 接收参数结果的指针 -// - envFileList 环境变量文件路径, 可以指定多个环境变量文件, 不指定默认加载服务运行目录下 .env 文件 -// - configFilePath 配置文件目录 -// - configFileNameList 要加载的配置文件列表 -func InitByViper(configReceiver any, envFileList []string, configFilePath string, configFileNameList []string) error { - viper.AddConfigPath(configFilePath) - for _, configFileName := range configFileNameList { - viper.SetConfigName(configFileName) - } - // 解析配置 - if err := viper.ReadInConfig(); nil != err { - return err - } - // 获取全部配置,并解析 - if err := serialize.JSON.Transition(viper.AllSettings(), configReceiver); nil != err { - return err - } - return nil -} - // Init 初始化配置 // // Author : go_developer@163.com<白茶清欢> @@ -53,72 +23,48 @@ func InitByViper(configReceiver any, envFileList []string, configFilePath string // Date : 16:25 2024/11/5 // // 参数: -// - configReceiver 接收参数结果的指针 +// - configReceiver 接收最终配置结果的指针 +// - cliReceiver 命令函参数配置结果 +// - envReceiver 环境变量配置结果 +// - fileReceiver 文件配置结果 // - envFileList 环境变量文件路径, 可以指定多个环境变量文件, 不指定默认加载服务运行目录下 .env 文件 -// - configFile 配置文件路径 -func Init(configReceiver any, envFileList []string, configFile string) error { +// - configFilePath 配置文件目录 +// - configFileNameList 配置文件列表 +func Init(configReceiver any, cliReceiver any, envReceiver any, fileReceiver any, envFileList []string, configFilePath string, configFileNameList []string) error { var ( - err error - fileConfigMap = map[string]any{} + err error ) - if nil == envFileList { - envFileList = make([]string, 0) + if len(envFileList) == 0 { + envFileList = []string{".env"} } - // 检查结构体指针是否合法 - if err = checkConfigReceiver(configReceiver); err != nil { - return err + if nil != envFileList { + // 加载环境变量 + if err = godotenv.Load(envFileList...); nil != err { + return err + } + if err = env.Parse(envReceiver); nil != err { + return errors.New("env param parse fail : " + err.Error()) + } } - // 加载环境变量 - if err = godotenv.Load(envFileList...); nil != err { - return err - } + if nil != fileReceiver && len(configFileNameList) > 0 { + // 读取配置文件内容 + for _, itemConfigFileName := range configFileNameList { + configFile := filepath.Join(configFilePath, itemConfigFileName) + if err = serialize.File.ReadAnyFileContent(configFile, &fileReceiver); nil != err { + return errors.New(configFile + " -> parse config file fail : " + err.Error()) + } + } + } // 解析命令行参数 - if _, err = flags.Parse(configReceiver); nil != err { + if _, err = flags.Parse(cliReceiver); nil != err { return err } - - // 读取配置文件内容 - if err = serialize.File.ReadAnyFileContent(configFile, &fileConfigMap); nil != err { - return err - } - - // 解析配置文件参数 - configTypeReflect := reflect.TypeOf(configReceiver).Elem() - configValueReflect := reflect.ValueOf(configReceiver).Elem() - configFieldNum := configValueReflect.NumField() - for i := 0; i < configFieldNum; i++ { - fmt.Println(configTypeReflect.Field(i).Name) - } - - return nil -} - -// checkConfigReceiver 校验配置接收者必须是指针 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:16 2024/11/5 -func checkConfigReceiver(configReceiver any) error { - if nil == configReceiver { - return errors.New("config receiver is nil") - } - receiverType := reflect.TypeOf(configReceiver) - if receiverType.Kind() != reflect.Ptr { - return errors.New("config receiver is not a pointer") + if err = serialize.JSON.MergeDataForReceiver(configReceiver, fileReceiver, envReceiver, configReceiver); nil != err { + return errors.New("merge config data fail : " + err.Error()) } return nil } - -// cloneReceiver ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:16 2024/11/5 -func cloneReceiver(configReceiver any) any { - dataType := reflect.TypeOf(configReceiver).Elem() - return reflect.New(dataType).Interface() -}