使用section.key的形式读取ini配置项

Go配置管理:从全局变量到高效检索

配置文件读取是很多Go项目必备的功能,这方面社区提供的方案也相对成熟稳定。但之前写这部分代码时除了使用了针对不同配置文件格式(比如:ini、toml等)的驱动包之外,很少直接使用第三方包对读取出的配置项的值进行管理。于是我们就面对这样一个问题:其他包如果要使用这些被读取出的配置项的值该如何做呢?我们以读取ini格式承载的配置文件为例,来简单说说。

1. 全局变量法

这是最粗糙的方法,但却是最易理解的方法。我们建立一个config包,在main函数中读取配置文件并将读取到的配置项信息存放在config包的一个导出的全局变量中,这样其他包要想获取配置文件中配置项的信息,直接通过该全局变量读取即可。下面的demo1就是一个使用全局变量组织读取出的配置项信息的示例项目,其代码结构如下:

// github.com/bigwhite/experiments/tree/master/read-ini/demo1

demo1
├── conf
│   └── demo.ini
├── go.mod
├── go.sum
├── main.go
└── pkg
    ├── config
    │   └── config.go
    └── pkg1
        └── pkg1.go

demo1中的conf/demo.ini中存储了下面这些配置项信息:

$cat demo.ini 
[server]
id = 100001 
port = 23333
tls_port = 83333

[log] 
level = 0; info:0, warn: 1, error: 2, dpanic:3, panic:4, fatal: 5, debug: -1
compress = true ; indicate whether the rotated log files should be compressed using gzip (default true)
path = "./log/demo.log" ; if it is empty, we use default logger(to stderr)
max_age = 3 ; the maximum number of days to retain old log files based on the timestamp encoded in their filename
maxbackups = 7 ; the maximum number of old log files to retain (default 7)
maxsize = 100 ; the maximum size in megabytes of the log file before it gets rotated (default 100)

[debug]
profile_on = true ;add profile web server for app to enable pprof through web
profile_port = 8091 ; profile web port

我们通过config包读取该配置文件(基于github.com/go-ini/ini包实现ini配置文件读取):

// github.com/bigwhite/experiments/tree/master/read-ini/demo1/pkg/config/config.go
package config

import (
 ini "github.com/go-ini/ini"
)

type Server struct {
 Id      string `ini:""`
 Port    int    `ini:"port"`
 TlsPort int    `ini:"tls_port"`
}

type Log struct {
 Compress   bool   `ini:"compress"`
 LogPath    string `ini:"path"`
 MaxAge     int    `ini:"max_age"`
 MaxBackups int    `ini:"maxbackups"`
 MaxSize    int    `ini:"maxsize"`
}

type Debug struct {
 ProfileOn   bool   `ini:"profile_on"`
 ProfilePort string `ini:"profile_port"`
}

type IniConfig struct {
 Server `ini:"server"`
 Log    `ini:"log"`
 Debug  `ini:"debug"`
}

var Config = &IniConfig{}

func InitFromFile(path string) error {
 cfg, err := ini.Load(path)
 if err != nil {
  return err
 }

 return cfg.MapTo(Config)
}

这是一种典型的Go通过struct field tag与ini配置文件中p和key绑定读取的示例,我们在main包中调用InitFromFile读取ini配置文件:

// github.com/bigwhite/experiments/tree/master/read-ini/demo1/main.go

package main
  
import (
    "github.com/bigwhite/readini/pkg/config"
    "github.com/bigwhite/readini/pkg/pkg1"
)

func main() {
    err := config.InitFromFile("conf/demo.ini")
    if err != nil {
        panic(err)
    }
    pkg1.Foo()
}

读取后的配置项信息存储在config.Config这个全局变量中。在其他包中(比如pkg/pkg1/pkg1.go),我们可直接访问该全局变量获取配置项信息:

// github.com/bigwhite/experiments/tree/master/read-ini/demo1/pkg/pkg1/pkg1.go
package pkg1

import (
 "fmt"

 "github.com/bigwhite/readini/pkg/config"
)

func Foo() {
 fmt.Printf("%#v\n", config.Config)
}

