Skip to content

Commit 99fba0f

Browse files
committed
fix #21 cast empty string to bool false
1 parent a852958 commit 99fba0f

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

decoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
380380

381381
case reflect.Bool:
382382

383-
if !ok || len(arr[idx]) == 0 {
383+
if !ok {
384384
return
385385
}
386386

decoder_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ func TestDecoderBool(t *testing.T) {
448448
type TestBool struct {
449449
Bool bool
450450
BoolPtr *bool
451+
BoolPtrNil *bool
452+
BoolPtrEmpty *bool
451453
BoolArray []bool
452454
BoolPtrArray []*bool
453455
BoolArrayArray [][]bool
@@ -460,6 +462,7 @@ func TestDecoderBool(t *testing.T) {
460462
values := url.Values{
461463
"Bool": []string{"true"},
462464
"BoolPtr": []string{"true"},
465+
"BoolPtrEmpty": []string{""},
463466
"BoolArray": []string{"off", "t", "on"},
464467
"BoolPtrArray[0]": []string{"true"},
465468
"BoolPtrArray[2]": []string{"T"},
@@ -484,6 +487,9 @@ func TestDecoderBool(t *testing.T) {
484487
Equal(t, test.Bool, true)
485488

486489
Equal(t, *test.BoolPtr, true)
490+
Equal(t, test.BoolPtrNil, nil)
491+
NotEqual(t, test.BoolPtrEmpty, nil)
492+
Equal(t, *test.BoolPtrEmpty, false)
487493

488494
Equal(t, len(test.BoolArray), 7)
489495
Equal(t, test.BoolArray[0], false)

util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func parseBool(str string) (bool, error) {
3636
switch str {
3737
case "1", "t", "T", "true", "TRUE", "True", "on", "yes", "ok":
3838
return true, nil
39-
case "0", "f", "F", "false", "FALSE", "False", "off", "no":
39+
case "", "0", "f", "F", "false", "FALSE", "False", "off", "no":
4040
return false, nil
4141
}
4242

0 commit comments

Comments
 (0)