section.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // NewKey creates a new key to given section.
  50. func (s *Section) NewKey(name, val string) (*Key, error) {
  51. if len(name) == 0 {
  52. return nil, errors.New("error creating new key: empty key name")
  53. } else if s.f.options.Insensitive {
  54. name = strings.ToLower(name)
  55. }
  56. if s.f.BlockMode {
  57. s.f.lock.Lock()
  58. defer s.f.lock.Unlock()
  59. }
  60. if inSlice(name, s.keyList) {
  61. s.keys[name].value = val
  62. return s.keys[name], nil
  63. }
  64. s.keyList = append(s.keyList, name)
  65. s.keys[name] = &Key{
  66. s: s,
  67. name: name,
  68. value: val,
  69. }
  70. s.keysHash[name] = val
  71. return s.keys[name], nil
  72. }
  73. // NewBooleanKey creates a new boolean type key to given section.
  74. func (s *Section) NewBooleanKey(name string) (*Key, error) {
  75. key, err := s.NewKey(name, "true")
  76. if err != nil {
  77. return nil, err
  78. }
  79. key.isBooleanType = true
  80. return key, nil
  81. }
  82. // GetKey returns key in section by given name.
  83. func (s *Section) GetKey(name string) (*Key, error) {
  84. // FIXME: change to section level lock?
  85. if s.f.BlockMode {
  86. s.f.lock.RLock()
  87. }
  88. if s.f.options.Insensitive {
  89. name = strings.ToLower(name)
  90. }
  91. key := s.keys[name]
  92. if s.f.BlockMode {
  93. s.f.lock.RUnlock()
  94. }
  95. if key == nil {
  96. // Check if it is a child-section.
  97. sname := s.name
  98. for {
  99. if i := strings.LastIndex(sname, "."); i > -1 {
  100. sname = sname[:i]
  101. sec, err := s.f.GetSection(sname)
  102. if err != nil {
  103. continue
  104. }
  105. return sec.GetKey(name)
  106. } else {
  107. break
  108. }
  109. }
  110. return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name)
  111. }
  112. return key, nil
  113. }
  114. // HasKey returns true if section contains a key with given name.
  115. func (s *Section) HasKey(name string) bool {
  116. key, _ := s.GetKey(name)
  117. return key != nil
  118. }
  119. // Haskey is a backwards-compatible name for HasKey.
  120. func (s *Section) Haskey(name string) bool {
  121. return s.HasKey(name)
  122. }
  123. // HasValue returns true if section contains given raw value.
  124. func (s *Section) HasValue(value string) bool {
  125. if s.f.BlockMode {
  126. s.f.lock.RLock()
  127. defer s.f.lock.RUnlock()
  128. }
  129. for _, k := range s.keys {
  130. if value == k.value {
  131. return true
  132. }
  133. }
  134. return false
  135. }
  136. // Key assumes named Key exists in section and returns a zero-value when not.
  137. func (s *Section) Key(name string) *Key {
  138. key, err := s.GetKey(name)
  139. if err != nil {
  140. // It's OK here because the only possible error is empty key name,
  141. // but if it's empty, this piece of code won't be executed.
  142. key, _ = s.NewKey(name, "")
  143. return key
  144. }
  145. return key
  146. }
  147. // Keys returns list of keys of section.
  148. func (s *Section) Keys() []*Key {
  149. keys := make([]*Key, len(s.keyList))
  150. for i := range s.keyList {
  151. keys[i] = s.Key(s.keyList[i])
  152. }
  153. return keys
  154. }
  155. // ParentKeys returns list of keys of parent section.
  156. func (s *Section) ParentKeys() []*Key {
  157. var parentKeys []*Key
  158. sname := s.name
  159. for {
  160. if i := strings.LastIndex(sname, "."); i > -1 {
  161. sname = sname[:i]
  162. sec, err := s.f.GetSection(sname)
  163. if err != nil {
  164. continue
  165. }
  166. parentKeys = append(parentKeys, sec.Keys()...)
  167. } else {
  168. break
  169. }
  170. }
  171. return parentKeys
  172. }
  173. // KeyStrings returns list of key names of section.
  174. func (s *Section) KeyStrings() []string {
  175. list := make([]string, len(s.keyList))
  176. copy(list, s.keyList)
  177. return list
  178. }
  179. // KeysHash returns keys hash consisting of names and values.
  180. func (s *Section) KeysHash() map[string]string {
  181. if s.f.BlockMode {
  182. s.f.lock.RLock()
  183. defer s.f.lock.RUnlock()
  184. }
  185. hash := map[string]string{}
  186. for key, value := range s.keysHash {
  187. hash[key] = value
  188. }
  189. return hash
  190. }
  191. // DeleteKey deletes a key from section.
  192. func (s *Section) DeleteKey(name string) {
  193. if s.f.BlockMode {
  194. s.f.lock.Lock()
  195. defer s.f.lock.Unlock()
  196. }
  197. for i, k := range s.keyList {
  198. if k == name {
  199. s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)
  200. delete(s.keys, name)
  201. return
  202. }
  203. }
  204. }