fake.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "errors"
  17. "strconv"
  18. "strings"
  19. )
  20. type fakeConfigContainer struct {
  21. data map[string]string
  22. }
  23. func (c *fakeConfigContainer) getData(key string) string {
  24. return c.data[strings.ToLower(key)]
  25. }
  26. func (c *fakeConfigContainer) Set(key, val string) error {
  27. c.data[strings.ToLower(key)] = val
  28. return nil
  29. }
  30. func (c *fakeConfigContainer) String(key string) string {
  31. return c.getData(key)
  32. }
  33. func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
  34. v := c.String(key)
  35. if v == "" {
  36. return defaultval
  37. }
  38. return v
  39. }
  40. func (c *fakeConfigContainer) Strings(key string) []string {
  41. v := c.String(key)
  42. if v == "" {
  43. return nil
  44. }
  45. return strings.Split(v, ";")
  46. }
  47. func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
  48. v := c.Strings(key)
  49. if v == nil {
  50. return defaultval
  51. }
  52. return v
  53. }
  54. func (c *fakeConfigContainer) Int(key string) (int, error) {
  55. return strconv.Atoi(c.getData(key))
  56. }
  57. func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
  58. v, err := c.Int(key)
  59. if err != nil {
  60. return defaultval
  61. }
  62. return v
  63. }
  64. func (c *fakeConfigContainer) Int64(key string) (int64, error) {
  65. return strconv.ParseInt(c.getData(key), 10, 64)
  66. }
  67. func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
  68. v, err := c.Int64(key)
  69. if err != nil {
  70. return defaultval
  71. }
  72. return v
  73. }
  74. func (c *fakeConfigContainer) Bool(key string) (bool, error) {
  75. return ParseBool(c.getData(key))
  76. }
  77. func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
  78. v, err := c.Bool(key)
  79. if err != nil {
  80. return defaultval
  81. }
  82. return v
  83. }
  84. func (c *fakeConfigContainer) Float(key string) (float64, error) {
  85. return strconv.ParseFloat(c.getData(key), 64)
  86. }
  87. func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
  88. v, err := c.Float(key)
  89. if err != nil {
  90. return defaultval
  91. }
  92. return v
  93. }
  94. func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
  95. if v, ok := c.data[strings.ToLower(key)]; ok {
  96. return v, nil
  97. }
  98. return nil, errors.New("key not find")
  99. }
  100. func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
  101. return nil, errors.New("not implement in the fakeConfigContainer")
  102. }
  103. func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
  104. return errors.New("not implement in the fakeConfigContainer")
  105. }
  106. var _ Configer = new(fakeConfigContainer)
  107. // NewFakeConfig return a fake Congiger
  108. func NewFakeConfig() Configer {
  109. return &fakeConfigContainer{
  110. data: make(map[string]string),
  111. }
  112. }