-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_service.go
86 lines (67 loc) · 2.09 KB
/
fake_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package config
import (
"github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
"gopkg.in/yaml.v2"
"strings"
"time"
)
type FakeConfig struct {
confMap map[string]interface{}
}
func (conf *FakeConfig) find(key string) interface{} {
return searchMap(conf.confMap, strings.Split(key, "."))
}
func (conf *FakeConfig) IsExist(key string) bool {
return conf.find(key) != nil
}
func (conf *FakeConfig) Get(key string) interface{} {
return conf.find(key)
}
func (conf *FakeConfig) GetBool(key string) bool {
return cast.ToBool(conf.find(key))
}
func (conf *FakeConfig) GetInt(key string) int {
return cast.ToInt(conf.find(key))
}
func (conf *FakeConfig) GetFloat64(key string) float64 {
return cast.ToFloat64(conf.find(key))
}
func (conf *FakeConfig) GetTime(key string) time.Time {
return cast.ToTime(conf.find(key))
}
func (conf *FakeConfig) GetString(key string) string {
return cast.ToString(conf.find(key))
}
func (conf *FakeConfig) GetIntSlice(key string) []int {
return cast.ToIntSlice(conf.find(key))
}
func (conf *FakeConfig) GetStringSlice(key string) []string {
return cast.ToStringSlice(conf.find(key))
}
// GetStringMap get map which key is string, value is interface
func (conf *FakeConfig) GetStringMap(key string) map[string]interface{} {
return cast.ToStringMap(conf.find(key))
}
// GetStringMapString get map which key is string, value is string
func (conf *FakeConfig) GetStringMapString(key string) map[string]string {
return cast.ToStringMapString(conf.find(key))
}
// GetStringMapStringSlice get map which key is string, value is string slice
func (conf *FakeConfig) GetStringMapStringSlice(key string) map[string][]string {
return cast.ToStringMapStringSlice(conf.find(key))
}
func (conf *FakeConfig) Load(key string, val interface{}) error {
return mapstructure.Decode(conf.find(key), val)
}
func NewFakeConfig(params ...interface{}) (interface{}, error) {
name := params[0].(string)
bf := params[1].([]byte)
c := map[string]interface{}{}
if err := yaml.Unmarshal(bf, &c); err != nil {
return nil, err
}
return &FakeConfig{
confMap: map[string]interface{}{name: c},
}, nil
}