Skip to content

Commit

Permalink
fixed bug where zero values for bools and ints would falsely error.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyLoy committed Oct 31, 2019
1 parent 13c22a5 commit 3e84cba
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 3e84cba

Please sign in to comment.