Skip to content

Commit

Permalink
feat: add GetAsUint64 function
Browse files Browse the repository at this point in the history
  • Loading branch information
DaruZero committed Apr 27, 2023
1 parent c7d0170 commit d2fbc73
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
22 changes: 22 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ func GetAsInt(key string, required bool, fallback int) (int, error) {
return i, nil
}

// GetAsUint64 returns the value of the environment variable as an uint64. If the environment variable is not set and not required, the fallback value is returned.
func GetAsUint64(key string, required bool, fallback uint64) (uint64, error) {
value, set := os.LookupEnv(key)

// Check if the environment variable is set
if !set {
// If not required, return the fallback value
if !required {
return fallback, nil
}
// If required, return an error
return 0, fmt.Errorf("environment variable %s is required but not set", key)
}

i, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, fmt.Errorf("environment variable %s is not an integer", key)
}

return i, nil
}

// GetAsBool returns the value of the environment variable as a bool. If the environment variable is not set and not required, the fallback value is returned.
func GetAsBool(key string, required bool, fallback bool) (bool, error) {
value, set := os.LookupEnv(key)
Expand Down
74 changes: 74 additions & 0 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,80 @@ func TestGetAsInt(t *testing.T) {
}
}

// TestGetAsUint64 tests the GetAsUint64 function
func TestGetAsUint64(t *testing.T) {
expected := uint64(123)
fallback := uint64(456)
setupEnv(strconv.FormatUint(expected, 10))
defer teardownEnv()

type args struct {
key string
required bool
fallback uint64
}
tests := []struct {
name string
args args
want uint64
wantErr bool
}{
{
name: "Case 1: Variable exists, return value",
args: args{
key: "EXISTING_VAR",
required: false,
fallback: fallback,
},
want: expected,
wantErr: false,
},
{
name: "Case 2: Variable is empty, return error",
args: args{
key: "EMPTY_VAR",
required: false,
fallback: fallback,
},
want: 0,
wantErr: true,
},
{
name: "Case 3: Variable does not exist and is not required, return fallback value",
args: args{
key: "NONEXISTENT_VAR",
required: false,
fallback: fallback,
},
want: fallback,
wantErr: false,
},
{
name: "Case 4: Variable does not exist and is required, return error",
args: args{
key: "NONEXISTENT_VAR",
required: true,
fallback: fallback,
},
want: 0,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAsUint64(tt.args.key, tt.args.required, tt.args.fallback)
if (err != nil) != tt.wantErr {
t.Errorf("GetAsUint64() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetAsUint64() = %v, want %v", got, tt.want)
}
})
}
}

// TestGetAsBool tests the GetAsBool function
func TestGetAsBool(t *testing.T) {
setupEnv(strconv.FormatBool(true))
Expand Down

0 comments on commit d2fbc73

Please sign in to comment.