section.go (6612B)
1 // Copyright 2014 Unknwon 2 // 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 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 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 13 // under the License. 14 15 package ini 16 17 import ( 18 "errors" 19 "fmt" 20 "strings" 21 ) 22 23 // Section represents a config section. 24 type Section struct { 25 f *File 26 Comment string 27 // TrailingComment is comments after the last key of this section 28 // (or after a raw section body) through EOF. Comments that sit 29 // immediately before the next [section] still attach to that next 30 // section's Comment, matching existing parse behavior. 31 TrailingComment string 32 name string 33 keys map[string]*Key 34 keyList []string 35 keysHash map[string]string 36 37 isRawSection bool 38 rawBody string 39 } 40 41 func newSection(f *File, name string) *Section { 42 return &Section{ 43 f: f, 44 name: name, 45 keys: make(map[string]*Key), 46 keyList: make([]string, 0, 10), 47 keysHash: make(map[string]string), 48 } 49 } 50 51 // Name returns name of Section. 52 func (s *Section) Name() string { 53 return s.name 54 } 55 56 // Body returns rawBody of Section if the section was marked as unparseable. 57 // It still follows the other rules of the INI format surrounding leading/trailing whitespace. 58 func (s *Section) Body() string { 59 return strings.TrimSpace(s.rawBody) 60 } 61 62 // SetBody updates body content only if section is raw. 63 func (s *Section) SetBody(body string) { 64 if !s.isRawSection { 65 return 66 } 67 s.rawBody = body 68 } 69 70 // NewKey creates a new key to given section. 71 func (s *Section) NewKey(name, val string) (*Key, error) { 72 if len(name) == 0 { 73 return nil, errors.New("error creating new key: empty key name") 74 } else if s.f.options.Insensitive || s.f.options.InsensitiveKeys { 75 name = strings.ToLower(name) 76 } 77 78 if s.f.BlockMode { 79 s.f.lock.Lock() 80 defer s.f.lock.Unlock() 81 } 82 83 if _, ok := s.keys[name]; ok { 84 if s.f.options.AllowShadows { 85 if err := s.keys[name].addShadow(val); err != nil { 86 return nil, err 87 } 88 } else { 89 s.keys[name].value = val 90 s.keysHash[name] = val 91 } 92 return s.keys[name], nil 93 } 94 95 s.keyList = append(s.keyList, name) 96 s.keys[name] = newKey(s, name, val) 97 s.keysHash[name] = val 98 return s.keys[name], nil 99 } 100 101 // NewBooleanKey creates a new boolean type key to given section. 102 func (s *Section) NewBooleanKey(name string) (*Key, error) { 103 key, err := s.NewKey(name, "true") 104 if err != nil { 105 return nil, err 106 } 107 108 key.isBooleanType = true 109 return key, nil 110 } 111 112 // GetKey returns key in section by given name. 113 func (s *Section) GetKey(name string) (*Key, error) { 114 if s.f.BlockMode { 115 s.f.lock.RLock() 116 } 117 if s.f.options.Insensitive || s.f.options.InsensitiveKeys { 118 name = strings.ToLower(name) 119 } 120 key := s.keys[name] 121 if s.f.BlockMode { 122 s.f.lock.RUnlock() 123 } 124 125 if key == nil { 126 // Check if it is a child-section. 127 sname := s.name 128 for { 129 if i := strings.LastIndex(sname, s.f.options.ChildSectionDelimiter); i > -1 { 130 sname = sname[:i] 131 sec, err := s.f.GetSection(sname) 132 if err != nil { 133 continue 134 } 135 return sec.GetKey(name) 136 } 137 break 138 } 139 return nil, fmt.Errorf("error when getting key of section %q: key %q does not exist", s.name, name) 140 } 141 return key, nil 142 } 143 144 // HasKey returns true if section contains a key with given name. 145 func (s *Section) HasKey(name string) bool { 146 key, _ := s.GetKey(name) 147 return key != nil 148 } 149 150 // Deprecated: Use "HasKey" instead. 151 func (s *Section) Haskey(name string) bool { 152 return s.HasKey(name) 153 } 154 155 // HasValue returns true if section contains given raw value. 156 func (s *Section) HasValue(value string) bool { 157 if s.f.BlockMode { 158 s.f.lock.RLock() 159 defer s.f.lock.RUnlock() 160 } 161 162 for _, k := range s.keys { 163 if value == k.value { 164 return true 165 } 166 } 167 return false 168 } 169 170 // Key assumes named Key exists in section and returns a zero-value when not. 171 // For an empty name, the returned Key is not part of the section, so setting 172 // its value changes nothing in the section. 173 func (s *Section) Key(name string) *Key { 174 key, err := s.GetKey(name) 175 if err != nil { 176 key, err = s.NewKey(name, "") 177 if err != nil { 178 // NewKey rejects empty names. Still honor the zero-value 179 // contract so Key("") is not nil. 180 return newKey(s, name, "") 181 } 182 return key 183 } 184 return key 185 } 186 187 // Keys returns list of keys of section. 188 func (s *Section) Keys() []*Key { 189 keys := make([]*Key, len(s.keyList)) 190 for i := range s.keyList { 191 keys[i] = s.Key(s.keyList[i]) 192 } 193 return keys 194 } 195 196 // ParentKeys returns list of keys of parent section. 197 func (s *Section) ParentKeys() []*Key { 198 var parentKeys []*Key 199 sname := s.name 200 for { 201 if i := strings.LastIndex(sname, s.f.options.ChildSectionDelimiter); i > -1 { 202 sname = sname[:i] 203 sec, err := s.f.GetSection(sname) 204 if err != nil { 205 continue 206 } 207 parentKeys = append(parentKeys, sec.Keys()...) 208 } else { 209 break 210 } 211 212 } 213 return parentKeys 214 } 215 216 // KeyStrings returns list of key names of section. 217 func (s *Section) KeyStrings() []string { 218 list := make([]string, len(s.keyList)) 219 copy(list, s.keyList) 220 return list 221 } 222 223 // KeysHash returns keys hash consisting of names and values. 224 func (s *Section) KeysHash() map[string]string { 225 if s.f.BlockMode { 226 s.f.lock.RLock() 227 defer s.f.lock.RUnlock() 228 } 229 230 hash := make(map[string]string, len(s.keysHash)) 231 for key, value := range s.keysHash { 232 hash[key] = value 233 } 234 return hash 235 } 236 237 // DeleteKey deletes a key from section. 238 func (s *Section) DeleteKey(name string) { 239 if s.f.options.Insensitive || s.f.options.InsensitiveKeys { 240 name = strings.ToLower(name) 241 } 242 243 if s.f.BlockMode { 244 s.f.lock.Lock() 245 defer s.f.lock.Unlock() 246 } 247 248 for i, k := range s.keyList { 249 if k == name { 250 s.keyList = append(s.keyList[:i], s.keyList[i+1:]...) 251 delete(s.keys, name) 252 delete(s.keysHash, name) 253 return 254 } 255 } 256 } 257 258 // ChildSections returns a list of child sections of current section. 259 // For example, "[parent.child1]" and "[parent.child12]" are child sections 260 // of section "[parent]". 261 func (s *Section) ChildSections() []*Section { 262 prefix := s.name + s.f.options.ChildSectionDelimiter 263 children := make([]*Section, 0, 3) 264 for _, name := range s.f.sectionList { 265 if strings.HasPrefix(name, prefix) { 266 children = append(children, s.f.sections[name]...) 267 } 268 } 269 return children 270 }