From 0361a2e88d99cf088727640c62a0ebd74a4149b7 Mon Sep 17 00:00:00 2001 From: Mario Kozjak Date: Sun, 21 Dec 2014 13:31:40 +0100 Subject: [PATCH 1/2] Dump file contents to the hash. Fixes #1 Signed-off-by: Mario Kozjak --- README.md | 5 +++++ ini.go | 25 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0c21a33..eff8723 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,11 @@ sections := cfg.Sections() names := cfg.SectionStrings() ``` +To get a hash of keys and corresponding values: +```go +hash := cfg.GetSection("").KeysHash() +``` + ### Working with keys To get a key under a section: diff --git a/ini.go b/ini.go index dd990de..20b6fce 100644 --- a/ini.go +++ b/ini.go @@ -296,17 +296,18 @@ func (k *Key) SetValue(v string) { // /_______ /\___ >\___ >__| |__|\____/|___| / // \/ \/ \/ \/ -// Section represetns a config section. +// Section represents a config section. type Section struct { - f *File - Comment string - name string - keys map[string]*Key - keyList []string + f *File + Comment string + name string + keys map[string]*Key + keyList []string + keysHash map[string]string } 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. @@ -332,6 +333,7 @@ func (s *Section) NewKey(name, val string) (*Key, error) { s.keyList = append(s.keyList, name) s.keys[name] = &Key{s, "", name, val, false} + s.keysHash[name] = val return s.keys[name], nil } @@ -379,6 +381,15 @@ func (s *Section) KeyStrings() []string { return list } +// KeysHash returns keys hash consisting of names and values. +func (s *Section) KeysHash() map[string]string { + hash := map[string]string{} + for key, value := range s.keysHash { + hash[key] = value + } + return hash +} + // DeleteKey deletes a key from section. func (s *Section) DeleteKey(name string) { if s.f.BlockMode { From d2483796e40a8d6c389a407c9676b5406f252a3a Mon Sep 17 00:00:00 2001 From: Mario Kozjak Date: Sun, 21 Dec 2014 14:05:45 +0100 Subject: [PATCH 2/2] Add RLock() to KeysHash Signed-off-by: Mario Kozjak --- ini.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ini.go b/ini.go index 20b6fce..1ad6654 100644 --- a/ini.go +++ b/ini.go @@ -383,6 +383,11 @@ func (s *Section) KeyStrings() []string { // 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