mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 09:11:28 +00:00
code style simplify
This commit is contained in:
parent
20e05a3908
commit
9457e61a0c
@ -83,8 +83,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) {
|
if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) {
|
||||||
section = string(line[1 : len(line)-1])
|
section = strings.ToLower(string(line[1 : len(line)-1])) // section name case insensitive
|
||||||
section = strings.ToLower(section) // section name case insensitive
|
|
||||||
if comment.Len() > 0 {
|
if comment.Len() > 0 {
|
||||||
cfg.sectionComment[section] = comment.String()
|
cfg.sectionComment[section] = comment.String()
|
||||||
comment.Reset()
|
comment.Reset()
|
||||||
@ -92,23 +91,24 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
|||||||
if _, ok := cfg.data[section]; !ok {
|
if _, ok := cfg.data[section]; !ok {
|
||||||
cfg.data[section] = make(map[string]string)
|
cfg.data[section] = make(map[string]string)
|
||||||
}
|
}
|
||||||
} else {
|
continue
|
||||||
if _, ok := cfg.data[section]; !ok {
|
}
|
||||||
cfg.data[section] = make(map[string]string)
|
|
||||||
}
|
|
||||||
keyval := bytes.SplitN(line, bEqual, 2)
|
|
||||||
val := bytes.TrimSpace(keyval[1])
|
|
||||||
if bytes.HasPrefix(val, bDQuote) {
|
|
||||||
val = bytes.Trim(val, `"`)
|
|
||||||
}
|
|
||||||
|
|
||||||
key := string(bytes.TrimSpace(keyval[0])) // key name case insensitive
|
if _, ok := cfg.data[section]; !ok {
|
||||||
key = strings.ToLower(key)
|
cfg.data[section] = make(map[string]string)
|
||||||
cfg.data[section][key] = string(val)
|
}
|
||||||
if comment.Len() > 0 {
|
keyValue := bytes.SplitN(line, bEqual, 2)
|
||||||
cfg.keycomment[section+"."+key] = comment.String()
|
val := bytes.TrimSpace(keyValue[1])
|
||||||
comment.Reset()
|
if bytes.HasPrefix(val, bDQuote) {
|
||||||
}
|
val = bytes.Trim(val, `"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := string(bytes.TrimSpace(keyValue[0])) // key name case insensitive
|
||||||
|
key = strings.ToLower(key)
|
||||||
|
cfg.data[section][key] = string(val)
|
||||||
|
if comment.Len() > 0 {
|
||||||
|
cfg.keyComment[section+"."+key] = comment.String()
|
||||||
|
comment.Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -121,38 +121,34 @@ type IniConfigContainer struct {
|
|||||||
filename string
|
filename string
|
||||||
data map[string]map[string]string // section=> key:val
|
data map[string]map[string]string // section=> key:val
|
||||||
sectionComment map[string]string // section : comment
|
sectionComment map[string]string // section : comment
|
||||||
keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment.
|
keyComment map[string]string // id: []{comment, key...}; id 1 is for main comment.
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bool returns the boolean value for a given key.
|
// Bool returns the boolean value for a given key.
|
||||||
func (c *IniConfigContainer) Bool(key string) (bool, error) {
|
func (c *IniConfigContainer) Bool(key string) (bool, error) {
|
||||||
key = strings.ToLower(key)
|
return strconv.ParseBool(c.getdata(strings.ToLower(key)))
|
||||||
return strconv.ParseBool(c.getdata(key))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int returns the integer value for a given key.
|
// Int returns the integer value for a given key.
|
||||||
func (c *IniConfigContainer) Int(key string) (int, error) {
|
func (c *IniConfigContainer) Int(key string) (int, error) {
|
||||||
key = strings.ToLower(key)
|
return strconv.Atoi(c.getdata(strings.ToLower(key)))
|
||||||
return strconv.Atoi(c.getdata(key))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64 returns the int64 value for a given key.
|
// Int64 returns the int64 value for a given key.
|
||||||
func (c *IniConfigContainer) Int64(key string) (int64, error) {
|
func (c *IniConfigContainer) Int64(key string) (int64, error) {
|
||||||
key = strings.ToLower(key)
|
return strconv.ParseInt(c.getdata(strings.ToLower(key)), 10, 64)
|
||||||
return strconv.ParseInt(c.getdata(key), 10, 64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float returns the float value for a given key.
|
// Float returns the float value for a given key.
|
||||||
func (c *IniConfigContainer) Float(key string) (float64, error) {
|
func (c *IniConfigContainer) Float(key string) (float64, error) {
|
||||||
key = strings.ToLower(key)
|
return strconv.ParseFloat(c.getdata(strings.ToLower(key)), 64)
|
||||||
return strconv.ParseFloat(c.getdata(key), 64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the string value for a given key.
|
// String returns the string value for a given key.
|
||||||
func (c *IniConfigContainer) String(key string) string {
|
func (c *IniConfigContainer) String(key string) string {
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
return c.getdata(key)
|
return c.getdata(strings.ToLower(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strings returns the []string value for a given key.
|
// Strings returns the []string value for a given key.
|
||||||
@ -170,16 +166,19 @@ func (c *IniConfigContainer) Set(key, value string) error {
|
|||||||
return errors.New("key is empty")
|
return errors.New("key is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
var section, k string
|
var (
|
||||||
key = strings.ToLower(key)
|
section, k string
|
||||||
sectionkey := strings.Split(key, "::")
|
sectionKey []string = strings.Split(key, "::")
|
||||||
if len(sectionkey) >= 2 {
|
)
|
||||||
section = sectionkey[0]
|
|
||||||
k = sectionkey[1]
|
if len(sectionKey) >= 2 {
|
||||||
|
section = sectionKey[0]
|
||||||
|
k = sectionKey[1]
|
||||||
} else {
|
} else {
|
||||||
section = DEFAULT_SECTION
|
section = DEFAULT_SECTION
|
||||||
k = sectionkey[0]
|
k = sectionKey[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := c.data[section]; !ok {
|
if _, ok := c.data[section]; !ok {
|
||||||
c.data[section] = make(map[string]string)
|
c.data[section] = make(map[string]string)
|
||||||
}
|
}
|
||||||
@ -189,8 +188,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
|
|||||||
|
|
||||||
// DIY returns the raw value by a given key.
|
// DIY returns the raw value by a given key.
|
||||||
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
|
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||||
key = strings.ToLower(key)
|
if v, ok := c.data[strings.ToLower(key)]; ok {
|
||||||
if v, ok := c.data[key]; ok {
|
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
return v, errors.New("key not find")
|
return v, errors.New("key not find")
|
||||||
@ -204,18 +202,19 @@ func (c *IniConfigContainer) getdata(key string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var section, k string
|
var (
|
||||||
key = strings.ToLower(key)
|
section, k string
|
||||||
sectionkey := strings.Split(key, "::")
|
sectionKey []string = strings.Split(key, "::")
|
||||||
if len(sectionkey) >= 2 {
|
)
|
||||||
section = sectionkey[0]
|
if len(sectionKey) >= 2 {
|
||||||
k = sectionkey[1]
|
section = sectionKey[0]
|
||||||
|
k = sectionKey[1]
|
||||||
} else {
|
} else {
|
||||||
section = DEFAULT_SECTION
|
section = DEFAULT_SECTION
|
||||||
k = sectionkey[0]
|
k = sectionKey[0]
|
||||||
}
|
}
|
||||||
if v, ok := c.data[section]; ok {
|
if v, ok := c.data[section]; ok {
|
||||||
if vv, o := v[k]; o {
|
if vv, ok2 := v[k]; ok2 {
|
||||||
return vv
|
return vv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user