1 Star 2 Fork 9

leastsoft/go配置文件解析器

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
config.go 3.76 KB
一键复制 编辑 原始数据 按行查看 历史
shirdon 提交于 2019-07-07 15:08 . 上传
//++++++++++++++++++++++++++++++++++++++++
//Fighting for great,share generate value!
//Build the best soft by golang,let's go!
//++++++++++++++++++++++++++++++++++++++++
//Author:ShirDon <http://www.shirdon.com>
//Email:hcbsts@163.com; 823923263@qq.com
//++++++++++++++++++++++++++++++++++++++++
package config
import (
"regexp"
"strings"
)
const (
// Default section name.
DEFAULT_SECTION = "DEFAULT"
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 200
DEFAULT_COMMENT = "# "
ALTERNATIVE_COMMENT = "; "
DEFAULT_SEPARATOR = ":"
ALTERNATIVE_SEPARATOR = "="
MUTI_KEY_IDENTIFIER = "@"
)
var (
// Strings accepted as boolean.
boolString = map[string]bool{
"t": true,
"true": true,
"y": true,
"yes": true,
"on": true,
"1": true,
"f": false,
"false": false,
"n": false,
"no": false,
"off": false,
"0": false,
}
varRegExp = regexp.MustCompile(`%\(([a-zA-Z0-9_.\-]+)\)s`) // %(variable)s
envVarRegExp = regexp.MustCompile(`\${([a-zA-Z0-9_.\-]+)}`) // ${envvar}
)
// Config is the representation of configuration settings.
type Config struct {
comment string
separator string
// Sections order
lastIdSection int // Last section identifier
idSection map[string]int // Section : position
// The last option identifier used for each section.
lastIdOption map[string]int // Section : last identifier
// Section -> option : value
data map[string]map[string]*tValue
}
// tValue holds the input position for a value.
type tValue struct {
position int // Option order
v string // value
vMuti []string //value for muti key
}
// New creates an empty configuration representation.
// This representation can be filled with AddSection and AddOption and then
// saved to a file using WriteFile.
//
// == Arguments
//
// comment: has to be `DEFAULT_COMMENT` or `ALTERNATIVE_COMMENT`
// separator: has to be `DEFAULT_SEPARATOR` or `ALTERNATIVE_SEPARATOR`
// preSpace: indicate if is inserted a space before of the separator
// postSpace: indicate if is added a space after of the separator
func New(comment, separator string, preSpace, postSpace bool) *Config {
if comment != DEFAULT_COMMENT && comment != ALTERNATIVE_COMMENT {
panic("comment character not valid")
}
if separator != DEFAULT_SEPARATOR && separator != ALTERNATIVE_SEPARATOR {
panic("separator character not valid")
}
// == Get spaces around separator
if preSpace {
separator = " " + separator
}
if postSpace {
separator += " "
}
//==
c := new(Config)
c.comment = comment
c.separator = separator
c.idSection = make(map[string]int)
c.lastIdOption = make(map[string]int)
c.data = make(map[string]map[string]*tValue)
c.AddSection(DEFAULT_SECTION) // Default section always exists.
return c
}
// NewDefault creates a configuration representation with values by default.
func NewDefault() *Config {
return New(DEFAULT_COMMENT, DEFAULT_SEPARATOR, false, true)
}
// Merge merges the given configuration "source" with this one ("target").
//
// Merging means that any option (under any section) from source that is not in
// target will be copied into target. When the target already has an option with
// the same name and section then it is overwritten (i.o.w. the source wins).
func (target *Config) Merge(source *Config) {
if source == nil || source.data == nil || len(source.data) == 0 {
return
}
for section, option := range source.data {
for optionName, optionValue := range option {
target.AddOption(section, optionName, optionValue.v)
}
}
}
// == Utility
func stripComments(l string) string {
// Comments are preceded by space or TAB
for _, c := range []string{" ;", "\t;", " #", "\t#"} {
if i := strings.Index(l, c); i != -1 {
l = l[0:i]
}
}
return l
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/leastsoft/configs_witch_go.git
git@gitee.com:leastsoft/configs_witch_go.git
leastsoft
configs_witch_go
go配置文件解析器
master

搜索帮助