Skip to content

Commit 59fb57b

Browse files
joeybloggsjoeybloggs
authored andcommitted
add fallback boolean values "on", "yes", "ok", "off","no"
1 parent 44b2bb1 commit 59fb57b

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package form
22
============
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/form/master/logo.jpg">
4-
![Project status](https://img.shields.io/badge/version-1.0-green.svg)
4+
![Project status](https://img.shields.io/badge/version-1.1.0-green.svg)
55
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/form/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/form)
66
[![Coverage Status](https://coveralls.io/repos/github/go-playground/form/badge.svg?branch=master)](https://coveralls.io/github/go-playground/form?branch=master)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/form)](https://goreportcard.com/report/github.com/go-playground/form)

form.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,12 @@ func (d *formDecoder) setFieldByType(current reflect.Value, namespace string, id
390390
var b bool
391391

392392
if b, err = strconv.ParseBool(arr[idx]); err != nil {
393-
d.setError(namespace, fmt.Errorf("Invalid Boolean Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), namespace))
394-
return
393+
// lets try some fallbacks bool values not supported by strconv.ParseBool(...)
394+
var found bool
395+
if b, found = fallbackBoolValue(arr[idx]); !found {
396+
d.setError(namespace, fmt.Errorf("Invalid Boolean Value '%s' Type '%v' Namespace '%s'", arr[idx], v.Type(), namespace))
397+
return
398+
}
395399
}
396400

397401
v.SetBool(b)
@@ -606,8 +610,12 @@ func (d *formDecoder) getMapKey(key string, current reflect.Value, namespace str
606610

607611
b, e := strconv.ParseBool(key)
608612
if e != nil {
609-
err = fmt.Errorf("Invalid Boolean Value '%s' Type '%v' Namespace '%s'", key, v.Type(), namespace)
610-
return
613+
// lets try some fallbacks bool values not supported by strconv.ParseBool(...)
614+
var found bool
615+
if b, found = fallbackBoolValue(key); !found {
616+
err = fmt.Errorf("Invalid Boolean Value '%s' Type '%v' Namespace '%s'", key, v.Type(), namespace)
617+
return
618+
}
611619
}
612620

613621
v.SetBool(b)

form_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,15 @@ func TestBool(t *testing.T) {
422422
values := url.Values{
423423
"Bool": []string{"true"},
424424
"BoolPtr": []string{"true"},
425-
"BoolArray": []string{"true", "t", "1"},
425+
"BoolArray": []string{"off", "t", "on"},
426426
"BoolPtrArray[0]": []string{"true"},
427427
"BoolPtrArray[2]": []string{"T"},
428428
"BoolArrayArray[0][0]": []string{"TRUE"},
429429
"BoolArrayArray[0][2]": []string{"True"},
430430
"BoolArrayArray[2][0]": []string{"true"},
431431
"BoolPtrArrayArray[0][0]": []string{"true"},
432-
"BoolPtrArrayArray[0][2]": []string{"true"},
433-
"BoolPtrArrayArray[2][0]": []string{"true"},
432+
"BoolPtrArrayArray[0][2]": []string{"t"},
433+
"BoolPtrArrayArray[2][0]": []string{"1"},
434434
"BoolMap[true]": []string{"true"},
435435
"BoolPtrMap[t]": []string{"true"},
436436
}
@@ -448,7 +448,7 @@ func TestBool(t *testing.T) {
448448
Equal(t, *test.BoolPtr, true)
449449

450450
Equal(t, len(test.BoolArray), 4)
451-
Equal(t, test.BoolArray[0], true)
451+
Equal(t, test.BoolArray[0], false)
452452
Equal(t, test.BoolArray[1], true)
453453
Equal(t, test.BoolArray[2], true)
454454
Equal(t, test.BoolArray[3], false)
@@ -658,18 +658,18 @@ func TestErrors(t *testing.T) {
658658
}
659659

660660
values := url.Values{
661-
"bool": []string{"yes"},
662-
"Int": []string{"bad"},
663-
"Uint": []string{"bad"},
664-
"Float32": []string{"bad"},
665-
"String": []string{"str bad return val"},
666-
"Time": []string{"bad"},
667-
"MapBadIntKey[key]": []string{"1"},
668-
"MapBadUintKey[key]": []string{"1"},
669-
"MapBadFloatKey[key]": []string{"1.1"},
670-
"MapBadBoolKey[yes]": []string{"true"},
671-
"MapBadKeyType[1.4]": []string{"5"},
672-
"BadArrayValue[0]": []string{"badintval"},
661+
"bool": []string{"uh-huh"},
662+
"Int": []string{"bad"},
663+
"Uint": []string{"bad"},
664+
"Float32": []string{"bad"},
665+
"String": []string{"str bad return val"},
666+
"Time": []string{"bad"},
667+
"MapBadIntKey[key]": []string{"1"},
668+
"MapBadUintKey[key]": []string{"1"},
669+
"MapBadFloatKey[key]": []string{"1.1"},
670+
"MapBadBoolKey[uh-huh]": []string{"true"},
671+
"MapBadKeyType[1.4]": []string{"5"},
672+
"BadArrayValue[0]": []string{"badintval"},
673673
}
674674

675675
var test TestError
@@ -686,7 +686,7 @@ func TestErrors(t *testing.T) {
686686

687687
err := errs.(DecodeErrors)
688688
k := err["bool"]
689-
Equal(t, k.Error(), "Invalid Boolean Value 'yes' Type 'bool' Namespace 'bool'")
689+
Equal(t, k.Error(), "Invalid Boolean Value 'uh-huh' Type 'bool' Namespace 'bool'")
690690

691691
k = err["Int"]
692692
Equal(t, k.Error(), "Invalid Integer Value 'bad' Type 'int' Namespace 'Int'")
@@ -713,7 +713,7 @@ func TestErrors(t *testing.T) {
713713
Equal(t, k.Error(), "Invalid Float Value 'key' Type 'float32' Namespace 'MapBadFloatKey'")
714714

715715
k = err["MapBadBoolKey"]
716-
Equal(t, k.Error(), "Invalid Boolean Value 'yes' Type 'bool' Namespace 'MapBadBoolKey'")
716+
Equal(t, k.Error(), "Invalid Boolean Value 'uh-huh' Type 'bool' Namespace 'MapBadBoolKey'")
717717

718718
k = err["MapBadKeyType"]
719719
Equal(t, k.Error(), "Unsupported Map Key '1.4', Type 'complex64' Namespace 'MapBadKeyType'")

util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ func (d *Decoder) parseStruct(current reflect.Value) cachedStruct {
6161

6262
return s
6363
}
64+
65+
func fallbackBoolValue(val string) (value bool, found bool) {
66+
switch val {
67+
case "on", "yes", "ok":
68+
value = true
69+
found = true
70+
case "off", "no":
71+
found = true
72+
}
73+
74+
return
75+
}

0 commit comments

Comments
 (0)