Skip to content

Commit

Permalink
fix: return fallback and error if not parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DaruZero committed May 17, 2023
1 parent 536aafe commit 6eed3c2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
10 changes: 6 additions & 4 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetAsInt(key string, required bool, fallback int) (int, error) {

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

return i, nil
Expand All @@ -78,7 +78,7 @@ func GetAsUint64(key string, required bool, fallback uint64) (uint64, error) {

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

return i, nil
Expand All @@ -100,7 +100,7 @@ func GetAsFloat64(key string, required bool, fallback float64) (float64, error)

f, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, fmt.Errorf("environment variable %s is not a float", key)
return fallback, fmt.Errorf("environment variable %s is not a float. using fallback value", key)
}

return f, nil
Expand All @@ -122,7 +122,7 @@ func GetAsBool(key string, required bool, fallback bool) (bool, error) {

b, err := strconv.ParseBool(value)
if err != nil {
return false, fmt.Errorf("environment variable %s is not a boolean", key)
return fallback, fmt.Errorf("environment variable %s is not a boolean. using fallback value", key)
}

return b, nil
Expand Down Expand Up @@ -164,6 +164,8 @@ func GetAsType[T any](key string, unmarshalTo *T, required bool, fallback T) err
err := json.Unmarshal([]byte(value), &unmarshalTo)
if err != nil {
// If unmarshaling fails, return an error message
var ptr *T = &fallback
*unmarshalTo = *ptr
return fmt.Errorf("failed to unmarshal environment variable %s: %s", key, err)
}

Expand Down
83 changes: 70 additions & 13 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ func teardown() {
fmt.Println("Unset EMPTY_VAR")
}

func setupEnv(value string) {
err := os.Setenv("EXISTING_VAR", value)
func setupEnv(correctValue, wrongValue string) {
err := os.Setenv("EXISTING_VAR", correctValue)
if err != nil {
panic(err)
}
err = os.Setenv("WRONG_VAR", wrongValue)
if err != nil {
panic(err)
}

fmt.Printf("Set EXISTING_VAR to %v\n", value)
fmt.Printf("Set EXISTING_VAR to %v\n", correctValue)
}

func teardownEnv() {
Expand All @@ -72,8 +76,9 @@ func TestMain(m *testing.M) {
// TestGetAsString tests the GetAsString function
func TestGetAsString(t *testing.T) {
expected := "my-value"
wrong := 123
fallback := "fallback-value"
setupEnv(expected)
setupEnv(expected, strconv.Itoa(wrong))
defer teardownEnv()

type args struct {
Expand Down Expand Up @@ -146,8 +151,9 @@ func TestGetAsString(t *testing.T) {
// TestGetAsInt tests the GetAsInt function
func TestGetAsInt(t *testing.T) {
expected := 123
wrong := "wrong"
fallback := 456
setupEnv(strconv.Itoa(expected))
setupEnv(strconv.Itoa(expected), wrong)
defer teardownEnv()

type args struct {
Expand Down Expand Up @@ -178,7 +184,7 @@ func TestGetAsInt(t *testing.T) {
required: false,
fallback: fallback,
},
want: 0,
want: fallback,
wantErr: true,
},
{
Expand All @@ -201,6 +207,16 @@ func TestGetAsInt(t *testing.T) {
want: 0,
wantErr: true,
},
{
name: "Case 5: Variable is not an integer, return fallback value and error",
args: args{
key: "WRONG_VAR",
required: false,
fallback: fallback,
},
want: fallback,
wantErr: true,
},
}

for _, tt := range tests {
Expand All @@ -220,8 +236,9 @@ func TestGetAsInt(t *testing.T) {
// TestGetAsUint64 tests the GetAsUint64 function
func TestGetAsUint64(t *testing.T) {
expected := uint64(123)
wrong := "wrong"
fallback := uint64(456)
setupEnv(strconv.FormatUint(expected, 10))
setupEnv(strconv.FormatUint(expected, 10), wrong)
defer teardownEnv()

type args struct {
Expand Down Expand Up @@ -252,7 +269,7 @@ func TestGetAsUint64(t *testing.T) {
required: false,
fallback: fallback,
},
want: 0,
want: fallback,
wantErr: true,
},
{
Expand All @@ -275,6 +292,16 @@ func TestGetAsUint64(t *testing.T) {
want: 0,
wantErr: true,
},
{
name: "Case 5: Variable is not an integer, return fallback value and error",
args: args{
key: "WRONG_VAR",
required: false,
fallback: fallback,
},
want: fallback,
wantErr: true,
},
}

for _, tt := range tests {
Expand All @@ -294,8 +321,9 @@ func TestGetAsUint64(t *testing.T) {
// TestGetAsFloat64 tests the GetAsFloat64 function
func TestGetAsFloat64(t *testing.T) {
expected := 123.456
wrong := "wrong"
fallback := 456.789
setupEnv(strconv.FormatFloat(expected, 'f', -1, 64))
setupEnv(strconv.FormatFloat(expected, 'f', -1, 64), wrong)
defer teardownEnv()

type args struct {
Expand Down Expand Up @@ -326,7 +354,7 @@ func TestGetAsFloat64(t *testing.T) {
required: false,
fallback: fallback,
},
want: 0,
want: fallback,
wantErr: true,
},
{
Expand All @@ -349,6 +377,16 @@ func TestGetAsFloat64(t *testing.T) {
want: 0,
wantErr: true,
},
{
name: "Case 5: Variable is not a float, return fallback value and error",
args: args{
key: "WRONG_VAR",
required: false,
fallback: fallback,
},
want: fallback,
wantErr: true,
},
}

for _, tt := range tests {
Expand All @@ -367,7 +405,7 @@ func TestGetAsFloat64(t *testing.T) {

// TestGetAsBool tests the GetAsBool function
func TestGetAsBool(t *testing.T) {
setupEnv(strconv.FormatBool(true))
setupEnv(strconv.FormatBool(true), "wrong")
defer teardownEnv()

type args struct {
Expand Down Expand Up @@ -421,6 +459,16 @@ func TestGetAsBool(t *testing.T) {
want: false,
wantErr: true,
},
{
name: "Case 5: Variable is not a bool, return fallback value and error",
args: args{
key: "WRONG_VAR",
required: false,
fallback: false,
},
want: false,
wantErr: true,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -449,6 +497,11 @@ func TestGetAsType(t *testing.T) {
IntValue: 123,
StringValue: "test",
}
wrong := struct {
WrongValue float64 `json:"wrongValue"`
}{
WrongValue: 1.23,
}
fallback := testStruct{
BoolValue: false,
IntValue: 456,
Expand All @@ -458,7 +511,11 @@ func TestGetAsType(t *testing.T) {
if err != nil {
t.Errorf("Error marshalling testStruct: %v", err)
}
setupEnv(string(strEnv))
strWrong, err := json.Marshal(wrong)
if err != nil {
t.Errorf("Error marshalling wrong: %v", err)
}
setupEnv(string(strEnv), string(strWrong))
defer teardownEnv()

type args struct {
Expand Down Expand Up @@ -489,7 +546,7 @@ func TestGetAsType(t *testing.T) {
required: false,
fallback: fallback,
},
want: testStruct{},
want: fallback,
wantErr: true,
},
{
Expand Down

0 comments on commit 6eed3c2

Please sign in to comment.