@@ -229,6 +229,57 @@ func int8SliceOrDefault(key string, defaultVal []int8, sep string) []int8 {
229229 return val
230230}
231231
232+ func intOrDefaultGen [T int | int8 | int16 | int32 | int64 ](key string , defaultVal T ) T {
233+ env := stringOrDefault (key , "" )
234+ if env == "" {
235+ return defaultVal
236+ }
237+
238+ const (
239+ base = 10
240+ )
241+
242+ var (
243+ bitsize int
244+ castFn func (val int64 ) T
245+ )
246+
247+ switch any (defaultVal ).(type ) {
248+ case int :
249+ bitsize = 0
250+ castFn = func (val int64 ) T {
251+ return any (int (val )).(T )
252+ }
253+ case int8 :
254+ bitsize = 8
255+ castFn = func (val int64 ) T {
256+ return any (int8 (val )).(T )
257+ }
258+ case int16 :
259+ bitsize = 16
260+ castFn = func (val int64 ) T {
261+ return any (int16 (val )).(T )
262+ }
263+ case int32 :
264+ bitsize = 32
265+ castFn = func (val int64 ) T {
266+ return any (int32 (val )).(T )
267+ }
268+ case int64 :
269+ bitsize = 64
270+ castFn = func (val int64 ) T {
271+ return any (val ).(T )
272+ }
273+ }
274+
275+ val , err := strconv .ParseInt (env , base , bitsize )
276+ if err != nil {
277+ return defaultVal
278+ }
279+
280+ return castFn (val )
281+ }
282+
232283// int32SliceOrDefault retrieves the int32 slice value of the environment variable named
233284// by the key and separated by sep.
234285// If variable not set or value is empty - defaultVal will be returned.
@@ -365,94 +416,6 @@ func durationSliceOrDefault(key string, defaultVal []time.Duration, separator st
365416 return val
366417}
367418
368- // int64OrDefault retrieves the int64 value of the environment variable named
369- // by the key.
370- // If variable not set or value is empty - defaultVal will be returned.
371- func int64OrDefault (key string , defaultVal int64 ) int64 {
372- env := stringOrDefault (key , "" )
373- if env == "" {
374- return defaultVal
375- }
376-
377- const (
378- base = 10
379- bitsize = 64
380- )
381-
382- val , err := strconv .ParseInt (env , base , bitsize )
383- if err != nil {
384- return defaultVal
385- }
386-
387- return val
388- }
389-
390- // int8OrDefault retrieves the int8 value of the environment variable named
391- // by the key.
392- // If variable not set or value is empty - defaultVal will be returned.
393- func int8OrDefault (key string , defaultVal int8 ) int8 {
394- env := stringOrDefault (key , "" )
395- if env == "" {
396- return defaultVal
397- }
398-
399- const (
400- base = 10
401- bitsize = 8
402- )
403-
404- val , err := strconv .ParseInt (env , base , bitsize )
405- if err != nil {
406- return defaultVal
407- }
408-
409- return int8 (val )
410- }
411-
412- // int16OrDefault retrieves the int16 value of the environment variable named
413- // by the key.
414- // If variable not set or value is empty - defaultVal will be returned.
415- func int16OrDefault (key string , defaultVal int16 ) int16 {
416- env := stringOrDefault (key , "" )
417- if env == "" {
418- return defaultVal
419- }
420-
421- const (
422- base = 10
423- bitsize = 16
424- )
425-
426- val , err := strconv .ParseInt (env , base , bitsize )
427- if err != nil {
428- return defaultVal
429- }
430-
431- return int16 (val )
432- }
433-
434- // int32OrDefault retrieves the int32 value of the environment variable named
435- // by the key.
436- // If variable not set or value is empty - defaultVal will be returned.
437- func int32OrDefault (key string , defaultVal int32 ) int32 {
438- env := stringOrDefault (key , "" )
439- if env == "" {
440- return defaultVal
441- }
442-
443- const (
444- base = 10
445- bitsize = 32
446- )
447-
448- val , err := strconv .ParseInt (env , base , bitsize )
449- if err != nil {
450- return defaultVal
451- }
452-
453- return int32 (val )
454- }
455-
456419// float32OrDefault retrieves the float32 value of the environment variable named
457420// by the key.
458421// If variable not set or value is empty - defaultVal will be returned.
@@ -946,7 +909,7 @@ func complex128OrDefault(key string, defaultVal complex128) complex128 {
946909
947910// complex128SliceOrDefault retrieves the complex128 slice value of the environment variable named
948911// by the key and separated by sep.
949- // If variable not set or value is empty - defaultVal will be returned.
912+ // If the variable is not set or the value is empty - defaultVal will be returned.
950913func complex128SliceOrDefault (key string , defaultVal []complex128 , sep string ) []complex128 {
951914 valraw := stringSliceOrDefault (key , nil , sep )
952915 if valraw == nil {
0 commit comments