section.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. package ini
  15. import (
  16. "errors"
  17. "fmt"
  18. "strings"
  19. )
  20. // Section represents a config section.
  21. type Section struct {
  22. f *File
  23. Comment string
  24. name string
  25. keys map[string]*Key
  26. keyList []string
  27. keysHash map[string]string
  28. isRawSection bool
  29. rawBody string
  30. }
  31. func newSection(f *File, name string) *Section {
  32. return &Section{
  33. f: f,
  34. name: name,
  35. keys: make(map[string]*Key),
  36. keyList: make([]string, 0, 10),
  37. keysHash: make(map[string]string),
  38. }
  39. }
  40. // Name returns name of Section.
  41. func (s *Section) Name() string {
  42. return s.name
  43. }
  44. // Body returns rawBody of Section if the section was marked as unparseable.
  45. // It still follows the other rules of the INI format surrounding leading/trailing whitespace.
  46. func (s *Section) Body() string {
  47. return strings.TrimSpace(s.rawBody)
  48. }
  49. // SetBody updates body content only if section is raw.
  50. func (s *Section) SetBody(body string) {
  51. if !s.isRawSection {
  52. return
  53. }
  54. s.rawBody = body
  55. }
  56. // NewKey creates a new key to given section.
  57. func (s *Section) NewKey(name, val string) (*Key, error) {
  58. if len(name) == 0 {
  59. return nil, errors.New("error creating new key: empty key name")
  60. } else if s.f.options.Insensitive {
  61. name = strings.ToLower(name)
  62. }
  63. if s.f.BlockMode {
  64. s.f.lock.Lock()
  65. defer s.f.lock.Unlock()
  66. }
  67. if inSlice(name, s.keyList) {
  68. if s.f.options.AllowShadows {
  69. if err := s.keys[name].addShadow(val); err != nil {
  70. return nil, err
  71. }
  72. } else {
  73. s.keys[name].value = val
  74. s.keysHash[name] = val
  75. }
  76. return s.keys[name], nil
  77. }
  78. s.keyList = append(s.keyList, name)
  79. s.keys[name] = newKey(s, name, val)
  80. s.keysHash[name] = val
  81. return s.keys[name], nil
  82. }
  83. // NewBooleanKey creates a new boolean type key to given section.
  84. func (s *Section) NewBooleanKey(name string) (*Key, error) {
  85. key, err := s.NewKey(name, "true")
  86. if err != nil {
  87. return nil, err
  88. }
  89. key.isBooleanType = true
  90. return key, nil
  91. }
  92. // GetKey returns key in section by given name.
  93. func (s *Section) GetKey(name string) (*Key, error) {
  94. if s.f.BlockMode {
  95. s.f.lock.RLock()
  96. }
  97. if s.f.options.Insensitive {
  98. name = strings.ToLower(name)
  99. }
  100. key := s.keys[name]
  101. if s.f.BlockMode {
  102. s.f.lock.RUnlock()
  103. }
  104. if key == nil {
  105. // Check if it is a child-section.
  106. sname := s.name
  107. for {
  108. if i := strings.LastIndex(sname, "."); i > -1 {
  109. sname = sname[:i]
  110. sec, err := s.f.GetSection(sname)
  111. if err != nil {
  112. continue
  113. }
  114. return sec.GetKey(name)
  115. }
  116. break
  117. }
  118. return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
  119. }
  120. return key, nil
  121. }
  122. // HasKey returns true if section contains a key with given name.
  123. func (s *Section) HasKey(name string) bool {
  124. key, _ := s.GetKey(name)
  125. return key != nil
  126. }
  127. // Deprecated: Use "HasKey" instead.
  128. func (s *Section) Haskey(name string) bool {
  129. return s.HasKey(name)
  130. }
  131. // HasValue returns true if section contains given raw value.
  132. func (s *Section) HasValue(value string) bool {
  133. if s.f.BlockMode {
  134. s.f.lock.RLock()
  135. defer s.f.lock.RUnlock()
  136. }
  137. for _, k := range s.keys {
  138. if value == k.value {
  139. return true
  140. }
  141. }
  142. return false
  143. }
  144. // Key assumes named Key exists in section and returns a zero-value when not.
  145. func (s *Section) Key(name string) *Key {
  146. key, err := s.GetKey(name)
  147. if err != nil {
  148. // It's OK here because the only possible error is empty key name,
  149. // but if it's empty, this piece of code won't be executed.
  150. key, _ = s.NewKey(name, "")
  151. return key
  152. }
  153. return key
  154. }
  155. // Keys returns list of keys of section.
  156. func (s *Section) Keys() []*Key {
  157. keys := make([]*Key, len(s.keyList))
  158. for i := range s.keyList {
  159. keys[i] = s.Key(s.keyList[i])
  160. }
  161. return keys
  162. }
  163. // ParentKeys returns list of keys of parent section.
  164. func (s *Section) ParentKeys() []*Key {
  165. var parentKeys []*Key
  166. sname := s.name
  167. for {
  168. if i := strings.LastIndex(sname, "."); i > -1 {
  169. sname = sname[:i]
  170. sec, err := s.f.GetSection(sname)
  171. if err != nil {
  172. continue
  173. }
  174. parentKeys = append(parentKeys, sec.Keys()...)
  175. } else {
  176. break
  177. }
  178. }
  179. return parentKeys
  180. }
  181. // KeyStrings returns list of key names of section.
  182. func (s *Section) KeyStrings() []string {
  183. list := make([]string, len(s.keyList))
  184. copy(list, s.keyList)
  185. return list
  186. }
  187. // KeysHash returns keys hash consisting of names and values.
  188. func (s *Section) KeysHash() map[string]string {
  189. if s.f.BlockMode {
  190. s.f.lock.RLock()
  191. defer s.f.lock.RUnlock()
  192. }
  193. hash := map[string]string{}
  194. for key, value := range s.keysHash {
  195. hash[key] = value
  196. }
  197. return hash
  198. }
  199. // DeleteKey deletes a key from section.
  200. func (s *Section) DeleteKey(name string) {
  201. if s.f.BlockMode {
  202. s.f.lock.Lock()
  203. defer s.f.lock.Unlock()
  204. }
  205. for i, k := range s.keyList {
  206. if k == name {
  207. s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)
  208. delete(s.keys, name)
  209. delete(s.keysHash, name)
  210. return
  211. }
  212. }
  213. }
  214. // ChildSections returns a list of child sections of current section.
  215. // For example, "[parent.child1]" and "[parent.child12]" are child sections
  216. // of section "[parent]".
  217. func (s *Section) ChildSections() []*Section {
  218. prefix := s.name + "."
  219. children := make([]*Section, 0, 3)
  220. for _, name := range s.f.sectionList {
  221. if strings.HasPrefix(name, prefix) {
  222. children = append(children, s.f.sections[name])
  223. }
  224. }
  225. return children
  226. }