Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions envx_uint8.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 33 in envx_uint8.go

View check run for this annotation

Codecov / codecov/patch

envx_uint8.go#L24-L33

Added lines #L24 - L33 were not covered by tests
}

// 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))

Check warning on line 40 in envx_uint8.go

View check run for this annotation

Codecov / codecov/patch

envx_uint8.go#L37-L40

Added lines #L37 - L40 were not covered by tests
}
res, err := strconv.ParseUint(aux, 10, 8)
if err != nil {
panic(fmt.Sprintf("invalid uint8 value for %s: %s", key, aux))

Check warning on line 44 in envx_uint8.go

View check run for this annotation

Codecov / codecov/patch

envx_uint8.go#L42-L44

Added lines #L42 - L44 were not covered by tests
}
return uint8(res)

Check warning on line 46 in envx_uint8.go

View check run for this annotation

Codecov / codecov/patch

envx_uint8.go#L46

Added line #L46 was not covered by tests
}

// 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))

Check warning on line 58 in envx_uint8.go

View check run for this annotation

Codecov / codecov/patch

envx_uint8.go#L51-L58

Added lines #L51 - L58 were not covered by tests
}
return uint8(res)

Check warning on line 60 in envx_uint8.go

View check run for this annotation

Codecov / codecov/patch

envx_uint8.go#L60

Added line #L60 was not covered by tests
}
43 changes: 43 additions & 0 deletions envx_uint8_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading