# configkit **Repository Path**: g_boot/configkit ## Basic Information - **Project Name**: configkit - **Description**: 配置解析库 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-06-15 - **Last Updated**: 2023-10-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # configkit #### 介绍 configkit是一个用于解析配置文件的工具,它可以帮助程序读取和解析配置文件,并提供一些方便的功能,如环境变量替换和默认值设置。该库的主要功能包括: 配置文件解析: 1. 该库可以读取和解析各种类型的配置文件,如.properties、.yaml、.json等。它提供了简单而灵活的API,使程序可以轻松地读取配置文件内容。 2. 环境变量替换:该库支持配置文件中使用${var}占位符来引用环境变量。在解析配置文件时,库会自动替换这些占位符为对应的环境变量的值,使程序能够动态地使用环境变量的值。 3. 默认值设置:该库允许使用${var:def}的方式为配置文件中的属性设置默认值。如果配置文件中没有定义某个属性,程序可以通过设置默认值来确保该属性始终有一个合理的值。 4. 多种配置文件格式支持:该库支持多种常见的配置文件格式,如.properties、.yaml、.json等。无论使用哪种格式的配置文件,程序都可以通过该库进行解析和读取。 灵活的配置项访问:该库提供了灵活的配置项访问方式,可以通过键值对、层级结构或对象属性的方式来获取配置项的值。这使得程序可以根据自己的需要选择最适合的访问方式。 #### 软件架构 使用分层结构来实现不同的功能和责任。以下是各个层次的功能和职责: 1. Loader(资源加载层):Loader负责从不同的资源来源加载配置文件。它可以从文件系统、网络等不同的位置获取配置数据。Loader层的主要职责是将配置数据读取到内存中,并将其传递给Resolver层进行解析。 2. Resolver(配置解析层):Resolver负责解析配置数据,并将其转换为更易于操作和理解的格式。Resolver层的主要职责是将解析后的配置数据传递给Config层进行组合。 3. Config(配置对象):Config层负责将不同来源的配置数据进行组合和合并,以生成最终的配置对象。Config层的主要职责是将组合后的配置对象传递给Adapter层进行适配。 4. Adapter(框架适配器):Adapter层负责将Config层生成的配置对象适配到具体的应用程序框架中。目前主要针对gofram进行适配 整体架构中,Loader层负责加载配置资源,Resolver层负责解析配置数据,Config层负责组合配置信息,而Adapter层负责将配置信息适配到应用程序中。这种分层结构可以使得每个层次的功能和职责清晰明确,便于维护和扩展。同时,分层结构也使得不同的层次可以独立进行开发和测试,提高了系统的可靠性和可维护性。 #### 例子说明 test.yaml文件 ``` # 基础类型 v_s: "hello" v_i: 101 v_f: 3.14 v_list: ['mango','apple','banana'] v_obj: v_o_p1: "p1" v_o_p2: 8848 v_o_p3: 0.333 v_o_p4: v_o_o_p1: "o_o_p_v" # 环境变量替换. v_gopath: ${gopath} # 默认值 v_exp1: ${v_gopath:/app/home} v_exp2: ${v_gopath1:/app/home} # 变量拼接 v_exp3: /files v_exp4: ${v_exp1:/app/home}${v_exp2}/objects # 数据库实例 database: logger: level: "${logger_level}" stdout: true Path: "resource/log/sql" default: link: "mysql:${mysql_user:root}:${mysql_pwd}@tcp(127.0.0.1:3306)/data" debug: true ``` ``` func TestGetValue(t *testing.T) { opts := slog.HandlerOptions{ Level: slog.LevelDebug, } slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr,&opts))) loadService := loader.YmlLoader{} resolverService := resolver.StringInterpolation{} rootPath := system.RootPath() // 读取当前目录下的test.yaml filepath := "file://"+ rootPath + "/test.yaml" yaml_config := config.YamlConfig{} yaml_config.Loader = &loadService yaml_config.Resolver = &resolverService yaml_config.URI = filepath // 设置环境变量 os.Setenv("logger_level", "debug") os.Setenv("mysql_user", "admin") os.Setenv("mysql_pwd", "123456") yaml_config.Build(nil) realValue := yaml_config.GetValue("v_s") if realValue == nil || !strings.EqualFold("hello", realValue.(string)) { t.Fatalf("获取字符串类型错误:期望值(%s),实际值(%s)", "hello", realValue) } realValue = yaml_config.GetValue("v_i") if realValue == nil || 101 != realValue.(int) { t.Fatalf("获取整型类型错误:期望值(%s),实际值(%s)", "101", realValue) } realValue = yaml_config.GetValue("v_f") if realValue == nil || 3.14 != realValue.(float64) { t.Fatalf("获取实型类型错误:期望值(%s),实际值(%s)", "3.14", realValue) } realValue = yaml_config.GetValue("v_obj.v_o_p1") if realValue == nil || !strings.EqualFold("p1", realValue.(string)) { t.Fatalf("获取字符串类型错误:期望值(%s),实际值(%s)", "p1", realValue) } realValue = yaml_config.GetValue("v_gopath") gopath_v := os.Getenv("GOPATH") if realValue == nil || !strings.EqualFold(gopath_v, realValue.(string)) { t.Fatalf("获取环境变量错误:期望值(%s),实际值(%s)", gopath_v, realValue) } realValue = yaml_config.GetValue("v_exp1") t.Logf("不使用默认值:(%s)", realValue) if realValue == nil || !strings.EqualFold(gopath_v, realValue.(string)) { t.Fatalf("获取环境变量错误:期望值(%s),实际值(%s)", gopath_v, realValue) } realValue = yaml_config.GetValue("v_exp2") t.Logf("使用默认值:(%s)", realValue) if realValue == nil || !strings.EqualFold("/app/home", realValue.(string)) { t.Fatalf("获取环境默认值错误:期望值(%s),实际值(%s)", "/app/home", realValue) } realValue = yaml_config.GetValue("v_exp4") t.Logf("变量拼接值:(%s)", realValue) if realValue == nil || !strings.EqualFold(os.Getenv("gopath")+"/app/home/objects", realValue.(string)) { t.Fatalf("获取环境默认值错误:期望值(%s),实际值(%s)", "E:\\repository\\go/app/home/objects", realValue) } realValue = yaml_config.GetValue("database.logger.level") t.Logf("数据库实例日志等级:(%s)", realValue) if realValue == nil || !strings.EqualFold("debug", realValue.(string)) { t.Fatalf("获取环境默认值错误:期望值(%s),实际值(%s)", "debug", realValue) } realValue = yaml_config.GetValue("database.default.link") t.Logf("数据库实例link:(%s)", realValue) if realValue == nil || !strings.EqualFold("mysql:admin:123456@tcp(192.168.10.241:30138)/gfast", realValue.(string)) { t.Fatalf("获取环境默认值错误:期望值(%s),实际值(%s)", "mysql:admin:123456@tcp(192.168.10.241:30138)/gfast", realValue) } } ``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request