Merge branch 'master' of github.com:go-ini/ini

This commit is contained in:
Unknwon
2014-12-21 08:07:31 -05:00
2 changed files with 28 additions and 7 deletions
+5
View File
@@ -73,6 +73,11 @@ sections := cfg.Sections()
names := cfg.SectionStrings() names := cfg.SectionStrings()
``` ```
To get a hash of keys and corresponding values:
```go
hash := cfg.GetSection("").KeysHash()
```
### Working with keys ### Working with keys
To get a key under a section: To get a key under a section:
+23 -7
View File
@@ -296,17 +296,18 @@ func (k *Key) SetValue(v string) {
// /_______ /\___ >\___ >__| |__|\____/|___| / // /_______ /\___ >\___ >__| |__|\____/|___| /
// \/ \/ \/ \/ // \/ \/ \/ \/
// Section represetns a config section. // Section represents a config section.
type Section struct { type Section struct {
f *File f *File
Comment string Comment string
name string name string
keys map[string]*Key keys map[string]*Key
keyList []string keyList []string
keysHash map[string]string
} }
func newSection(f *File, name string) *Section { func newSection(f *File, name string) *Section {
return &Section{f, "", name, make(map[string]*Key), make([]string, 0, 10)} return &Section{f, "", name, make(map[string]*Key), make([]string, 0, 10), make(map[string]string)}
} }
// Name returns name of Section. // Name returns name of Section.
@@ -332,6 +333,7 @@ func (s *Section) NewKey(name, val string) (*Key, error) {
s.keyList = append(s.keyList, name) s.keyList = append(s.keyList, name)
s.keys[name] = &Key{s, "", name, val, false} s.keys[name] = &Key{s, "", name, val, false}
s.keysHash[name] = val
return s.keys[name], nil return s.keys[name], nil
} }
@@ -379,6 +381,20 @@ func (s *Section) KeyStrings() []string {
return list return list
} }
// KeysHash returns keys hash consisting of names and values.
func (s *Section) KeysHash() map[string]string {
if s.f.BlockMode {
s.f.lock.RLock()
defer s.f.lock.RUnlock()
}
hash := map[string]string{}
for key, value := range s.keysHash {
hash[key] = value
}
return hash
}
// DeleteKey deletes a key from section. // DeleteKey deletes a key from section.
func (s *Section) DeleteKey(name string) { func (s *Section) DeleteKey(name string) {
if s.f.BlockMode { if s.f.BlockMode {