conf.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // Copyright 2013 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 goconfig is a fully functional and comments-support configuration file(.ini) parser.
  15. package goconfig
  16. import (
  17. "fmt"
  18. "regexp"
  19. "runtime"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. )
  24. const (
  25. // Default section name.
  26. DEFAULT_SECTION = "DEFAULT"
  27. // Maximum allowed depth when recursively substituing variable names.
  28. _DEPTH_VALUES = 200
  29. )
  30. type ParseError int
  31. const (
  32. ERR_SECTION_NOT_FOUND ParseError = iota + 1
  33. ERR_KEY_NOT_FOUND
  34. ERR_BLANK_SECTION_NAME
  35. ERR_COULD_NOT_PARSE
  36. )
  37. var LineBreak = "\n"
  38. // Variable regexp pattern: %(variable)s
  39. var varPattern = regexp.MustCompile(`%\(([^\)]+)\)s`)
  40. func init() {
  41. if runtime.GOOS == "windows" {
  42. LineBreak = "\r\n"
  43. }
  44. }
  45. // A ConfigFile represents a INI formar configuration file.
  46. type ConfigFile struct {
  47. lock sync.RWMutex // Go map is not safe.
  48. fileNames []string // Support mutil-files.
  49. data map[string]map[string]string // Section -> key : value
  50. // Lists can keep sections and keys in order.
  51. sectionList []string // Section name list.
  52. keyList map[string][]string // Section -> Key name list
  53. sectionComments map[string]string // Sections comments.
  54. keyComments map[string]map[string]string // Keys comments.
  55. BlockMode bool // Indicates whether use lock or not.
  56. }
  57. // newConfigFile creates an empty configuration representation.
  58. func newConfigFile(fileNames []string) *ConfigFile {
  59. c := new(ConfigFile)
  60. c.fileNames = fileNames
  61. c.data = make(map[string]map[string]string)
  62. c.keyList = make(map[string][]string)
  63. c.sectionComments = make(map[string]string)
  64. c.keyComments = make(map[string]map[string]string)
  65. c.BlockMode = true
  66. return c
  67. }
  68. // SetValue adds a new section-key-value to the configuration.
  69. // It returns true if the key and value were inserted,
  70. // or returns false if the value was overwritten.
  71. // If the section does not exist in advance, it will be created.
  72. func (c *ConfigFile) SetValue(section, key, value string) bool {
  73. // Blank section name represents DEFAULT section.
  74. if len(section) == 0 {
  75. section = DEFAULT_SECTION
  76. }
  77. if len(key) == 0 {
  78. return false
  79. }
  80. if c.BlockMode {
  81. c.lock.Lock()
  82. defer c.lock.Unlock()
  83. }
  84. // Check if section exists.
  85. if _, ok := c.data[section]; !ok {
  86. // Execute add operation.
  87. c.data[section] = make(map[string]string)
  88. // Append section to list.
  89. c.sectionList = append(c.sectionList, section)
  90. }
  91. // Check if key exists.
  92. _, ok := c.data[section][key]
  93. c.data[section][key] = value
  94. if !ok {
  95. // If not exists, append to key list.
  96. c.keyList[section] = append(c.keyList[section], key)
  97. }
  98. return !ok
  99. }
  100. // DeleteKey deletes the key in given section.
  101. // It returns true if the key was deleted,
  102. // or returns false if the section or key didn't exist.
  103. func (c *ConfigFile) DeleteKey(section, key string) bool {
  104. // Blank section name represents DEFAULT section.
  105. if len(section) == 0 {
  106. section = DEFAULT_SECTION
  107. }
  108. // Check if section exists.
  109. if _, ok := c.data[section]; !ok {
  110. return false
  111. }
  112. // Check if key exists.
  113. if _, ok := c.data[section][key]; ok {
  114. delete(c.data[section], key)
  115. // Remove comments of key.
  116. c.SetKeyComments(section, key, "")
  117. // Get index of key.
  118. i := 0
  119. for _, keyName := range c.keyList[section] {
  120. if keyName == key {
  121. break
  122. }
  123. i++
  124. }
  125. // Remove from key list.
  126. c.keyList[section] = append(c.keyList[section][:i], c.keyList[section][i+1:]...)
  127. return true
  128. }
  129. return false
  130. }
  131. // GetValue returns the value of key available in the given section.
  132. // If the value needs to be unfolded
  133. // (see e.g. %(google)s example in the GoConfig_test.go),
  134. // then String does this unfolding automatically, up to
  135. // _DEPTH_VALUES number of iterations.
  136. // It returns an error and empty string value if the section does not exist,
  137. // or key does not exist in DEFAULT and current sections.
  138. func (c *ConfigFile) GetValue(section, key string) (string, error) {
  139. if c.BlockMode {
  140. c.lock.RLock()
  141. defer c.lock.RUnlock()
  142. }
  143. // Blank section name represents DEFAULT section.
  144. if len(section) == 0 {
  145. section = DEFAULT_SECTION
  146. }
  147. // Check if section exists
  148. if _, ok := c.data[section]; !ok {
  149. // Section does not exist.
  150. return "", getError{ERR_SECTION_NOT_FOUND, section}
  151. }
  152. // Section exists.
  153. // Check if key exists or empty value.
  154. value, ok := c.data[section][key]
  155. if !ok {
  156. // Check if it is a sub-section.
  157. if i := strings.LastIndex(section, "."); i > -1 {
  158. return c.GetValue(section[:i], key)
  159. }
  160. // Return empty value.
  161. return "", getError{ERR_KEY_NOT_FOUND, key}
  162. }
  163. // Key exists.
  164. var i int
  165. for i = 0; i < _DEPTH_VALUES; i++ {
  166. vr := varPattern.FindString(value)
  167. if len(vr) == 0 {
  168. break
  169. }
  170. // Take off leading '%(' and trailing ')s'.
  171. noption := strings.TrimLeft(vr, "%(")
  172. noption = strings.TrimRight(noption, ")s")
  173. // Search variable in default section.
  174. nvalue, err := c.GetValue(DEFAULT_SECTION, noption)
  175. if err != nil && section != DEFAULT_SECTION {
  176. // Search in the same section.
  177. if _, ok := c.data[section][noption]; ok {
  178. nvalue = c.data[section][noption]
  179. }
  180. }
  181. // Substitute by new value and take off leading '%(' and trailing ')s'.
  182. value = strings.Replace(value, vr, nvalue, -1)
  183. }
  184. return value, nil
  185. }
  186. // Bool returns bool type value.
  187. func (c *ConfigFile) Bool(section, key string) (bool, error) {
  188. value, err := c.GetValue(section, key)
  189. if err != nil {
  190. return false, err
  191. }
  192. return strconv.ParseBool(value)
  193. }
  194. // Float64 returns float64 type value.
  195. func (c *ConfigFile) Float64(section, key string) (float64, error) {
  196. value, err := c.GetValue(section, key)
  197. if err != nil {
  198. return 0.0, err
  199. }
  200. return strconv.ParseFloat(value, 64)
  201. }
  202. // Int returns int type value.
  203. func (c *ConfigFile) Int(section, key string) (int, error) {
  204. value, err := c.GetValue(section, key)
  205. if err != nil {
  206. return 0, err
  207. }
  208. return strconv.Atoi(value)
  209. }
  210. // Int64 returns int64 type value.
  211. func (c *ConfigFile) Int64(section, key string) (int64, error) {
  212. value, err := c.GetValue(section, key)
  213. if err != nil {
  214. return 0, err
  215. }
  216. return strconv.ParseInt(value, 10, 64)
  217. }
  218. // MustValue always returns value without error.
  219. // It returns empty string if error occurs, or the default value if given.
  220. func (c *ConfigFile) MustValue(section, key string, defaultVal ...string) string {
  221. val, err := c.GetValue(section, key)
  222. if len(defaultVal) > 0 && (err != nil || len(val) == 0) {
  223. return defaultVal[0]
  224. }
  225. return val
  226. }
  227. // MustValue always returns value without error,
  228. // It returns empty string if error occurs, or the default value if given,
  229. // and a bool value indicates whether default value is returned.
  230. func (c *ConfigFile) MustValueSet(section, key string, defaultVal ...string) (string, bool) {
  231. val, err := c.GetValue(section, key)
  232. if len(defaultVal) > 0 && (err != nil || len(val) == 0) {
  233. c.SetValue(section, key, defaultVal[0])
  234. return defaultVal[0], true
  235. }
  236. return val, false
  237. }
  238. // MustValueRange always returns value without error,
  239. // it returns default value if error occurs or doesn't fit into range.
  240. func (c *ConfigFile) MustValueRange(section, key, defaultVal string, candidates []string) string {
  241. val, err := c.GetValue(section, key)
  242. if err != nil || len(val) == 0 {
  243. return defaultVal
  244. }
  245. for _, cand := range candidates {
  246. if val == cand {
  247. return val
  248. }
  249. }
  250. return defaultVal
  251. }
  252. // MustValueArray always returns value array without error,
  253. // it returns empty array if error occurs, split by delimiter otherwise.
  254. func (c *ConfigFile) MustValueArray(section, key, delim string) []string {
  255. val, err := c.GetValue(section, key)
  256. if err != nil || len(val) == 0 {
  257. return []string{}
  258. }
  259. vals := strings.Split(val, delim)
  260. for i := range vals {
  261. vals[i] = strings.TrimSpace(vals[i])
  262. }
  263. return vals
  264. }
  265. // MustBool always returns value without error,
  266. // it returns false if error occurs.
  267. func (c *ConfigFile) MustBool(section, key string, defaultVal ...bool) bool {
  268. val, err := c.Bool(section, key)
  269. if len(defaultVal) > 0 && err != nil {
  270. return defaultVal[0]
  271. }
  272. return val
  273. }
  274. // MustFloat64 always returns value without error,
  275. // it returns 0.0 if error occurs.
  276. func (c *ConfigFile) MustFloat64(section, key string, defaultVal ...float64) float64 {
  277. value, err := c.Float64(section, key)
  278. if len(defaultVal) > 0 && err != nil {
  279. return defaultVal[0]
  280. }
  281. return value
  282. }
  283. // MustInt always returns value without error,
  284. // it returns 0 if error occurs.
  285. func (c *ConfigFile) MustInt(section, key string, defaultVal ...int) int {
  286. value, err := c.Int(section, key)
  287. if len(defaultVal) > 0 && err != nil {
  288. return defaultVal[0]
  289. }
  290. return value
  291. }
  292. // MustInt64 always returns value without error,
  293. // it returns 0 if error occurs.
  294. func (c *ConfigFile) MustInt64(section, key string, defaultVal ...int64) int64 {
  295. value, err := c.Int64(section, key)
  296. if len(defaultVal) > 0 && err != nil {
  297. return defaultVal[0]
  298. }
  299. return value
  300. }
  301. // GetSectionList returns the list of all sections
  302. // in the same order in the file.
  303. func (c *ConfigFile) GetSectionList() []string {
  304. list := make([]string, len(c.sectionList))
  305. copy(list, c.sectionList)
  306. return list
  307. }
  308. // GetKeyList returns the list of all keys in give section
  309. // in the same order in the file.
  310. // It returns nil if given section does not exist.
  311. func (c *ConfigFile) GetKeyList(section string) []string {
  312. // Blank section name represents DEFAULT section.
  313. if len(section) == 0 {
  314. section = DEFAULT_SECTION
  315. }
  316. // Check if section exists.
  317. if _, ok := c.data[section]; !ok {
  318. return nil
  319. }
  320. // Non-default section has a blank key as section keeper.
  321. offset := 1
  322. if section == DEFAULT_SECTION {
  323. offset = 0
  324. }
  325. list := make([]string, len(c.keyList[section])-offset)
  326. copy(list, c.keyList[section][offset:])
  327. return list
  328. }
  329. // DeleteSection deletes the entire section by given name.
  330. // It returns true if the section was deleted, and false if the section didn't exist.
  331. func (c *ConfigFile) DeleteSection(section string) bool {
  332. // Blank section name represents DEFAULT section.
  333. if len(section) == 0 {
  334. section = DEFAULT_SECTION
  335. }
  336. // Check if section exists.
  337. if _, ok := c.data[section]; !ok {
  338. return false
  339. }
  340. delete(c.data, section)
  341. // Remove comments of section.
  342. c.SetSectionComments(section, "")
  343. // Get index of section.
  344. i := 0
  345. for _, secName := range c.sectionList {
  346. if secName == section {
  347. break
  348. }
  349. i++
  350. }
  351. // Remove from section and key list.
  352. c.sectionList = append(c.sectionList[:i], c.sectionList[i+1:]...)
  353. delete(c.keyList, section)
  354. return true
  355. }
  356. // GetSection returns key-value pairs in given section.
  357. // It section does not exist, returns nil and error.
  358. func (c *ConfigFile) GetSection(section string) (map[string]string, error) {
  359. // Blank section name represents DEFAULT section.
  360. if len(section) == 0 {
  361. section = DEFAULT_SECTION
  362. }
  363. // Check if section exists.
  364. if _, ok := c.data[section]; !ok {
  365. // Section does not exist.
  366. return nil, getError{ERR_SECTION_NOT_FOUND, section}
  367. }
  368. // Remove pre-defined key.
  369. secMap := c.data[section]
  370. delete(c.data[section], " ")
  371. // Section exists.
  372. return secMap, nil
  373. }
  374. // SetSectionComments adds new section comments to the configuration.
  375. // If comments are empty(0 length), it will remove its section comments!
  376. // It returns true if the comments were inserted or removed,
  377. // or returns false if the comments were overwritten.
  378. func (c *ConfigFile) SetSectionComments(section, comments string) bool {
  379. // Blank section name represents DEFAULT section.
  380. if len(section) == 0 {
  381. section = DEFAULT_SECTION
  382. }
  383. if len(comments) == 0 {
  384. if _, ok := c.sectionComments[section]; ok {
  385. delete(c.sectionComments, section)
  386. }
  387. // Not exists can be seen as remove.
  388. return true
  389. }
  390. // Check if comments exists.
  391. _, ok := c.sectionComments[section]
  392. if comments[0] != '#' && comments[0] != ';' {
  393. comments = "; " + comments
  394. }
  395. c.sectionComments[section] = comments
  396. return !ok
  397. }
  398. // SetKeyComments adds new section-key comments to the configuration.
  399. // If comments are empty(0 length), it will remove its section-key comments!
  400. // It returns true if the comments were inserted or removed,
  401. // or returns false if the comments were overwritten.
  402. // If the section does not exist in advance, it is created.
  403. func (c *ConfigFile) SetKeyComments(section, key, comments string) bool {
  404. // Blank section name represents DEFAULT section.
  405. if len(section) == 0 {
  406. section = DEFAULT_SECTION
  407. }
  408. // Check if section exists.
  409. if _, ok := c.keyComments[section]; ok {
  410. if len(comments) == 0 {
  411. if _, ok := c.keyComments[section][key]; ok {
  412. delete(c.keyComments[section], key)
  413. }
  414. // Not exists can be seen as remove.
  415. return true
  416. }
  417. } else {
  418. if len(comments) == 0 {
  419. // Not exists can be seen as remove.
  420. return true
  421. } else {
  422. // Execute add operation.
  423. c.keyComments[section] = make(map[string]string)
  424. }
  425. }
  426. // Check if key exists.
  427. _, ok := c.keyComments[section][key]
  428. if comments[0] != '#' && comments[0] != ';' {
  429. comments = "; " + comments
  430. }
  431. c.keyComments[section][key] = comments
  432. return !ok
  433. }
  434. // GetSectionComments returns the comments in the given section.
  435. // It returns an empty string(0 length) if the comments do not exist.
  436. func (c *ConfigFile) GetSectionComments(section string) (comments string) {
  437. // Blank section name represents DEFAULT section.
  438. if len(section) == 0 {
  439. section = DEFAULT_SECTION
  440. }
  441. return c.sectionComments[section]
  442. }
  443. // GetKeyComments returns the comments of key in the given section.
  444. // It returns an empty string(0 length) if the comments do not exist.
  445. func (c *ConfigFile) GetKeyComments(section, key string) (comments string) {
  446. // Blank section name represents DEFAULT section.
  447. if len(section) == 0 {
  448. section = DEFAULT_SECTION
  449. }
  450. if _, ok := c.keyComments[section]; ok {
  451. return c.keyComments[section][key]
  452. }
  453. return ""
  454. }
  455. // getError occurs when get value in configuration file with invalid parameter.
  456. type getError struct {
  457. Reason ParseError
  458. Name string
  459. }
  460. // Error implements Error interface.
  461. func (err getError) Error() string {
  462. switch err.Reason {
  463. case ERR_SECTION_NOT_FOUND:
  464. return fmt.Sprintf("section '%s' not found", err.Name)
  465. case ERR_KEY_NOT_FOUND:
  466. return fmt.Sprintf("key '%s' not found", err.Name)
  467. }
  468. return "invalid get error"
  469. }