本站源代码
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

257 行
5.7KB

  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. }
上海开阖软件有限公司 沪ICP备12045867号-1