Skip to content

Commit f3c74c9

Browse files
committed
Add boolean value comparison to where tpl function
`where` template function's internal condition check function doesn't check boolean values and always returns `false` silently. This adds missing boolean value comparison to the function. `where Values ".Param.key" true` like clause can be used. Only "=", "==", "eq", "!=", "<>", "ne" operators are allowed to be used with a boolean value. If an other operator is passed with it, the condition check function returns `false` like before.
1 parent e445c35 commit f3c74c9

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

‎tpl/template_funcs.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,16 @@ func checkCondition(v, mv reflect.Value, op string) (bool, error) {
588588
return false, nil
589589
}
590590

591+
if v.Kind() == reflect.Bool && mv.Kind() == reflect.Bool {
592+
switch op {
593+
case "", "=", "==", "eq":
594+
return v.Bool() == mv.Bool(), nil
595+
case "!=", "<>", "ne":
596+
return v.Bool() != mv.Bool(), nil
597+
}
598+
return false, nil
599+
}
600+
591601
var ivp, imvp *int64
592602
var svp, smvp *string
593603
var ima []int64

‎tpl/template_funcs_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ func TestCheckCondition(t *testing.T) {
727727
"",
728728
expect{true, false},
729729
},
730+
{reflect.ValueOf(true), reflect.ValueOf(true), "", expect{true, false}},
730731
{reflect.ValueOf(nil), reflect.ValueOf(nil), "", expect{true, false}},
731732
{reflect.ValueOf(123), reflect.ValueOf(456), "!=", expect{true, false}},
732733
{reflect.ValueOf("foo"), reflect.ValueOf("bar"), "!=", expect{true, false}},
@@ -736,6 +737,7 @@ func TestCheckCondition(t *testing.T) {
736737
"!=",
737738
expect{true, false},
738739
},
740+
{reflect.ValueOf(true), reflect.ValueOf(false), "!=", expect{true, false}},
739741
{reflect.ValueOf(123), reflect.ValueOf(nil), "!=", expect{true, false}},
740742
{reflect.ValueOf(456), reflect.ValueOf(123), ">=", expect{true, false}},
741743
{reflect.ValueOf("foo"), reflect.ValueOf("bar"), ">=", expect{true, false}},
@@ -799,8 +801,12 @@ func TestCheckCondition(t *testing.T) {
799801
{reflect.ValueOf("foo"), reflect.Value{}, "", expect{false, false}},
800802
{reflect.ValueOf((*TstX)(nil)), reflect.ValueOf("foo"), "", expect{false, false}},
801803
{reflect.ValueOf("foo"), reflect.ValueOf((*TstX)(nil)), "", expect{false, false}},
804+
{reflect.ValueOf(true), reflect.ValueOf("foo"), "", expect{false, false}},
805+
{reflect.ValueOf("foo"), reflect.ValueOf(true), "", expect{false, false}},
802806
{reflect.ValueOf("foo"), reflect.ValueOf(map[int]string{}), "", expect{false, false}},
803807
{reflect.ValueOf("foo"), reflect.ValueOf([]int{1, 2}), "", expect{false, false}},
808+
{reflect.ValueOf((*TstX)(nil)), reflect.ValueOf((*TstX)(nil)), ">", expect{false, false}},
809+
{reflect.ValueOf(true), reflect.ValueOf(false), ">", expect{false, false}},
804810
{reflect.ValueOf(123), reflect.ValueOf([]int{}), "in", expect{false, false}},
805811
{reflect.ValueOf(123), reflect.ValueOf(123), "op", expect{false, true}},
806812
} {
@@ -1024,6 +1030,31 @@ func TestWhere(t *testing.T) {
10241030
key: "b", op: ">", match: nil,
10251031
expect: []map[string]int{},
10261032
},
1033+
{
1034+
sequence: []map[string]bool{
1035+
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
1036+
},
1037+
key: "b", op: "", match: true,
1038+
expect: []map[string]bool{
1039+
{"c": true, "b": true},
1040+
},
1041+
},
1042+
{
1043+
sequence: []map[string]bool{
1044+
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
1045+
},
1046+
key: "b", op: "!=", match: true,
1047+
expect: []map[string]bool{
1048+
{"a": true, "b": false}, {"d": true, "b": false},
1049+
},
1050+
},
1051+
{
1052+
sequence: []map[string]bool{
1053+
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
1054+
},
1055+
key: "b", op: ">", match: false,
1056+
expect: []map[string]bool{},
1057+
},
10271058
{sequence: (*[]TstX)(nil), key: "A", match: "a", expect: false},
10281059
{sequence: TstX{A: "a", B: "b"}, key: "A", match: "a", expect: false},
10291060
{sequence: []map[string]*TstX{{"foo": nil}}, key: "foo.B", match: "d", expect: false},

0 commit comments

Comments
 (0)