这种方式很简单、直观也易于理解,但以全局变量形式将配置项信息暴露给其他包,从代码设计层面,这总是会予人口实的。那么我们是否可以只暴露包函数,而不暴露具体实现呢?

2. 通过p.key形式读取配置项

由于是采用的tag与结构体字段的绑定方法,实际配置项名字与绑定的字段名字可能是不一致的,比如下面代码段中的结构体字段TlsPort与其tag tls_port:

type Server struct {
 Id      string `ini:"id"`
 Port    int    `ini:"port"`
 TlsPort int    `ini:"tls_port"`
}

这样使用config包的用户在要获取配置项值时就必须了解绑定的结构体字段的名字。如果我们不暴露这些绑定结构体的实现细节的话,config包的用户所掌握的信息仅仅就是配置文件(比如:demo.ini)本身了。

于是一个很自然的想法就会萌发出来!我们是否可以通过p.key的形式得到对应配置项的值,比如以下面配置项为例:

[server]
id = 100001 
port = 23333
tls_port = 83333

我们需要通过server.id来获得id这个配置项的值,类似的其他配置项的获取方式是传入server.port、server.tls_port等。这样我们的config包仅需保留类似一个接收xx.yy.zz为参数的GetSectionKey函数即可,就像下面这样:

id, ok := config.GetSectionKey("server.id")

接下来,我们就沿着这个思路在demo1的基础上重构为新方案demo2。下面是修改后的demo2的config包代码:

// github.com/bigwhite/experiments/tree/master/read-ini/demo2/pkg/config/config.go
package config

import (
 "reflect"
 "strings"

 ini "github.com/go-ini/ini"
)

type server struct {
 Id      string `ini:"id"`
 Port    int    `ini:"port"`
 TlsPort int    `ini:"tls_port"`
}

type log struct {
 Compress   bool   `ini:"compress"`
 LogPath    string `ini:"path"`
 MaxAge     int    `ini:"max_age"`
 MaxBackups int    `ini:"maxbackups"`
 MaxSize    int    `ini:"maxsize"`
}

type debug struct {
 ProfileOn   bool   `ini:"profile_on"`
 ProfilePort string `ini:"profile_port"`
}

type iniConfig struct {
 Server server `ini:"server"`
 Log    log    `ini:"log"`
 Dbg    debug  `ini:"debug"`
}

var thisConfig = iniConfig{}

func InitFromFile(path string) error {
 cfg, err := ini.Load(path)
 if err != nil {
  return err
 }

 return cfg.MapTo(&thisConfig)
}

func GetSectionKey(name string) (interface{}, bool) {
 keys := strings.Split(name, ".")
 lastKey := keys[len(keys)-1]
 v := reflect.ValueOf(thisConfig)
 t := reflect.TypeOf(thisConfig)

 found := false
 for _, key := range keys {
  cnt := v.NumField()

  for i := 0; i < cnt; i++ {
   field := t.Field(i)
   if field.Tag.Get("ini") == key {
    t = field.Type
    v = v.Field(i)
    if key == lastKey {
     found = true
    }
    break
   }
  }
 }

 if found {
  return v.Interface(), true
 }
 return nil, false
}

我们将原先暴露出去的全局变量改为了包内变量(thisConfig),几个绑定的结构体类型也都改为非导出的了。我们提供了一个对外的函数:GetSectionKey,这样通过该函数,我们就可以使用p.key的形式获取到对应配置项的值了。在GetSectionKey函数的实现中,我们使用了反射来获取结构体定义中各个字段的tag来和传入的p.key的各个部分做比对,一旦匹配,便将对应的值传出来。如果没有匹配到,则返回false,这里GetSectionKey的返回值列表设计也使用了经典的“comma, ok”模式。

这样,我们在pkg1包中便可以这样来获取对应的配置项的值了:

// github.com/bigwhite/experiments/tree/master/read-ini/demo2/pkg/pkg1/pkg1.go
package pkg1

import (
 "fmt"

 "github.com/bigwhite/readini/pkg/config"
)

