1 Star 0 Fork 9

Shirdon / go配置文件解析器

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
option.go 3.51 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 "errors"
// AddOption adds a new option and value to the configuration.
//
// If the section is nil then uses the section by default; if it does not exist,
// it is created in advance.
//
// It returns true if the option and value were inserted, and false if the value
// was overwritten.
func (c *Config) AddOption(section string, option string, value string) bool {
c.AddSection(section) // Make sure section exists
if section == "" {
section = DEFAULT_SECTION
}
_, ok := c.data[section][option]
// Add muti key process
// muti key will start with MUTI_KEY_IDENTIFIER and may appear more than once
// key will be key except MUTI_KEY_IDENTIFIER and value will be an array
if MUTI_KEY_IDENTIFIER == string(option[0]) {
option = string(option[1:])
var vMuti []string
val, ok := c.data[section][option]
if true == ok {
vMuti = val.vMuti
} else {
vMuti = []string{}
}
vMuti = append(vMuti, value)
c.data[section][option] = &tValue{c.lastIdOption[section], value, vMuti}
} else {
c.data[section][option] = &tValue{c.lastIdOption[section], value, []string{}}
}
c.lastIdOption[section]++
return !ok
}
// RemoveOption removes a option and value from the configuration.
// It returns true if the option and value were removed, and false otherwise,
// including if the section did not exist.
func (c *Config) RemoveOption(section string, option string) bool {
if _, ok := c.data[section]; !ok {
return false
}
_, ok := c.data[section][option]
delete(c.data[section], option)
return ok
}
// HasOption checks if the configuration has the given option in the section.
// It returns false if either the option or section do not exist.
func (c *Config) HasOption(section string, option string) bool {
if _, ok := c.data[section]; !ok {
return false
}
_, okd := c.data[DEFAULT_SECTION][option]
_, oknd := c.data[section][option]
return okd || oknd
}
// Options returns the list of options available in the given section.
// It returns an error if the section does not exist and an empty list if the
// section is empty. Options within the default section are also included.
func (c *Config) Options(section string) (options []string, err error) {
if _, ok := c.data[section]; !ok {
return nil, errors.New(SectionError(section).Error())
}
// Keep a map of option names we've seen to deduplicate.
optionMap := make(map[string]struct{},
len(c.data[DEFAULT_SECTION])+len(c.data[section]))
for s, _ := range c.data[DEFAULT_SECTION] {
optionMap[s] = struct{}{}
}
for s, _ := range c.data[section] {
optionMap[s] = struct{}{}
}
// Get the keys.
i := 0
options = make([]string, len(optionMap))
for k, _ := range optionMap {
options[i] = k
i++
}
return options, nil
}
// SectionOptions returns only the list of options available in the given section.
// Unlike Options, SectionOptions doesn't return options in default section.
// It returns an error if the section doesn't exist.
func (c *Config) SectionOptions(section string) (options []string, err error) {
if _, ok := c.data[section]; !ok {
return nil, errors.New(SectionError(section).Error())
}
options = make([]string, len(c.data[section]))
i := 0
for s, _ := range c.data[section] {
options[i] = s
i++
}
return options, nil
}
Go
1
https://gitee.com/shirdon/configs_witch_go.git
git@gitee.com:shirdon/configs_witch_go.git
shirdon
configs_witch_go
go配置文件解析器
master

搜索帮助