-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
151 lines (121 loc) · 3.1 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
package config
import (
"math/big"
"time"
)
type Option func(*Config)
type ConfigurationProvider interface {
LoadConfig(filename string) Configuration
ParseString(cfgStr string) Configuration
}
type Configuration interface {
GetBoolean(path string, defaultVal ...bool) bool
GetByteSize(path string) *big.Int
GetInt32(path string, defaultVal ...int32) int32
GetInt64(path string, defaultVal ...int64) int64
GetString(path string, defaultVal ...string) string
GetFloat32(path string, defaultVal ...float32) float32
GetFloat64(path string, defaultVal ...float64) float64
GetTimeDuration(path string, defaultVal ...time.Duration) time.Duration
GetTimeDurationInfiniteNotAllowed(path string, defaultVal ...time.Duration) time.Duration
GetBooleanList(path string) []bool
GetFloat32List(path string) []float32
GetFloat64List(path string) []float64
GetInt32List(path string) []int32
GetInt64List(path string) []int64
GetByteList(path string) []byte
GetStringList(path string) []string
GetConfig(path string) Configuration
WithFallback(fallback Configuration) Configuration
HasPath(path string) bool
Keys() []string
IsEmpty() bool
IsObject(path string) bool
IsArray(path string) bool
String() string
}
type Config struct {
ConfigFile string
ConfigString string
Configuration
configProvider ConfigurationProvider
}
func NewConfig(opts ...Option) *Config {
conf := &Config{}
conf.init(opts...)
return conf
}
func (p *Config) init(opts ...Option) {
for i := 0; i < len(opts); i++ {
opts[i](p)
}
if p.configProvider == nil {
p.configProvider = &HOCONConfigProvider{}
}
conf := p.Configuration
if conf == nil {
conf = p.configProvider.ParseString("")
}
var confString, confFile Configuration
if len(p.ConfigFile) > 0 {
confFile = p.configProvider.LoadConfig(p.ConfigFile)
conf = conf.WithFallback(confFile)
}
if len(p.ConfigString) > 0 {
confString = p.configProvider.ParseString(p.ConfigString)
conf = conf.WithFallback(confString)
}
p.Configuration = conf
}
func (p *Config) String() string {
if p == nil || p.Configuration == nil {
return ""
}
return p.Configuration.String()
}
func (p *Config) WithFallback(fallback Configuration) Configuration {
if fallback == nil {
return p
}
if p.Configuration == nil {
return p
}
switch v := fallback.(type) {
case *Config:
{
return p.WithFallback(v.Configuration)
}
default:
p.Configuration = p.Configuration.WithFallback(v)
}
return p
}
func ConfigFile(fn string) Option {
return func(o *Config) {
o.ConfigFile = fn
}
}
func ConfigString(str string) Option {
return func(o *Config) {
o.ConfigString = str
}
}
func WithConfig(conf Configuration) Option {
return func(o *Config) {
o.Configuration = conf
}
}
func ConfigProvider(provider ConfigurationProvider) Option {
return func(o *Config) {
o.configProvider = provider
}
}
func ConfigProviderByName(driverName string) Option {
return func(o *Config) {
provider, err := NewConfigurationProvider(driverName)
if err != nil {
panic("create configration provider of " + driverName + " failure. error:" + err.Error())
}
o.configProvider = provider
}
}