func Foo() {
 id, ok := config.GetSectionKey("server.id")
 fmt.Printf("id = [%v], ok = [%t]\n", id, ok)
 tlsPort, ok := config.GetSectionKey("server.tls_port")
 fmt.Printf("tls_port = [%v], ok = [%t]\n", tlsPort, ok)
 logPath, ok := config.GetSectionKey("log.path")
 fmt.Printf("path = [%v], ok = [%t]\n", logPath, ok)
 logPath1, ok := config.GetSectionKey("log.path1")
 fmt.Printf("path1 = [%v], ok = [%t]\n", logPath1, ok)
}

运行demo2,我们将看到如下结果:

$go run main.go          
id = [100001], ok = [true]
tls_port = [83333], ok = [true]
path = [./log/demo.log], ok = [true]
path1 = [<nil>], ok = [false]

现在还有一个问题,那就是config包暴露的函数GetSectionKey的第一个返回值类型为interface{},这样我们得到配置项的值后还得根据其类型通过类型断言方式进行转型,体验略差,我们可以在config包中提供常见类型的“语法糖”函数,比如下面这些:

// github.com/bigwhite/experiments/tree/master/read-ini/demo2/pkg/config/config.go
func GetInt(name string) (int, bool) {
 i, ok := GetSectionKey(name)
 if !ok {
  return 0, false
 }

 if v, ok := i.(int); ok {
  return v, true
 }

 // maybe it is a digital string
 s, ok := i.(string)
 if !ok {
  return 0, false
 }

 n, err := strconv.Atoi(s)
 if err != nil {
  return 0, false
 }
 return n, true
}

func GetString(name string) (string, bool) {
 i, ok := GetSectionKey(name)
 if !ok {
  return "", false
 }

 s, ok := i.(string)
 if !ok {
  return "", false
 }
 return s, true
}

func GetBool(name string) (bool, bool) {
 i, ok := GetSectionKey(name)
 if !ok {
  return false, false
 }

 b, ok := i.(bool)
 if !ok {
  return false, false
 }
 return b, true
}

这样我们在pkg1包中就可以直接使用这些语法糖函数获取对应类型的配置项值了:

// github.com/bigwhite/experiments/tree/master/read-ini/demo2/pkg/pkg1/pkg1.go
b, ok := config.GetBool("debug.profile_on")
fmt.Printf("profile_on = [%t], ok = [%t]\n", b, ok)

3. 优化

配置读取一般都是在系统初始化阶段,对其性能要求不高。后续系统运行过程中,也会偶有获取配置项的业务逻辑。一旦在关键路径上有获取配置项值的逻辑,上面的方案便值得商榷,因为每次通过GetSectionKey获取一个配置项的值都要通过反射进行一番操作,性能肯定不佳。

那么如何优化呢?我们可以通过为每个key建立索引来进行。我们在config包中创建一个除初始化时只读的map变量:

// github.com/bigwhite/experiments/tree/master/read-ini/demo3/pkg/config/config.go
var index = make(map[string]interface{}, 100)

在config包的InitFromFile中我们将配置项以p.key为key的形式索引到该index变量中:

// github.com/bigwhite/experiments/tree/master/read-ini/demo3/pkg/config/config.go

func InitFromFile(path string) error {
    cfg, err := ini.Load(path)
    if err != nil {
        return err
    }

    err = cfg.MapTo(&thisConfig)
    if err != nil {
        return err
    }

    createIndex()
    return nil
}

createIndex的实现如下:

// github.com/bigwhite/experiments/tree/master/read-ini/demo3/pkg/config/config.go
func createIndex() {
    v := reflect.ValueOf(thisConfig)
    t := reflect.TypeOf(thisConfig)
    cnt := v.NumField()
    for i := 0; i < cnt; i++ {
        fieldVal := v.Field(i)
        if fieldVal.Kind() != reflect.Struct {
            continue
        }

        // it is a struct kind field, go on to get tag
        fieldStructTyp := t.Field(i)
        tag := fieldStructTyp.Tag.Get("ini")
        if tag == "" {
            continue // no ini tag, ignore it
        }

        // append Field Recursively
        appendField(tag, fieldVal)
    }
}


