1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 19:50:56 +00:00
Beego/core/config/fake.go

117 lines
2.9 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 (
2020-08-26 03:46:22 +00:00
"context"
2020-07-22 14:50:08 +00:00
"errors"
"strconv"
"strings"
)
type fakeConfigContainer struct {
2020-08-19 14:08:29 +00:00
BaseConfiger
2020-07-22 14:50:08 +00:00
data map[string]string
}
func (c *fakeConfigContainer) getData(key string) string {
return c.data[strings.ToLower(key)]
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) Set(key, val string) error {
2020-07-22 14:50:08 +00:00
c.data[strings.ToLower(key)] = val
return nil
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) Int(key string) (int, error) {
2020-07-22 14:50:08 +00:00
return strconv.Atoi(c.getData(key))
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) DefaultInt(key string, defaultVal int) int {
v, err := c.Int(key)
2020-07-22 14:50:08 +00:00
if err != nil {
return defaultVal
2020-07-22 14:50:08 +00:00
}
return v
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) Int64(key string) (int64, error) {
2020-07-22 14:50:08 +00:00
return strconv.ParseInt(c.getData(key), 10, 64)
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) DefaultInt64(key string, defaultVal int64) int64 {
v, err := c.Int64(key)
2020-07-22 14:50:08 +00:00
if err != nil {
return defaultVal
2020-07-22 14:50:08 +00:00
}
return v
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) Bool(key string) (bool, error) {
2020-07-22 14:50:08 +00:00
return ParseBool(c.getData(key))
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) DefaultBool(key string, defaultVal bool) bool {
v, err := c.Bool(key)
2020-07-22 14:50:08 +00:00
if err != nil {
return defaultVal
2020-07-22 14:50:08 +00:00
}
return v
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) Float(key string) (float64, error) {
2020-07-22 14:50:08 +00:00
return strconv.ParseFloat(c.getData(key), 64)
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) DefaultFloat(key string, defaultVal float64) float64 {
v, err := c.Float(key)
2020-07-22 14:50:08 +00:00
if err != nil {
return defaultVal
2020-07-22 14:50:08 +00:00
}
return v
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
2020-07-22 14:50:08 +00:00
if v, ok := c.data[strings.ToLower(key)]; ok {
return v, nil
}
return nil, errors.New("key not find")
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
2020-07-22 14:50:08 +00:00
return nil, errors.New("not implement in the fakeConfigContainer")
}
2020-10-13 14:32:41 +00:00
func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
2020-07-22 14:50:08 +00:00
return errors.New("not implement in the fakeConfigContainer")
}
2020-10-13 16:24:22 +00:00
func (c *fakeConfigContainer) Unmarshaler(prefix string, obj interface{}, opt ...DecodeOption) error {
return errors.New("unsupported operation")
}
2020-07-22 14:50:08 +00:00
var _ Configer = new(fakeConfigContainer)
// NewFakeConfig return a fake Configer
func NewFakeConfig() Configer {
2020-08-26 03:46:22 +00:00
res := &fakeConfigContainer{
2020-07-22 14:50:08 +00:00
data: make(map[string]string),
}
2020-08-26 03:46:22 +00:00
res.BaseConfiger = NewBaseConfiger(func(ctx context.Context, key string) (string, error) {
return res.getData(key), nil
})
return res
2020-07-22 14:50:08 +00:00
}