section.go 5.8 KB

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