func appendField(parentTag string, v reflect.Value) {
    cnt := v.NumField()
    for i := 0; i < cnt; i++ {
        fieldVal := v.Field(i)
        fieldTyp := v.Type()
        fieldStructTyp := fieldTyp.Field(i)
        tag := fieldStructTyp.Tag.Get("ini")
        if tag == "" {
            continue
        }
        if fieldVal.Kind() != reflect.Struct {
            // leaf field,  add to map
            index[parentTag+"."+tag] = fieldVal.Interface()
        } else {
            // recursive call appendField
            appendField(parentTag+"."+tag, fieldVal)
        }
    }
}

这样我们的GetSectionKey就会变得异常简单:

func GetSectionKey(name string) (interface{}, bool) {
    v, ok := index[name]
    return v, ok
}

我们看到:每次调用config.GetSectionKey将变成一次map的查询操作,这性能那是相当的高:)。

4. 第三方方案

其实前面那些仅仅是一个配置项读取思路的演进过程,你完全无需自行实现,因为我们有实现的更好的第三方包可以直接使用,比如viper[2]。我们用viper来替换demo3中的config包,代码见demo4:

// github.com/bigwhite/experiments/tree/master/read-ini/demo4/main.go
package main

import (
 "github.com/bigwhite/readini/pkg/pkg1"
 "github.com/spf13/viper"
)

func main() {
 viper.SetConfigName("demo")
 viper.SetConfigType("ini")
 viper.AddConfigPath("./conf")
 err := viper.ReadInConfig()
 if err != nil {
  panic(err)
 }
 pkg1.Foo()
}

我们在main函数中利用viper的API读取demo.ini中的配置。然后在pkg1.Foo函数中向下面这样获取配置项的值即可:

// github.com/bigwhite/experiments/tree/master/read-ini/demo4/pkg/pkg1/pkg1.go
package pkg1

import (
 "fmt"

 "github.com/spf13/viper"
)

func Foo() {
 id := viper.GetString("server.id")
 fmt.Printf("id = [%s]\n", id)
 tlsPort := viper.GetInt("server.tls_port")
 fmt.Printf("tls_port = [%d]\n", tlsPort)

 logPath := viper.GetString("log.path")
 fmt.Printf("path = [%s]\n", logPath)
 if viper.IsSet("log.path1") {
  logPath1 := viper.GetString("log.path1")
  fmt.Printf("path1 = [%s]\n", logPath1)
 } else {
  fmt.Printf("log.path1 is not found\n")
 }
}

上面的实现基本等价于我们在demo3中所作的一切,viper没有使用“comma, ok”模式,我们需要自己调用viper.IsSet来判断是否有某个配置项,而不是通过像GetString这样的函数返回的空字符串来判断。

使用viper后,我们甚至无需创建与配置文件中配置项对应的结构体类型了。viper是一个强大的Go配置操作框架,它能实现的不仅限于上面这些,它还支持写配置文件、监视配置文件变化并热加载、支持多种配置文件类型(JSON, TOML, YAML, HCL, ini等)、支持从环境变量和命令行参数读取配置,并且命令行参数、环境变量、配置文件等究竟以哪个配置为准,viper是按一定优先级次序的,从高到低分别为:

  • 明确调用Set

  • flag

  • env

  • config

  • key/value store

  • default

有如此完善的配置操作第三方库,我们完全无需手动撸自己的实现了。

5. 小结

除了在本文中提供的使用包级API获取配置项值的方法外,我们还可以将读取出的配置项集合放入应用上下文,以参数的形式“传递”到应用的各个角落,但笔者更喜欢向viper这种通过公共函数获取配置项的方法。本文阐述的就是这种思路的演化过程,并给出一个“玩票”的实现(未经系统测试),以帮助大家了解其中原理,但不要将其用到你的项目中哦。

本文涉及的源码请到这里下载[3]:https://github.com/bigwhite/experiments/tree/master/read-ini。


“Gopher部落”知识星球正式转正(从试运营星球变成了正式星球)!“gopher部落”旨在打造一个精品Go学习和进阶社群!高品质首发Go技术文章,“三天”首发阅读权,每年两期Go语言发展现状分析,每天提前1小时阅读到新鲜的Gopher日报,网课、技术专栏、图书内容前瞻,六小时内必答保证等满足你关于Go语言生态的所有需求!部落目前虽小,但持续力很强。在2021年上半年,部落将策划两个专题系列分享,并且是部落独享哦:

  • Go技术书籍的书摘和读书体会系列

  • Go与eBPF系列

