-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
421 lines (379 loc) · 9.54 KB
/
Copy pathget.go
File metadata and controls
421 lines (379 loc) · 9.54 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2026 The Gopherly 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 synthra
import (
"fmt"
"reflect"
"time"
"github.com/spf13/cast"
)
// Get returns the value associated with the given key as type T.
// If c is nil, it returns [ErrNilConfig].
// If the key is missing or empty, or the value cannot be converted to T,
// it returns an error.
//
// Example:
//
// port, err := synthra.Get[int](cfg, "server.port")
// if err != nil {
// return fmt.Errorf("server.port: %w", err)
// }
//
// timeout, err := synthra.Get[time.Duration](cfg, "timeout")
// if err != nil {
// return err
// }
func Get[T any](c *Synthra, key string) (T, error) {
zero := getZeroValue[T]()
val, err := c.requireValue(key)
if err != nil {
return zero, err
}
if result, ok := val.(T); ok {
return result, nil
}
result, ok := convertToType[T](val)
if ok {
return result, nil
}
return zero, NewConfigError(OpGet, key, fmt.Errorf("cannot convert to %T", zero))
}
// GetOr returns the value associated with the given key as type T.
// If the key is not found or cannot be converted to type T, it returns the
// provided default value.
// The type T is inferred from the default value.
//
// Example:
//
// port := synthra.GetOr(cfg, "server.port", 8080) // type inferred as int
// host := synthra.GetOr(cfg, "server.host", "localhost") // type inferred as string
// timeout := synthra.GetOr(cfg, "timeout", 30*time.Second) // type inferred as time.Duration
func GetOr[T any](c *Synthra, key string, defaultVal T) T {
if c == nil {
return defaultVal
}
val := c.Configuration().Get(key)
if val == nil {
return defaultVal
}
if result, ok := val.(T); ok {
return result
}
result, ok := convertToType[T](val)
if ok {
return result
}
return defaultVal
}
// --- Typed accessors on *Synthra (delegate to Snapshot) ---
// String returns the value at key as a string.
func (c *Synthra) String(key string) (string, error) {
v, err := c.requireValue(key)
if err != nil {
return "", err
}
s, err := cast.ToStringE(v)
if err != nil {
return "", NewConfigError(OpGet, key, err)
}
return s, nil
}
// Int returns the value at key as an int.
func (c *Synthra) Int(key string) (int, error) {
v, err := c.requireValue(key)
if err != nil {
return 0, err
}
i, err := cast.ToIntE(v)
if err != nil {
return 0, NewConfigError(OpGet, key, err)
}
return i, nil
}
// Int64 returns the value at key as an int64.
func (c *Synthra) Int64(key string) (int64, error) {
v, err := c.requireValue(key)
if err != nil {
return 0, err
}
i, err := cast.ToInt64E(v)
if err != nil {
return 0, NewConfigError(OpGet, key, err)
}
return i, nil
}
// Float64 returns the value at key as a float64.
func (c *Synthra) Float64(key string) (float64, error) {
v, err := c.requireValue(key)
if err != nil {
return 0, err
}
f, err := cast.ToFloat64E(v)
if err != nil {
return 0, NewConfigError(OpGet, key, err)
}
return f, nil
}
// Bool returns the value at key as a bool.
func (c *Synthra) Bool(key string) (bool, error) {
v, err := c.requireValue(key)
if err != nil {
return false, err
}
b, err := cast.ToBoolE(v)
if err != nil {
return false, NewConfigError(OpGet, key, err)
}
return b, nil
}
// Duration returns the value at key as a [time.Duration].
func (c *Synthra) Duration(key string) (time.Duration, error) {
v, err := c.requireValue(key)
if err != nil {
return 0, err
}
d, err := cast.ToDurationE(v)
if err != nil {
return 0, NewConfigError(OpGet, key, err)
}
return d, nil
}
// Time returns the value at key as a [time.Time].
func (c *Synthra) Time(key string) (time.Time, error) {
v, err := c.requireValue(key)
if err != nil {
return time.Time{}, err
}
tm, err := cast.ToTimeE(v)
if err != nil {
return time.Time{}, NewConfigError(OpGet, key, err)
}
return tm, nil
}
// StringSlice returns the value at key as a []string.
func (c *Synthra) StringSlice(key string) ([]string, error) {
v, err := c.requireValue(key)
if err != nil {
return nil, err
}
s, err := cast.ToStringSliceE(v)
if err != nil {
return nil, NewConfigError(OpGet, key, err)
}
return s, nil
}
// IntSlice returns the value at key as a []int.
func (c *Synthra) IntSlice(key string) ([]int, error) {
v, err := c.requireValue(key)
if err != nil {
return nil, err
}
s, err := cast.ToIntSliceE(v)
if err != nil {
return nil, NewConfigError(OpGet, key, err)
}
return s, nil
}
// StringMap returns the value at key as a map[string]any.
func (c *Synthra) StringMap(key string) (map[string]any, error) {
v, err := c.requireValue(key)
if err != nil {
return nil, err
}
m, err := cast.ToStringMapE(v)
if err != nil {
return nil, NewConfigError(OpGet, key, err)
}
return m, nil
}
// --- Or-variant accessors ---
// StringOr returns the value at key as a string, or the default if not found.
func (c *Synthra) StringOr(key, defaultVal string) string {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToString(val)
}
// IntOr returns the value at key as an int, or the default if not found.
func (c *Synthra) IntOr(key string, defaultVal int) int {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToInt(val)
}
// Int64Or returns the value at key as an int64, or the default if not found.
func (c *Synthra) Int64Or(key string, defaultVal int64) int64 {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToInt64(val)
}
// Float64Or returns the value at key as a float64, or the default if not found.
func (c *Synthra) Float64Or(key string, defaultVal float64) float64 {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToFloat64(val)
}
// BoolOr returns the value at key as a bool, or the default if not found.
func (c *Synthra) BoolOr(key string, defaultVal bool) bool {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToBool(val)
}
// DurationOr returns the value at key as a [time.Duration], or def if not found.
func (c *Synthra) DurationOr(key string, defaultVal time.Duration) time.Duration {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToDuration(val)
}
// TimeOr returns the value at key as a [time.Time], or the default if not found.
func (c *Synthra) TimeOr(key string, defaultVal time.Time) time.Time {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToTime(val)
}
// StringSliceOr returns the value at key as a []string, or def if not found.
func (c *Synthra) StringSliceOr(key string, defaultVal []string) []string {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToStringSlice(val)
}
// IntSliceOr returns the value at key as a []int, or the default if not found.
func (c *Synthra) IntSliceOr(key string, defaultVal []int) []int {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToIntSlice(val)
}
// StringMapOr returns the value at key as a map[string]any, or def if not found.
func (c *Synthra) StringMapOr(key string, defaultVal map[string]any) map[string]any {
if c == nil {
return defaultVal
}
val := c.Get(key)
if val == nil {
return defaultVal
}
return cast.ToStringMap(val)
}
// --- Helpers ---
// getZeroValue returns a proper zero value for type T.
// For slices and maps, it returns empty initialized values instead of nil.
func getZeroValue[T any]() T {
var zero T
v := reflect.ValueOf(&zero).Elem()
switch v.Kind() {
case reflect.Slice:
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
case reflect.Map:
v.Set(reflect.MakeMap(v.Type()))
}
return zero
}
// convertToType attempts to convert a value to type T using the cast library.
func convertToType[T any](val any) (T, bool) {
var zero T
var result any
switch any(zero).(type) {
case string:
result = cast.ToString(val)
case int:
result = cast.ToInt(val)
case int64:
result = cast.ToInt64(val)
case int32:
result = cast.ToInt32(val)
case int16:
result = cast.ToInt16(val)
case int8:
result = cast.ToInt8(val)
case uint:
result = cast.ToUint(val)
case uint64:
result = cast.ToUint64(val)
case uint32:
result = cast.ToUint32(val)
case uint16:
result = cast.ToUint16(val)
case uint8:
result = cast.ToUint8(val)
case float64:
result = cast.ToFloat64(val)
case float32:
result = cast.ToFloat32(val)
case bool:
result = cast.ToBool(val)
case []string:
result = cast.ToStringSlice(val)
case []int:
result = cast.ToIntSlice(val)
case map[string]any:
result = cast.ToStringMap(val)
case map[string]string:
result = cast.ToStringMapString(val)
case map[string][]string:
result = cast.ToStringMapStringSlice(val)
case time.Duration:
result = cast.ToDuration(val)
case time.Time:
result = cast.ToTime(val)
default:
return zero, false
}
if typedResult, ok := result.(T); ok {
return typedResult, true
}
return zero, false
}