diff --git a/envx_uint8.go b/envx_uint8.go new file mode 100644 index 0000000..793295c --- /dev/null +++ b/envx_uint8.go @@ -0,0 +1,61 @@ +package envx + +import ( + "fmt" + "os" + "strconv" +) + +// GetUint8 returns the uint8 value of the environment variable or an error if not set or invalid. +func GetUint8(key string) (uint8, error) { + aux := os.Getenv(key) + if aux == "" { + return 0, NewMissingEnvVarError(key) + } + res, err := strconv.ParseUint(aux, 10, 8) + if err != nil { + return 0, fmt.Errorf("invalid uint8 value for %s: %s", key, aux) + } + return uint8(res), nil +} + +// GetUint8Or returns the uint8 value of the environment variable or the default if not set. +// If the variable is set but cannot be parsed as an uint8, it returns an error. +func GetUint8Or(key string, defaultValue uint8) (uint8, error) { + aux := os.Getenv(key) + if aux == "" { + return defaultValue, nil + } + res, err := strconv.ParseUint(aux, 10, 8) + if err != nil { + return defaultValue, fmt.Errorf("invalid uint8 value for %s: %s", key, aux) + } + return uint8(res), nil +} + +// MustGetUint8 returns the uint8 value of an environment variable, panicking if it's missing. +func MustGetUint8(key string) uint8 { + aux := os.Getenv(key) + if aux == "" { + panic(NewMissingEnvVarError(key)) + } + res, err := strconv.ParseUint(aux, 10, 8) + if err != nil { + panic(fmt.Sprintf("invalid uint8 value for %s: %s", key, aux)) + } + return uint8(res) +} + +// MustGetUint8Or returns the uint8 value of an environment variable or a default if it's missing. +// It panics if the variable is set but has an invalid uint8 format. +func MustGetUint8Or(key string, defaultValue uint8) uint8 { + aux := os.Getenv(key) + if aux == "" { + return defaultValue + } + res, err := strconv.ParseUint(aux, 10, 8) + if err != nil { + panic(fmt.Sprintf("invalid uint8 value for %s: %s", key, aux)) + } + return uint8(res) +} diff --git a/envx_uint8_test.go b/envx_uint8_test.go new file mode 100644 index 0000000..31e2fa7 --- /dev/null +++ b/envx_uint8_test.go @@ -0,0 +1,43 @@ +package envx + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetUint8(t *testing.T) { + tests := []struct { + key string + value string + expected uint8 + setEnv bool + hasError bool + }{ + {"EXISTING_UINT8", "123", 123, true, false}, + {"MAX_UINT8", "255", 255, true, false}, // Maximum uint8 value + {"MIN_UINT8", "0", 0, true, false}, // Minimum uint8 value + {"OVERFLOW_UINT8", "256", 0, true, true}, // Overflow case for uint8 + {"INVALID_UINT8", "notanint", 0, true, true}, // Non-integer string + {"MISSING_UINT8", "", 0, false, true}, // Missing variable case + } + + for _, tc := range tests { + t.Run(tc.key, func(t *testing.T) { + if tc.setEnv { + os.Setenv(tc.key, tc.value) + } + + result, err := GetUint8(tc.key) + if tc.hasError { + assert.Error(t, err, "Expected an error for key: %s", tc.key) + } else { + assert.NoError(t, err, "Did not expect an error for key: %s", tc.key) + assert.Equal(t, tc.expected, result, "GetInt16(%s) = %v; expected %v", tc.key, result, tc.expected) + } + + os.Unsetenv(tc.key) + }) + } +}