section.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // FIXME: change to section level lock?
  95. if s.f.BlockMode {
  96. s.f.lock.RLock()
  97. }
  98. if s.f.options.Insensitive {
  99. name = strings.ToLower(name)
  100. }
  101. key := s.keys[name]
  102. if s.f.BlockMode {
  103. s.f.lock.RUnlock()
  104. }
  105. if key == nil {
  106. // Check if it is a child-section.
  107. sname := s.name
  108. for {
  109. if i := strings.LastIndex(sname, "."); i > -1 {
  110. sname = sname[:i]
  111. sec, err := s.f.GetSection(sname)
  112. if err != nil {
  113. continue
  114. }
  115. return sec.GetKey(name)
  116. } else {
  117. break
  118. }
  119. }
  120. return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
  121. }
  122. return key, nil
  123. }
  124. // HasKey returns true if section contains a key with given name.
  125. func (s *Section) HasKey(name string) bool {
  126. key, _ := s.GetKey(name)
  127. return key != nil
  128. }
  129. // Haskey is a backwards-compatible name for HasKey.
  130. // TODO: delete me in v2
  131. func (s *Section) Haskey(name string) bool {
  132. return s.HasKey(name)
  133. }
  134. // HasValue returns true if section contains given raw value.
  135. func (s *Section) HasValue(value string) bool {
  136. if s.f.BlockMode {
  137. s.f.lock.RLock()
  138. defer s.f.lock.RUnlock()
  139. }
  140. for _, k := range s.keys {
  141. if value == k.value {
  142. return true
  143. }
  144. }
  145. return false
  146. }
  147. // Key assumes named Key exists in section and returns a zero-value when not.
  148. func (s *Section) Key(name string) *Key {
  149. key, err := s.GetKey(name)
  150. if err != nil {
  151. // It's OK here because the only possible error is empty key name,
  152. // but if it's empty, this piece of code won't be executed.
  153. key, _ = s.NewKey(name, "")
  154. return key
  155. }
  156. return key
  157. }
  158. // Keys returns list of keys of section.
  159. func (s *Section) Keys() []*Key {
  160. keys := make([]*Key, len(s.keyList))
  161. for i := range s.keyList {
  162. keys[i] = s.Key(s.keyList[i])
  163. }
  164. return keys
  165. }
  166. // ParentKeys returns list of keys of parent section.
  167. func (s *Section) ParentKeys() []*Key {
  168. var parentKeys []*Key
  169. sname := s.name
  170. for {
  171. if i := strings.LastIndex(sname, "."); i > -1 {
  172. sname = sname[:i]
  173. sec, err := s.f.GetSection(sname)
  174. if err != nil {
  175. continue
  176. }
  177. parentKeys = append(parentKeys, sec.Keys()...)
  178. } else {
  179. break
  180. }
  181. }
  182. return parentKeys
  183. }
  184. // KeyStrings returns list of key names of section.
  185. func (s *Section) KeyStrings() []string {
  186. list := make([]string, len(s.keyList))
  187. copy(list, s.keyList)
  188. return list
  189. }
  190. // KeysHash returns keys hash consisting of names and values.
  191. func (s *Section) KeysHash() map[string]string {
  192. if s.f.BlockMode {
  193. s.f.lock.RLock()
  194. defer s.f.lock.RUnlock()
  195. }
  196. hash := map[string]string{}
  197. for key, value := range s.keysHash {
  198. hash[key] = value
  199. }
  200. return hash
  201. }
  202. // DeleteKey deletes a key from section.
  203. func (s *Section) DeleteKey(name string) {
  204. if s.f.BlockMode {
  205. s.f.lock.Lock()
  206. defer s.f.lock.Unlock()
  207. }
  208. for i, k := range s.keyList {
  209. if k == name {
  210. s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)
  211. delete(s.keys, name)
  212. delete(s.keysHash, name)
  213. return
  214. }
  215. }
  216. }
  217. // ChildSections returns a list of child sections of current section.
  218. // For example, "[parent.child1]" and "[parent.child12]" are child sections
  219. // of section "[parent]".
  220. func (s *Section) ChildSections() []*Section {
  221. prefix := s.name + "."
  222. children := make([]*Section, 0, 3)
  223. for _, name := range s.f.sectionList {
  224. if strings.HasPrefix(name, prefix) {
  225. children = append(children, s.f.sections[name])
  226. }
  227. }
  228. return children
  229. }