From 3e84cba47a22b166551f9311ea88d83a2189f0aa Mon Sep 17 00:00:00 2001 From: Jeremy Loy Date: Thu, 31 Oct 2019 00:43:52 -0400 Subject: [PATCH] fixed bug where zero values for bools and ints would falsely error. --- config.go | 3 +++ config_test.go | 11 ++++++++--- example_test.go | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 1d85bd4..f52d0fc 100644 --- a/config.go +++ b/config.go @@ -228,6 +228,9 @@ func convertAndSetSlice(slicePtr interface{}, values []string) []int { // False is used to prevent accidental logging of secrets as // as the strconv include s in their error message. func convertAndSetValue(settable interface{}, s string) bool { + if s == "" { + return true + } settableValue := reflect.ValueOf(settable).Elem() var ( err error diff --git a/config_test.go b/config_test.go index f453043..d2fea0d 100644 --- a/config_test.go +++ b/config_test.go @@ -44,6 +44,7 @@ func Test_Integration(t *testing.T) { "C=4 5 6", "DoG__E=true", "DoG__FErDINANd=true", + // should NOT log doc_j as it is not provided "G=1 y 2", // should log G[1] as it is an incorrect type, but still work with 0 and 2 "H=-84", // should log H as it is an incorrect type "I=", // should NOT log I as there is no way to tell if it is missing or deliberately empty @@ -54,7 +55,11 @@ func Test_Integration(t *testing.T) { } err = os.Setenv("B", "overridden") if err != nil { - t.Fatalf("failed to override environ: %v", err) + t.Fatalf("failed to set environ: %v", err) + } + err = os.Setenv("C", "") // this should NOT override as it is empty + if err != nil { + t.Fatalf("failed to set environ: %v", err) } var got testConfig @@ -71,12 +76,12 @@ func Test_Integration(t *testing.T) { H: 0, I: "", } - wantFailedFields := []string{"file[" + nonExistFile.Name() + "]", "dog__j", "g[1]", "h"} + wantFailedFields := []string{"file[" + nonExistFile.Name() + "]", "g[1]", "h"} builder := From(file.Name()).From(nonExistFile.Name()).FromEnv() gotErr := builder.To(&got) if !reflect.DeepEqual(got, want) { - t.Errorf("Integration: got %+v, wantPanic %+v", got, want) + t.Errorf("Integration: got %+v, want %+v", got, want) } if gotErr == nil { t.Errorf("Integration: should have had an error") diff --git a/example_test.go b/example_test.go index 650d63f..f2c1ab8 100644 --- a/example_test.go +++ b/example_test.go @@ -53,7 +53,7 @@ func Example_errorHandling() { fmt.Println(err) // Output: - // config: the following fields had errors: [port feature_flag] + // config: the following fields had errors: [port] } func Example_fromFileWithOverride() {