欢迎大家加入!

Go技术专栏“改善Go语⾔编程质量的50个有效实践[4]”正在慕课网火热热销中!本专栏主要满足广大gopher关于Go语言进阶的需求,围绕如何写出地道且高质量Go代码给出50条有效实践建议,上线后收到一致好评!欢迎大家订 阅!


我的网课“Kubernetes实战:高可用集群搭建、配置、运维与应用[5]”在慕课网热卖中,欢迎小伙伴们订阅学习!


我爱发短信[6]:企业级短信平台定制开发专家 https://51smspush.com/。smspush : 可部署在企业内部的定制化短信平台,三网覆盖,不惧大并发接入,可定制扩展;短信内容你来定,不再受约束, 接口丰富,支持长短信,签名可选。2020年4月8日,中国三大电信运营商联合发布《5G消息白皮书》,51短信平台也会全新升级到“51商用消息平台”,全面支持5G RCS消息。

著名云主机服务厂商DigitalOcean发布最新的主机计划,入门级Droplet配置升级为:1 core CPU、1G内存、25G高速SSD,价格5$/月。有使用DigitalOcean需求的朋友,可以打开这个链接地址[7]:https://m.do.co/c/bff6eed92687 开启你的DO主机之路。

Gopher Daily(Gopher每日新闻)归档仓库 - https://github.com/bigwhite/gopherdaily

我的联系方式:

  • 微博:https://weibo.com/bigwhite20xx

  • 微信公众号:iamtonybai

  • 博客:tonybai.com

  • github: https://github.com/bigwhite

  • “Gopher部落”知识星球:https://public.zsxq.com/groups/51284458844544

商务合作方式:撰稿、出书、培训、在线课程、合伙创业、咨询、广告合作。

参考资料

[1] 

本文永久链接: https://tonybai.com/2021/07/10/read-ini-config-item-by-passing-p-key

[2] 

viper: https://github.com/spf13/viper

[3] 

这里下载: https://github.com/bigwhite/experiments/tree/master/read-ini

[4] 

改善Go语⾔编程质量的50个有效实践: https://www.imooc.com/read/87

[5] 

Kubernetes实战:高可用集群搭建、配置、运维与应用: https://coding.imooc.com/class/284.html

[6] 

我爱发短信: https://51smspush.com/

[7] 

链接地址: https://m.do.co/c/bff6eed92687

