1 // Copyright 2014 Unknwon
3 // Licensed under the Apache License, Version 2.0 (the "License"): you may
4 // not use this file except in compliance with the License. You may obtain
5 // a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations
23 // Section represents a config section.
30 keysHash map[string]string
36 func newSection(f *File, name string) *Section {
40 keys: make(map[string]*Key),
41 keyList: make([]string, 0, 10),
42 keysHash: make(map[string]string),
46 // Name returns name of Section.
47 func (s *Section) Name() string {
51 // Body returns rawBody of Section if the section was marked as unparseable.
52 // It still follows the other rules of the INI format surrounding leading/trailing whitespace.
53 func (s *Section) Body() string {
54 return strings.TrimSpace(s.rawBody)
57 // NewKey creates a new key to given section.
58 func (s *Section) NewKey(name, val string) (*Key, error) {
60 return nil, errors.New("error creating new key: empty key name")
61 } else if s.f.options.Insensitive {
62 name = strings.ToLower(name)
67 defer s.f.lock.Unlock()
70 if inSlice(name, s.keyList) {
71 s.keys[name].value = val
72 return s.keys[name], nil
75 s.keyList = append(s.keyList, name)
81 s.keysHash[name] = val
82 return s.keys[name], nil
85 // GetKey returns key in section by given name.
86 func (s *Section) GetKey(name string) (*Key, error) {
87 // FIXME: change to section level lock?
91 if s.f.options.Insensitive {
92 name = strings.ToLower(name)
100 // Check if it is a child-section.
103 if i := strings.LastIndex(sname, "."); i > -1 {
105 sec, err := s.f.GetSection(sname)
109 return sec.GetKey(name)
114 return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
119 // HasKey returns true if section contains a key with given name.
120 func (s *Section) HasKey(name string) bool {
121 key, _ := s.GetKey(name)
125 // Haskey is a backwards-compatible name for HasKey.
126 func (s *Section) Haskey(name string) bool {
127 return s.HasKey(name)
130 // HasValue returns true if section contains given raw value.
131 func (s *Section) HasValue(value string) bool {
134 defer s.f.lock.RUnlock()
137 for _, k := range s.keys {
138 if value == k.value {
145 // Key assumes named Key exists in section and returns a zero-value when not.
146 func (s *Section) Key(name string) *Key {
147 key, err := s.GetKey(name)
149 // It's OK here because the only possible error is empty key name,
150 // but if it's empty, this piece of code won't be executed.
151 key, _ = s.NewKey(name, "")
157 // Keys returns list of keys of section.
158 func (s *Section) Keys() []*Key {
159 keys := make([]*Key, len(s.keyList))
160 for i := range s.keyList {
161 keys[i] = s.Key(s.keyList[i])
166 // ParentKeys returns list of keys of parent section.
167 func (s *Section) ParentKeys() []*Key {
168 var parentKeys []*Key
171 if i := strings.LastIndex(sname, "."); i > -1 {
173 sec, err := s.f.GetSection(sname)
177 parentKeys = append(parentKeys, sec.Keys()...)
186 // KeyStrings returns list of key names of section.
187 func (s *Section) KeyStrings() []string {
188 list := make([]string, len(s.keyList))
189 copy(list, s.keyList)
193 // KeysHash returns keys hash consisting of names and values.
194 func (s *Section) KeysHash() map[string]string {
197 defer s.f.lock.RUnlock()
200 hash := map[string]string{}
201 for key, value := range s.keysHash {
207 // DeleteKey deletes a key from section.
208 func (s *Section) DeleteKey(name string) {
211 defer s.f.lock.Unlock()
214 for i, k := range s.keyList {
216 s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)