-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
133 lines (102 loc) · 2.82 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
package activerecord
import (
"context"
"fmt"
"time"
)
type DefaultConfig struct {
cfg map[string]interface{}
created time.Time
}
func NewDefaultConfig() *DefaultConfig {
return &DefaultConfig{
cfg: make(map[string]interface{}),
}
}
func NewDefaultConfigFromMap(cfg map[string]interface{}) *DefaultConfig {
return &DefaultConfig{
cfg: cfg,
created: time.Now(),
}
}
func (dc *DefaultConfig) GetLastUpdateTime() time.Time {
return dc.created
}
func (dc *DefaultConfig) GetBool(ctx context.Context, confPath string, dfl ...bool) bool {
if ret, ok := dc.GetBoolIfExists(ctx, confPath); ok {
return ret
}
if len(dfl) != 0 {
return dfl[0]
}
return false
}
func (dc *DefaultConfig) GetBoolIfExists(ctx context.Context, confPath string) (value bool, ok bool) {
if param, ex := dc.cfg[confPath]; ex {
if ret, ok := param.(bool); ok {
return ret, true
}
Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want bool", confPath, param))
}
return false, false
}
func (dc *DefaultConfig) GetInt(ctx context.Context, confPath string, dfl ...int) int {
if ret, ok := dc.GetIntIfExists(ctx, confPath); ok {
return ret
}
if len(dfl) != 0 {
return dfl[0]
}
return 0
}
func (dc *DefaultConfig) GetIntIfExists(ctx context.Context, confPath string) (int, bool) {
if param, ex := dc.cfg[confPath]; ex {
if ret, ok := param.(int); ok {
return ret, true
}
Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want int", confPath, param))
}
return 0, false
}
func (dc *DefaultConfig) GetDuration(ctx context.Context, confPath string, dfl ...time.Duration) time.Duration {
if ret, ok := dc.GetDurationIfExists(ctx, confPath); ok {
return ret
}
if len(dfl) != 0 {
return dfl[0]
}
return 0
}
func (dc *DefaultConfig) GetDurationIfExists(ctx context.Context, confPath string) (time.Duration, bool) {
if param, ex := dc.cfg[confPath]; ex {
if ret, ok := param.(time.Duration); ok {
return ret, true
}
Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want time.Duration", confPath, param))
}
return 0, false
}
func (dc *DefaultConfig) GetString(ctx context.Context, confPath string, dfl ...string) string {
if ret, ok := dc.GetStringIfExists(ctx, confPath); ok {
return ret
}
if len(dfl) != 0 {
return dfl[0]
}
return ""
}
func (dc *DefaultConfig) GetStringIfExists(ctx context.Context, confPath string) (string, bool) {
if param, ex := dc.cfg[confPath]; ex {
if ret, ok := param.(string); ok {
return ret, true
}
Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want string", confPath, param))
}
return "", false
}
func (dc *DefaultConfig) GetStrings(ctx context.Context, confPath string, dfl []string) []string {
return []string{}
}
func (dc *DefaultConfig) GetStruct(ctx context.Context, confPath string, valuePtr interface{}) (bool, error) {
return false, nil
}