-
Notifications
You must be signed in to change notification settings - Fork 814
/
config.go
204 lines (171 loc) · 5.26 KB
/
config.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2021 CloudWeGo Authors
*
* 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 configutil
import "time"
// Config provides access to configurations.
type Config interface {
Get(key string) (val interface{}, ok bool)
}
// Container manages multiple config sources with priorities.
type Container interface {
Config
AddPriorSource(src Config) error
}
// DefaultConfig provides access to configurations.
type DefaultConfig interface {
Get(key string, def interface{}) interface{}
}
// RichTypeConfig provides typed get functions.
type RichTypeConfig interface {
GetBool(key string) (val, ok bool)
GetInt(key string) (val int, ok bool)
GetString(key string) (val string, ok bool)
GetInt64(key string) (val int64, ok bool)
GetFloat(key string) (val float64, ok bool)
GetDuration(key string) (val time.Duration, ok bool)
}
// RichTypeDefaultConfig provides typed get functions with default value support.
type RichTypeDefaultConfig interface {
GetBool(key string, def bool) bool
GetInt(key string, def int) int
GetString(key, def string) string
GetInt64(key string, def int64) int64
GetFloat(key string, def float64) float64
GetDuration(key string, def time.Duration) time.Duration
}
// dummyConfig is a dummy config source.
type dummyConfig struct{}
func (dc *dummyConfig) Get(key string) (val interface{}, ok bool) { return }
// NewDummyConfig creates a dummy config.
func NewDummyConfig() Config {
return &dummyConfig{}
}
// configs implements Config and Container interfaces.
type configs struct {
sources []Config
}
// NewConfigContainer creates an empty Container.
func NewConfigContainer() Container {
return &configs{}
}
func (cc *configs) Get(key string) (val interface{}, ok bool) {
for i := len(cc.sources); i > 0; i-- {
src := cc.sources[i-1]
if val, ok = src.Get(key); ok {
break
}
}
return
}
func (cc *configs) AddPriorSource(src Config) error {
cc.sources = append(cc.sources, src)
return nil
}
// defaultConfig wraps a Config to implement DefaultConfig.
type defaultConfig struct {
Config
}
// NewDefaultConfig creates a DefaultConfig with the given Config.
func NewDefaultConfig(c Config) DefaultConfig {
return &defaultConfig{c}
}
func (dc *defaultConfig) Get(key string, def interface{}) interface{} {
if val, exist := dc.Config.Get(key); exist {
return val
}
return def
}
// richTypeConfig wraps a Config to implement RichTypeConfig.
type richTypeConfig struct {
DefaultConfig
}
// NewRichTypeConfig creates a RichTypeConfig with the given Config.
func NewRichTypeConfig(c Config) RichTypeConfig {
return &richTypeConfig{NewDefaultConfig(c)}
}
func (rd *richTypeConfig) GetBool(key string) (val, ok bool) {
val, ok = rd.DefaultConfig.Get(key, nil).(bool)
return
}
func (rd *richTypeConfig) GetInt(key string) (val int, ok bool) {
val, ok = rd.DefaultConfig.Get(key, nil).(int)
return
}
func (rd *richTypeConfig) GetString(key string) (val string, ok bool) {
val, ok = rd.DefaultConfig.Get(key, nil).(string)
return
}
func (rd *richTypeConfig) GetInt64(key string) (val int64, ok bool) {
if val, ok = rd.DefaultConfig.Get(key, nil).(int64); ok {
return
}
if i32, ok := rd.DefaultConfig.Get(key, nil).(int); ok {
return int64(i32), ok
}
return
}
func (rd *richTypeConfig) GetFloat(key string) (val float64, ok bool) {
val, ok = rd.DefaultConfig.Get(key, nil).(float64)
return
}
func (rd *richTypeConfig) GetDuration(key string) (val time.Duration, ok bool) {
val, ok = rd.DefaultConfig.Get(key, nil).(time.Duration)
return
}
// richTypeDefaultConfig wraps a RichTypeConfig to implement RichTypeDefaultConfig.
type richTypeDefaultConfig struct {
RichTypeConfig
}
// NewRichTypeDefaultConfig creates a RichTypeDefaultConfig with the given RichTypeConfig.
func NewRichTypeDefaultConfig(rtc RichTypeConfig) RichTypeDefaultConfig {
return &richTypeDefaultConfig{rtc}
}
func (rd *richTypeDefaultConfig) GetBool(key string, def bool) bool {
if val, exist := rd.RichTypeConfig.GetBool(key); exist {
return val
}
return def
}
func (rd *richTypeDefaultConfig) GetInt(key string, def int) int {
if val, exist := rd.RichTypeConfig.GetInt(key); exist {
return val
}
return def
}
func (rd *richTypeDefaultConfig) GetString(key, def string) string {
if val, exist := rd.RichTypeConfig.GetString(key); exist {
return val
}
return def
}
func (rd *richTypeDefaultConfig) GetInt64(key string, def int64) int64 {
if val, exist := rd.RichTypeConfig.GetInt64(key); exist {
return val
}
return def
}
func (rd *richTypeDefaultConfig) GetFloat(key string, def float64) float64 {
if val, exist := rd.RichTypeConfig.GetFloat(key); exist {
return val
}
return def
}
func (rd *richTypeDefaultConfig) GetDuration(key string, def time.Duration) time.Duration {
if val, exist := rd.RichTypeConfig.GetDuration(key); exist {
return val
}
return def
}