github.com/eframework-cn/EP.GO.UTIL@v1.0.0/xconfig/fake.go (about)

     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  
    15  package xconfig
    16  
    17  import (
    18  	"errors"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  type fakeConfigContainer struct {
    24  	data map[string]string
    25  }
    26  
    27  func (c *fakeConfigContainer) getData(key string) string {
    28  	return c.data[strings.ToLower(key)]
    29  }
    30  
    31  func (c *fakeConfigContainer) Set(key, val string) error {
    32  	c.data[strings.ToLower(key)] = val
    33  	return nil
    34  }
    35  
    36  func (c *fakeConfigContainer) String(key string) string {
    37  	return c.getData(key)
    38  }
    39  
    40  func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
    41  	v := c.String(key)
    42  	if v == "" {
    43  		return defaultval
    44  	}
    45  	return v
    46  }
    47  
    48  func (c *fakeConfigContainer) Strings(key string) []string {
    49  	v := c.String(key)
    50  	if v == "" {
    51  		return nil
    52  	}
    53  	return strings.Split(v, ";")
    54  }
    55  
    56  func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
    57  	v := c.Strings(key)
    58  	if v == nil {
    59  		return defaultval
    60  	}
    61  	return v
    62  }
    63  
    64  func (c *fakeConfigContainer) Int(key string) (int, error) {
    65  	return strconv.Atoi(c.getData(key))
    66  }
    67  
    68  func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
    69  	v, err := c.Int(key)
    70  	if err != nil {
    71  		return defaultval
    72  	}
    73  	return v
    74  }
    75  
    76  func (c *fakeConfigContainer) Int64(key string) (int64, error) {
    77  	return strconv.ParseInt(c.getData(key), 10, 64)
    78  }
    79  
    80  func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
    81  	v, err := c.Int64(key)
    82  	if err != nil {
    83  		return defaultval
    84  	}
    85  	return v
    86  }
    87  
    88  func (c *fakeConfigContainer) Bool(key string) (bool, error) {
    89  	return ParseBool(c.getData(key))
    90  }
    91  
    92  func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
    93  	v, err := c.Bool(key)
    94  	if err != nil {
    95  		return defaultval
    96  	}
    97  	return v
    98  }
    99  
   100  func (c *fakeConfigContainer) Float(key string) (float64, error) {
   101  	return strconv.ParseFloat(c.getData(key), 64)
   102  }
   103  
   104  func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
   105  	v, err := c.Float(key)
   106  	if err != nil {
   107  		return defaultval
   108  	}
   109  	return v
   110  }
   111  
   112  func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
   113  	if v, ok := c.data[strings.ToLower(key)]; ok {
   114  		return v, nil
   115  	}
   116  	return nil, errors.New("key not find")
   117  }
   118  
   119  func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
   120  	return nil, errors.New("not implement in the fakeConfigContainer")
   121  }
   122  
   123  func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
   124  	return errors.New("not implement in the fakeConfigContainer")
   125  }
   126  
   127  var _ Configer = new(fakeConfigContainer)
   128  
   129  // NewFakeConfig return a fake Configer
   130  func NewFakeConfig() Configer {
   131  	return &fakeConfigContainer{
   132  		data: make(map[string]string),
   133  	}
   134  }