added more int parsing and tests
This commit is contained in:
81
env/uint.go
vendored
Normal file
81
env/uint.go
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Get an environment variable as a uint, specifying a default value if its
|
||||
// not set or can't be parsed properly into a uint
|
||||
func UInt(key string, defaultValue uint) uint {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.ParseUint(val, 10, 0)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return uint(intVal)
|
||||
}
|
||||
|
||||
// Get an environment variable as a uint8, specifying a default value if its
|
||||
// not set or can't be parsed properly into a uint8
|
||||
func UInt8(key string, defaultValue uint8) uint8 {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.ParseUint(val, 10, 8)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return uint8(intVal)
|
||||
}
|
||||
|
||||
// Get an environment variable as a uint16, specifying a default value if its
|
||||
// not set or can't be parsed properly into a uint16
|
||||
func UInt16(key string, defaultValue uint16) uint16 {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.ParseUint(val, 10, 16)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return uint16(intVal)
|
||||
}
|
||||
|
||||
// Get an environment variable as a uint32, specifying a default value if its
|
||||
// not set or can't be parsed properly into a uint32
|
||||
func UInt32(key string, defaultValue uint32) uint32 {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.ParseUint(val, 10, 32)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return uint32(intVal)
|
||||
}
|
||||
|
||||
// Get an environment variable as a uint64, specifying a default value if its
|
||||
// not set or can't be parsed properly into a uint64
|
||||
func UInt64(key string, defaultValue uint64) uint64 {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.ParseUint(val, 10, 64)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return intVal
|
||||
}
|
||||
Reference in New Issue
Block a user