wini

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

介绍

wego/wini是一款GO语言版本的ini配置文件解析工具,wego/wini具有以下特征:

  1. 提供了GetString、GetInt...以及MustrString、MustInt...函数,方便配置数据的获取。
  2. 支持通过struct的tag来自动将配置数据赋值给struct的字段。
  3. 支持使用环境变量配置项的值。

安装

go get github.com/haming123/wego/wini

快速上手

首先准备app.conf配置文件:

#应用名称
app_name = demo
#日志级别: 0 OFF 1 FATAL 2 ERROR 3 WARN 4 INFO 5 DEBUG
level = 5
#获取环境变量
go_path = ${GOPATH}

[mysql]
db_name = demodb
db_host = 127.0.0.1:3306
db_user = root
db_pwd = demopwd

接下看看如何读取配置文件,并获取配置项的值:

package main
import (
	"fmt"
	"github.com/haming123/wego/wini"
)
func main()  {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app.conf", &cfg)
	if err != nil {
		fmt.Println(err)
		return
	}

	//通过GetXXX获取配置项的值
	val := cfg.GetString("app_name")
	if val.Error != nil {
		fmt.Println( val.Error)
		return
	}
	fmt.Println(val.Value)
}

读取Section中的配置项

ini 文件是以分区(section)组织的。分区以[name]开始,在下一个分区前结束。所有分区前的内容属于默认分区([root])。以下代码是section配置项的读取示例:

func TestSectionGet(t *testing.T) {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app.conf", &cfg)
	if err != nil {
		t.Error(err)
		return
	}

	//通过GetXXX获取配置项的值
	val := cfg.Section("mysql").GetString("db_name")
	if val.Error != nil {
		t.Error(err)
		return
	}
	t.Log(val.Value)
}

各种类型的数据的读取

为了方便各种类型的配置数据的获取, wego/wini提供了GetString、GetInt...等函数,例如以下配置文件的读取:

#各种数据类型的配置项
str_value = hello
bool_value = true
int_value = 99
float_value = 123.45
func TestIniGetXXX(t *testing.T) {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app2.conf", &cfg)
	if err != nil {
		t.Error(err)
		return
	}

	val := cfg.GetString("str_value")
	if val.Error != nil {
		t.Error(val.Error)
		return
	}
	t.Log(val.Value)

	val_bool := cfg.GetString("bool_value")
	if val.Error != nil {
		t.Error(val_bool.Error)
		return
	}
	t.Log(val_bool.Value)

	val_int := cfg.GetString("int_value")
	if val.Error != nil {
		t.Error(val_int.Error)
		return
	}
	t.Log(val_int.Value)

	val_float := cfg.GetString("float_value")
	if val.Error != nil {
		t.Error(val_float.Error)
		return
	}
	t.Log(val_float.Value)
}

数据的快捷读取

使用GetXXX函数读取配置项需要进行错误判断,这样的代码写起来会非常繁琐。为此,wego/wini提供对应的MustXXX方法,这个方法只返回一个值, 同时它可接受缺省参数,如果没有配置对应的配置项或配置内容无法转换,则使用缺省值作为返回值。

func TestIniMustXXX(t *testing.T) {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app2.conf", &cfg)
	if err != nil {
		t.Error(err)
		return
	}

	t.Log(cfg.MustString("str_value"))
	t.Log(cfg.MustBool("bool_value"))
	t.Log(cfg.MustInt("int_value"))
	t.Log(cfg.MustFloat("float_value"))
}

数组类型数据的读取

wego/wini也支持数组类型数据的读取,要求:数组要作为一个配置项添加到ini文件中,并且数组成员之间用指定的分隔符(例如“,”)分隔:

#数组配置项
ints_value = 1,2,3,4,5
func TestGetArray(t *testing.T) {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app2.conf", &cfg)
	if err != nil {
		t.Error(err)
		return
	}

	arr, err := cfg.GetInts("ints_value", ",")
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(arr)
}

结构体字段映射与数据的读取

wego/wini持通过struct的tag来获取struct字段与配置项的映射关系,并可以通过映射关系自动给struct字段赋值,首先需要在struct定义中指定映射关系:

type DbConfig struct {
	MysqlHost 	string 		`ini:"db_host"`
	MysqlUser 	string 		`ini:"db_user"`
	MysqlPwd  	string 		`ini:"db_pwd"`
	MysqlDb   	string 		`ini:"db_name"`
}

type AppConfig struct {
	AppName  	string   	`ini:"app_name"`
	HttpPort	uint     	`ini:"http_port;default=8080"`
	GoPath   	string   	`ini:"go_path"`
	DbParam  	DbConfig 	`ini:"mysql"`
}

