1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 21:40:54 +00:00
Beego/config/fake.go

135 lines
3.1 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"errors"
"strconv"
"strings"
)
type fakeConfigContainer struct {
data map[string]string
}
func (c *fakeConfigContainer) getData(key string) string {
return c.data[strings.ToLower(key)]
}
func (c *fakeConfigContainer) Set(key, val string) error {
2014-07-12 08:03:14 +00:00
c.data[strings.ToLower(key)] = val
return nil
}
func (c *fakeConfigContainer) String(key string) string {
return c.getData(key)
}
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
v := c.String(key)
2015-09-10 06:53:19 +00:00
if v == "" {
return defaultval
}
2015-09-10 06:53:19 +00:00
return v
}
func (c *fakeConfigContainer) Strings(key string) []string {
v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
}
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
2015-09-10 06:53:19 +00:00
v := c.Strings(key)
if v == nil {
return defaultval
}
2015-09-10 06:53:19 +00:00
return v
}
func (c *fakeConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.getData(key))
}
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
2015-09-10 06:53:19 +00:00
v, err := c.Int(key)
if err != nil {
return defaultval
}
2015-09-10 06:53:19 +00:00
return v
}
func (c *fakeConfigContainer) Int64(key string) (int64, error) {
return strconv.ParseInt(c.getData(key), 10, 64)
}
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
2015-09-10 06:53:19 +00:00
v, err := c.Int64(key)
if err != nil {
return defaultval
}
2015-09-10 06:53:19 +00:00
return v
}
func (c *fakeConfigContainer) Bool(key string) (bool, error) {
return ParseBool(c.getData(key))
}
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
2015-09-10 06:53:19 +00:00
v, err := c.Bool(key)
if err != nil {
return defaultval
}
2015-09-10 06:53:19 +00:00
return v
}
func (c *fakeConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.getData(key), 64)
}
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
2015-09-10 06:53:19 +00:00
v, err := c.Float(key)
if err != nil {
return defaultval
}
2015-09-10 06:53:19 +00:00
return v
}
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
2014-07-12 08:03:14 +00:00
if v, ok := c.data[strings.ToLower(key)]; ok {
return v, nil
}
return nil, errors.New("key not find")
}
func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
return nil, errors.New("not implement in the fakeConfigContainer")
}
func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
return errors.New("not implement in the fakeConfigContainer")
}
2015-09-10 06:53:19 +00:00
var _ Configer = new(fakeConfigContainer)
2018-03-27 15:29:13 +00:00
// NewFakeConfig return a fake Configer
2015-09-10 06:53:19 +00:00
func NewFakeConfig() Configer {
return &fakeConfigContainer{
data: make(map[string]string),
}
}