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 // SetBody updates body content only if section is raw.
58 func (s *Section) SetBody(body string) {
65 // NewKey creates a new key to given section.
66 func (s *Section) NewKey(name, val string) (*Key, error) {
68 return nil, errors.New("error creating new key: empty key name")
69 } else if s.f.options.Insensitive {
70 name = strings.ToLower(name)
75 defer s.f.lock.Unlock()
78 if inSlice(name, s.keyList) {
79 if s.f.options.AllowShadows {
80 if err := s.keys[name].addShadow(val); err != nil {
84 s.keys[name].value = val
85 s.keysHash[name] = val
87 return s.keys[name], nil
90 s.keyList = append(s.keyList, name)
91 s.keys[name] = newKey(s, name, val)
92 s.keysHash[name] = val
93 return s.keys[name], nil
96 // NewBooleanKey creates a new boolean type key to given section.
97 func (s *Section) NewBooleanKey(name string) (*Key, error) {
98 key, err := s.NewKey(name, "true")
103 key.isBooleanType = true
107 // GetKey returns key in section by given name.
108 func (s *Section) GetKey(name string) (*Key, error) {
112 if s.f.options.Insensitive {
113 name = strings.ToLower(name)
121 // Check if it is a child-section.
124 if i := strings.LastIndex(sname, "."); i > -1 {
126 sec, err := s.f.GetSection(sname)
130 return sec.GetKey(name)
134 return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
139 // HasKey returns true if section contains a key with given name.
140 func (s *Section) HasKey(name string) bool {
141 key, _ := s.GetKey(name)
145 // Deprecated: Use "HasKey" instead.
146 func (s *Section) Haskey(name string) bool {
147 return s.HasKey(name)
150 // HasValue returns true if section contains given raw value.
151 func (s *Section) HasValue(value string) bool {
154 defer s.f.lock.RUnlock()
157 for _, k := range s.keys {
158 if value == k.value {
165 // Key assumes named Key exists in section and returns a zero-value when not.
166 func (s *Section) Key(name string) *Key {
167 key, err := s.GetKey(name)
169 // It's OK here because the only possible error is empty key name,
170 // but if it's empty, this piece of code won't be executed.
171 key, _ = s.NewKey(name, "")
177 // Keys returns list of keys of section.
178 func (s *Section) Keys() []*Key {
179 keys := make([]*Key, len(s.keyList))
180 for i := range s.keyList {
181 keys[i] = s.Key(s.keyList[i])
186 // ParentKeys returns list of keys of parent section.
187 func (s *Section) ParentKeys() []*Key {
188 var parentKeys []*Key
191 if i := strings.LastIndex(sname, "."); i > -1 {
193 sec, err := s.f.GetSection(sname)
197 parentKeys = append(parentKeys, sec.Keys()...)
206 // KeyStrings returns list of key names of section.
207 func (s *Section) KeyStrings() []string {
208 list := make([]string, len(s.keyList))
209 copy(list, s.keyList)
213 // KeysHash returns keys hash consisting of names and values.
214 func (s *Section) KeysHash() map[string]string {
217 defer s.f.lock.RUnlock()
220 hash := map[string]string{}
221 for key, value := range s.keysHash {
227 // DeleteKey deletes a key from section.
228 func (s *Section) DeleteKey(name string) {
231 defer s.f.lock.Unlock()
234 for i, k := range s.keyList {
236 s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)
238 delete(s.keysHash, name)
244 // ChildSections returns a list of child sections of current section.
245 // For example, "[parent.child1]" and "[parent.child12]" are child sections
246 // of section "[parent]".
247 func (s *Section) ChildSections() []*Section {
248 prefix := s.name + "."
249 children := make([]*Section, 0, 3)
250 for _, name := range s.f.sectionList {
251 if strings.HasPrefix(name, prefix) {
252 children = append(children, s.f.sections[name])