-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
executable file
·294 lines (270 loc) · 7.83 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package dsc
import (
"context"
"github.com/viant/scy/cred"
"github.com/viant/scy/cred/secret"
"github.com/viant/toolbox"
"github.com/viant/toolbox/url"
"strings"
"sync"
"sync/atomic"
"time"
)
// BatchSizeKey represents a config batch size parameter
const BatchSizeKey = "batchSize"
// Config represent datastore config.
type Config struct {
URL string
//@deprecated use driver instead
DriverName string `json:"-"`
Driver string
PoolSize int
MaxPoolSize int
//@deprecated use DSN instead
Descriptor string `json:"-"`
DSN string
InitSQL []string
Parameters map[string]interface{}
Credentials string
MaxRequestPerSecond int
cred string
username string
password string
dsnDescriptor string
lock *sync.Mutex
race uint32
initRun bool
CredConfig *cred.Generic `json:"-"`
}
// Get returns value for passed in parameter name or panic - please use Config.Has to check if value is present.
func (c *Config) Get(name string) string {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
return toolbox.AsString(result)
}
return ""
}
// DsnDescriptor return dsn expanded descriptor or error
func (c *Config) DsnDescriptor() (string, error) {
if c.dsnDescriptor == "" {
if err := c.Init(); err != nil {
return "", err
}
}
return c.dsnDescriptor, nil
}
// Get returns value for passed in parameter name or panic - please use Config.Has to check if value is present.
func (c *Config) GetMap(name string) map[string]interface{} {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
if toolbox.IsMap(result) {
return toolbox.AsMap(result)
}
}
return nil
}
// GetInt returns value for passed in parameter name or defaultValue
func (c *Config) GetInt(name string, defaultValue int) int {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
return toolbox.AsInt(result)
}
return defaultValue
}
// GetFloat returns value for passed in parameter name or defaultValue
func (c *Config) GetFloat(name string, defaultValue float64) float64 {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
return toolbox.AsFloat(result)
}
return defaultValue
}
// GetDuration returns value for passed in parameter name or defaultValue
func (c *Config) GetDuration(name string, multiplier time.Duration, defaultValue time.Duration) time.Duration {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
return time.Duration(toolbox.AsInt(result)) * multiplier
}
return defaultValue
}
// GetString returns value for passed in parameter name or defaultValue
func (c *Config) GetString(name string, defaultValue string) string {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
return toolbox.AsString(result)
}
return defaultValue
}
// GetBoolean returns value for passed in parameter name or defaultValue
func (c *Config) GetBoolean(name string, defaultValue bool) bool {
c.lock.Lock()
defer c.lock.Unlock()
if result, ok := c.Parameters[name]; ok {
return toolbox.AsBoolean(result)
}
return defaultValue
}
// HasDateLayout returns true if config has date layout, it checks dateLayout or dateFormat parameter names.
func (c *Config) HasDateLayout() bool {
c.lock.Lock()
defer c.lock.Unlock()
return toolbox.HasTimeLayout(c.Parameters)
}
// GetDateLayout returns date layout
func (c *Config) GetDateLayout() string {
c.lock.Lock()
defer c.lock.Unlock()
return toolbox.GetTimeLayout(c.Parameters)
}
// Has returns true if parameter with passed in name is present, otherwise it returns false.
func (c *Config) Has(name string) bool {
c.lock.Lock()
defer c.lock.Unlock()
if _, ok := c.Parameters[name]; ok {
return true
}
return false
}
func (c *Config) initLock() {
if c.lock == nil {
if atomic.CompareAndSwapUint32(&c.race, 0, 1) {
c.lock = &sync.Mutex{}
} else {
c.initLock()
}
}
}
func (c *Config) loadCredentials(ctx context.Context) error {
if c.Credentials == "" {
return nil
}
secrets := secret.New()
config, err := secrets.GetCredentials(ctx, c.Credentials)
if err != nil {
return err
}
if len(c.Parameters) == 0 {
c.Parameters = make(map[string]interface{})
}
return c.ApplyCredentials(config)
}
func (c *Config) ApplyCredentials(config *cred.Generic) error {
c.username = config.Username
c.password = config.Password
if len(c.Parameters) == 0 {
c.Parameters = make(map[string]interface{})
}
c.Parameters["username"] = c.username
c.CredConfig = config
return nil
}
// Init makes parameter map from encoded parameters if presents, expands descriptor with parameter value using [param_name] matching pattern.
func (c *Config) Init() error {
defer func() { c.initRun = true }()
if c.cred == "" {
c.cred = c.Credentials
}
c.initLock()
if c.URL != "" && c.DriverName == "" {
resource := url.NewResource(c.URL)
if err := resource.Decode(c); err != nil {
return err
}
}
var lock = c.lock
lock.Lock()
defer lock.Unlock()
if err := c.loadCredentials(context.Background()); err != nil {
return err
}
if c.DriverName == "" {
c.DriverName = c.Driver
}
if c.Descriptor == "" {
c.Descriptor = c.DSN
}
c.dsnDescriptor = c.Descriptor
c.dsnDescriptor = strings.Replace(c.dsnDescriptor, "[username]", c.username, 1)
c.dsnDescriptor = strings.Replace(c.dsnDescriptor, "[password]", c.password, 1)
for key, value := range c.Parameters {
textValue, ok := value.(string)
if !ok {
continue
}
macro := "[" + key + "]"
c.dsnDescriptor = strings.Replace(c.dsnDescriptor, macro, textValue, 1)
}
return nil
}
// Clone clones config
func (c *Config) Clone() *Config {
cred := c.cred
if cred == "" {
cred = c.Credentials
}
result := &Config{
DriverName: c.DriverName,
URL: c.URL,
InitSQL: c.InitSQL,
Descriptor: c.Descriptor,
Driver: c.Driver,
DSN: c.DSN,
PoolSize: c.PoolSize,
MaxPoolSize: c.MaxPoolSize,
MaxRequestPerSecond: c.MaxRequestPerSecond,
Parameters: make(map[string]interface{}),
username: c.username,
password: c.password,
dsnDescriptor: c.dsnDescriptor,
lock: &sync.Mutex{},
Credentials: cred,
cred: c.cred,
}
if len(c.Parameters) > 0 {
for k, v := range c.Parameters {
result.Parameters[k] = v
}
}
return result
}
// NewConfig creates new Config, it takes the following parameters
// descriptor - optional datastore connection string with macros that will be looked epxanded from for instance [user]:[password]@[url]
// encodedParameters should be in the following format: <key1>:<value1>, ...,<keyN>:<valueN>
func NewConfig(driverName string, descriptor string, encodedParameters string) *Config {
var parameters = toolbox.MakeMap(encodedParameters, ":", ",")
result := &Config{DriverName: driverName, PoolSize: 1, MaxPoolSize: 2, Descriptor: descriptor, DSN: descriptor, Driver: driverName, Parameters: parameters,
lock: &sync.Mutex{}}
result.Init()
return result
}
// NewConfigWithParameters creates a new config with parameters
func NewConfigWithParameters(driverName string, descriptor string, credential string, parameters map[string]interface{}) (*Config, error) {
result := &Config{
DriverName: driverName,
Descriptor: descriptor,
DSN: descriptor,
Driver: driverName,
Credentials: credential,
Parameters: parameters,
lock: &sync.Mutex{},
}
err := result.Init()
return result, err
}
// NewConfigFromUrl returns new config from url
func NewConfigFromURL(URL string) (*Config, error) {
result := &Config{}
var resource = url.NewResource(URL)
err := resource.Decode(result)
result.lock = &sync.Mutex{}
if err == nil {
err = result.Init()
}
return result, err
}