<think>根据用户的问题,我们需要根据ini文件中特定Key的值来查找并返回对应的section。 我们可以遍历每个section,然后检查该section中是否有指定的key,并且该key的值是否等于目标值。 注意:可能有多个section包含相同的key,但值不同;也可能有多个section包含相同的key和相同的值(如果允许重复的话,但通常ini文件不允许同一个section内有重复key,但不同section可以)。 因此,我们可能需要返回一个列表,包含所有满足条件的section。 步骤: 1. 读取ini文件 2. 遍历所有section 3. 在每个section中,检查是否存在目标key,并且该key的值是否等于目标值(注意:ini文件中的值都是字符串,所以比较时注意类型) 4. 如果匹配,则记录当前的section 5. 返回所有匹配的section 注意:如果目标key不存在或者没有匹配的值,返回空列表。 另外,引用[1]中提到了如何获取指定section下所有的键值对,我们可以利用类似的方法遍历。 引用[4]中提到了使用configparser读取ini文件的基本操作。 我们假设用户给定了key和value,然后要找出哪些section中有这个key且值等于给定的value。 函数设计: def find_sections_by_key_value(file_path, key, value): # 实现 注意:ini文件中的值都是字符串,所以比较时直接使用字符串比较。如果用户希望进行类型转换,需要额外处理(但题目没有要求,我们就按字符串比较)。 另外,我们还要考虑大小写敏感问题。ini文件默认情况下,sectionkey都是大小写敏感的。但有时我们可能希望忽略大小写,这里我们可以提供选项,但题目没有要求,我们先按大小写敏感处理。 如果用户希望忽略大小写,可以在比较时统一转换为小写(或大写)比较。 我们这里先按大小写敏感处理。 示例ini文件内容(config.ini): [database] host = localhost port = 3306 [test_database] host = test.example.com port = 3306 假设我们要查找key为'port',value为'3306'的section,那么应该返回['database','test_database']。 实现代码:</think># Python 根据 Key 值查找 INI 文件中的 SectionINI 配置文件中,当我们需要根据特定 Key 的值查找对应的 Section 时,可以使用以下 Python 解决方案: ```python import configparser def find_section_by_key_value(file_path, target_key, target_value): """ 根据 Key 和 Value 查找对应的 Section 参数: file_path (str): INI 文件路径 target_key (str): 要查找的 Key target_value (str): 要匹配的 Value 返回: list: 包含匹配的 Section 名称的列表 """ config = configparser.ConfigParser() config.read(file_path, encoding='utf-8') # 处理中文编码 matching_sections = [] for section in config.sections(): # 检查当前 section 是否包含目标 key if target_key in config[section]: # 获取 key 的值并进行比较 key_value = config[section][target_key] if key_value == target_value: matching_sections.append(section) return matching_sections # 使用示例 if __name__ == "__main__": file_path = 'config.ini' target_key = 'port' target_value = '3306' sections = find_section_by_key_value(file_path, target_key, target_value) if sections: print(f"包含 {target_key}={target_value} 的 Section: {', '.join(sections)}") else: print(f"未找到包含 {target_key}={target_value} 的 Section") ``` ## 实现原理说明 1. **读取配置文件**: - 使用 `configparser` 模块读取 INI 文件 - 指定编码参数处理中文内容(参考引用[2][^2]) 2. **遍历所有 Section**: - 使用 `config.sections()` 获取所有 Section 列表(参考引用[4][^4]) - 对每个 Section 进行检查 3. **检查 Key 和 Value**: - 验证当前 Section 是否包含目标 Key - 比较目标 Key 的值是否与指定 Value 匹配 - 将匹配的 Section 名称添加到结果列表 4. **返回匹配结果**: - 返回包含所有匹配 Section 名称的列表 - 处理找不到匹配项的情况 ## 示例 INI 文件 (config.ini) ```ini [database] host = 127.0.0.1 port = 3306 user = root password = 123456 [test_database] host = test.example.com port = 3306 user = tester password = test123 [production] host = db.prod.com port = 5432 user = admin password = securePass ``` ## 使用场景 1. 在复杂配置系统中定位特定配置所在的区域 2. 验证配置项是否在正确的 Section 中 3. 自动化配置检查工具 4. 配置文件的迁移和重构 ## 增强功能:支持模糊匹配和大小写不敏感 ```python def find_section_by_key_value_enhanced( file_path, target_key, target_value, case_sensitive=False, partial_match=False ): config = configparser.ConfigParser() if not case_sensitive: # 转换为小写比较 config.optionxform = lambda option: option.lower() config.read(file_path, encoding='utf-8') matching_sections = [] for section in config.sections(): # 处理大小写不敏感 compare_key = target_key.lower() if not case_sensitive else target_key if compare_key in [k.lower() if not case_sensitive else k for k in config[section]]: actual_value = config[section][target_key] # 值匹配逻辑 if partial_match: match = target_value in actual_value elif not case_sensitive: match = actual_value.lower() == target_value.lower() else: match = actual_value == target_value if match: matching_sections.append(section) return matching_sections ``` ### 使用增强版功能: ```python # 查找包含 "port=3306" 的 section(不区分大小写) sections = find_section_by_key_value_enhanced( 'config.ini', 'port', '3306', case_sensitive=False ) # 查找包含 "pass" 的密码配置(部分匹配) sections = find_section_by_key_value_enhanced( 'config.ini', 'password', 'pass', partial_match=True ) ``` ## 注意事项 1. **Key 重复处理**:不同 Section 可以有相同 Key,但同一 SectionKey 应唯一(参考引用[3][^3]) 2. **性能考虑**:对于大型配置文件,考虑使用字典缓存优化 3. **错误处理**:添加文件不存在、格式错误等异常处理 4. **值类型转换**:INI 文件中的值都是字符串,需要时进行类型转换
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值