-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
116 lines (88 loc) · 2.39 KB
/
wrapper.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package bviper
import (
"time"
"github.com/go-masonry/mortar/interfaces/cfg"
"github.com/spf13/viper"
)
type viperWrapper struct {
instance *viper.Viper
}
func (v *viperWrapper) Get(key string) cfg.Value {
return &valueWrapper{
key: key,
viperWrapper: v,
}
}
func (v *viperWrapper) Set(key string, value interface{}) {
v.instance.Set(key, value)
}
func (v *viperWrapper) Map() map[string]interface{} {
return v.instance.AllSettings()
}
func (v *viperWrapper) Implementation() interface{} {
return v.instance
}
type valueWrapper struct {
key string
*viperWrapper
}
func (v *valueWrapper) IsSet() bool {
return v.instance.IsSet(v.key)
}
func (v *valueWrapper) Raw() interface{} {
return v.instance.Get(v.key)
}
func (v *valueWrapper) Bool() bool {
return v.instance.GetBool(v.key)
}
func (v *valueWrapper) Int() int {
return v.instance.GetInt(v.key)
}
func (v *valueWrapper) Int32() int32 {
return v.instance.GetInt32(v.key)
}
func (v *valueWrapper) Int64() int64 {
return v.instance.GetInt64(v.key)
}
func (v *valueWrapper) Uint() uint {
return v.instance.GetUint(v.key)
}
func (v *valueWrapper) Uint32() uint32 {
return v.instance.GetUint32(v.key)
}
func (v *valueWrapper) Uint64() uint64 {
return v.instance.GetUint64(v.key)
}
func (v *valueWrapper) Float64() float64 {
return v.instance.GetFloat64(v.key)
}
func (v *valueWrapper) Time() time.Time {
return v.instance.GetTime(v.key)
}
func (v *valueWrapper) Duration() time.Duration {
return v.instance.GetDuration(v.key)
}
func (v *valueWrapper) String() string {
return v.instance.GetString(v.key)
}
func (v *valueWrapper) IntSlice() []int {
return v.instance.GetIntSlice(v.key)
}
func (v *valueWrapper) StringSlice() []string {
return v.instance.GetStringSlice(v.key)
}
func (v *valueWrapper) StringMap() map[string]interface{} {
return v.instance.GetStringMap(v.key)
}
func (v *valueWrapper) StringMapString() map[string]string {
return v.instance.GetStringMapString(v.key)
}
func (v *valueWrapper) StringMapStringSlice() map[string][]string {
return v.instance.GetStringMapStringSlice(v.key)
}
// Unmarshal uses default decoder options, if you need some special behavior than it's best to get cfg.Implementation() and use it from there
func (v *valueWrapper) Unmarshal(result interface{}) error {
return v.instance.UnmarshalKey(v.key, result)
}
var _ cfg.Config = (*viperWrapper)(nil)
var _ cfg.Value = (*valueWrapper)(nil)