说明: wego/wini的定义映射关系时支持配置缺省值,再进行数据解析时若没有配置内容,则使用缺省值作为字段的值。 wego/wini使用GetStruct来struct字段赋值,例如:

func TestIniGetStruct(t *testing.T) {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app.conf", &cfg)
	if err != nil {
		t.Error(err)
		return
	}

	var data AppConfig
	err = cfg.GetStruct(&data)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(data)
}

也可以调用Section的GetStruct函数直接从section中获取配置内容,例如:

func TestIniGetSectionStruct(t *testing.T) {
	var cfg wini.ConfigData
	err := wini.ParseFile("./app.conf", &cfg)
	if err != nil {
		t.Error(err)
		return
	}

	var data DbConfig
	err = cfg.Section("mysql").GetStruct(&data)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(data)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBools

func GetBools(key string, delim string) ([]bool, error)

func GetFloats

func GetFloats(key string, delim string) ([]float64, error)

func GetInt64s

func GetInt64s(key string, delim string) ([]int64, error)

func GetInts

func GetInts(key string, delim string) ([]int, error)

func GetStrings

func GetStrings(key string, delim string) ([]string, error)

func GetStruct

func GetStruct(ptr interface{}) error

func MustBool

func MustBool(key string, defaultValue ...bool) bool

func MustFloat

func MustFloat(key string, defaultValue ...float64) float64

func MustInt

func MustInt(key string, defaultValue ...int) int

func MustInt32

func MustInt32(key string, defaultValue ...int32) int32

func MustInt64

func MustInt64(key string, defaultValue ...int64) int64

func MustString

func MustString(key string, defaultValue ...string) string

func MustTime

func MustTime(key string, format string, defaultValue ...time.Time) time.Time

func ParseFile

func ParseFile(file_path string, cfg *ConfigData) error

在 ini 文件中,每个键值对占用一行,中间使用=隔开。以#开头的内容为注释。 ini 文件是以分区(section)组织的。 分区以[name]开始,在下一个分区前结束。所有分区前的内容属于默认分区

func SplitAndTrim

func SplitAndTrim(str string, sep string) (string, string, bool)

func SplitString

func SplitString(str string, sep string) (string, string, bool)

Types

type ConfigData

type ConfigData struct {
	// contains filtered or unexported fields
}

func InitConfigData

func InitConfigData(file_name ...string) (*ConfigData, error)

func (*ConfigData) GetBool

func (this *ConfigData) GetBool(key string, defaultValue ...bool) ValidBool

func (ConfigData) GetBools

func (this ConfigData) GetBools(key string, delim string) ([]bool, error)

func (*ConfigData) GetFloat

func (this *ConfigData) GetFloat(key string, defaultValue ...float64) ValidFloat

func (ConfigData) GetFloats

func (this ConfigData) GetFloats(key string, delim string) ([]float64, error)

func (*ConfigData) GetInt

func (this *ConfigData) GetInt(key string, defaultValue ...int) ValidInt

func (*ConfigData) GetInt32

func (this *ConfigData) GetInt32(key string, defaultValue ...int32) ValidInt32

func (*ConfigData) GetInt64

func (this *ConfigData) GetInt64(key string, defaultValue ...int64) ValidInt64

func (ConfigData) GetInt64s

func (this ConfigData) GetInt64s(key string, delim string) ([]int64, error)

func (ConfigData) GetInts

func (this ConfigData) GetInts(key string, delim string) ([]int, error)

func (*ConfigData) GetString

func (this *ConfigData) GetString(key string, defaultValue ...string) ValidString

func (ConfigData) GetStrings

func (this ConfigData) GetStrings(key string, delim string) ([]string, error)

func (*ConfigData) GetStruct

func (this *ConfigData) GetStruct(ptr interface{}) error

func (ConfigData) GetTime

func (this ConfigData) GetTime(key string, format string, defaultValue ...time.Time) ValidTime

func (*ConfigData) MustBool

func (this *ConfigData) MustBool(key string, defaultValue ...bool) bool

func (*ConfigData) MustFloat

func (this *ConfigData) MustFloat(key string, defaultValue ...float64) float64

func (*ConfigData) MustInt

func (this *ConfigData) MustInt(key string, defaultValue ...int) int

func (*ConfigData) MustInt32

func (this *ConfigData) MustInt32(key string, defaultValue ...int32) int32

func (*ConfigData) MustInt64

func (this *ConfigData) MustInt64(key string, defaultValue ...int64) int64

func (*ConfigData) MustString

func (this *ConfigData) MustString(key string, default_value ...string) string

func (*ConfigData) MustTime

func (this *ConfigData) MustTime(key string, format string, defaultValue ...time.Time) time.Time

func (*ConfigData) ParseFile

func (this *ConfigData) ParseFile(file_path string) error

func (*ConfigData) SaveConfig

func (this *ConfigData) SaveConfig(filename string) error

func (*ConfigData) Section

func (this *ConfigData) Section(section ...string) ConfigSection

func (*ConfigData) SetData

func (this *ConfigData) SetData(section string, name string, value string)

type ConfigItem

type ConfigItem struct {
	// contains filtered or unexported fields
}

type ConfigSection

type ConfigSection map[string]string

func Section

func Section(section ...string) ConfigSection

func (ConfigSection) GetBool

func (this ConfigSection) GetBool(key string, defaultValue ...bool) ValidBool

func (ConfigSection) GetBools

func (this ConfigSection) GetBools(key string, delim string) ([]bool, error)

func (ConfigSection) GetFloat

func (this ConfigSection) GetFloat(key string, defaultValue ...float64) ValidFloat

func (ConfigSection) GetFloats

func (this ConfigSection) GetFloats(key string, delim string) ([]float64, error)

func (ConfigSection) GetInt

func (this ConfigSection) GetInt(key string, defaultValue ...int) ValidInt

func (ConfigSection) GetInt32

func (this ConfigSection) GetInt32(key string, defaultValue ...int32) ValidInt32

func (ConfigSection) GetInt64

func (this ConfigSection) GetInt64(key string, defaultValue ...int64) ValidInt64

func (ConfigSection) GetInt64s

func (this ConfigSection) GetInt64s(key string, delim string) ([]int64, error)

func (ConfigSection) GetInts

func (this ConfigSection) GetInts(key string, delim string) ([]int, error)

func (ConfigSection) GetString

func (this ConfigSection) GetString(key string, defaultValue ...string) ValidString

func (ConfigSection) GetStrings

func (this ConfigSection) GetStrings(key string, delim string) ([]string, error)

func (ConfigSection) GetStruct

func (this ConfigSection) GetStruct(ptr interface{}) error

func (ConfigSection) GetTime

func (this ConfigSection) GetTime(key string, format string, defaultValue ...time.Time) ValidTime

func (ConfigSection) MustBool

func (this ConfigSection) MustBool(key string, defaultValue ...bool) bool

func (ConfigSection) MustFloat

func (this ConfigSection) MustFloat(key string, defaultValue ...float64) float64

func (ConfigSection) MustInt

func (this ConfigSection) MustInt(key string, defaultValue ...int) int

func (ConfigSection) MustInt32

func (this ConfigSection) MustInt32(key string, defaultValue ...int32) int32

func (ConfigSection) MustInt64

func (this ConfigSection) MustInt64(key string, defaultValue ...int64) int64

func (ConfigSection) MustString

func (this ConfigSection) MustString(key string, defaultValue ...string) string

func (ConfigSection) MustTime

func (this ConfigSection) MustTime(key string, format string, defaultValue ...time.Time) time.Time

type TagInfo

type TagInfo struct {
	FieldName string
	HasValue  bool
	DefValue  string
}

func GetTagInfo

func GetTagInfo(ff reflect.StructField, tag string) TagInfo

获取struct的字段信息

type ValidBool

type ValidBool struct {
	Value bool
	Error error
}

func GetBool

func GetBool(key string, defaultValue ...bool) ValidBool

type ValidFloat

type ValidFloat struct {
	Value float64
	Error error
}

func GetFloat

func GetFloat(key string, defaultValue ...float64) ValidFloat

type ValidInt

type ValidInt struct {
	Value int
	Error error
}

func GetInt

func GetInt(key string, defaultValue ...int) ValidInt

type ValidInt32

type ValidInt32 struct {
	Value int32
	Error error
}

func GetInt32

func GetInt32(key string, defaultValue ...int32) ValidInt32

type ValidInt64

type ValidInt64 struct {
	Value int64
	Error error
}

func GetInt64

func GetInt64(key string, defaultValue ...int64) ValidInt64

type ValidString

type ValidString struct {
	Value string
	Error error
}

func GetString

func GetString(key string, defaultValue ...string) ValidString

type ValidTime

type ValidTime struct {
	Value time.Time
	Error error
}

func GetTime

func GetTime(key string, format string, defaultValue ...time.Time) ValidTime

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL