Add boolean value comparison to where tpl function
authorTatsushi Demachi <tdemachi@gmail.com>
Mon, 7 Dec 2015 20:28:36 +0000 (05:28 +0900)
committerTatsushi Demachi <tdemachi@gmail.com>
Mon, 7 Dec 2015 20:39:11 +0000 (05:39 +0900)
`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.

tpl/template_funcs.go
tpl/template_funcs_test.go

index 12d412a8fd649574192e9b35a18c08f49c6f0f02..87dde3292c0e66d1a2c4ea3d141578b72c3e62a1 100644 (file)
@@ -588,6 +588,16 @@ func checkCondition(v, mv reflect.Value, op string) (bool, error) {
                return false, nil
        }
 
+       if v.Kind() == reflect.Bool && mv.Kind() == reflect.Bool {
+               switch op {
+               case "", "=", "==", "eq":
+                       return v.Bool() == mv.Bool(), nil
+               case "!=", "<>", "ne":
+                       return v.Bool() != mv.Bool(), nil
+               }
+               return false, nil
+       }
+
        var ivp, imvp *int64
        var svp, smvp *string
        var ima []int64
index 99acfa475bcb443a13ee48e244b6b7bb4daaa1e7..db6236f1129f7622a15e86e808282c50c2a5f323 100644 (file)
@@ -727,6 +727,7 @@ func TestCheckCondition(t *testing.T) {
                        "",
                        expect{true, false},
                },
+               {reflect.ValueOf(true), reflect.ValueOf(true), "", expect{true, false}},
                {reflect.ValueOf(nil), reflect.ValueOf(nil), "", expect{true, false}},
                {reflect.ValueOf(123), reflect.ValueOf(456), "!=", expect{true, false}},
                {reflect.ValueOf("foo"), reflect.ValueOf("bar"), "!=", expect{true, false}},
@@ -736,6 +737,7 @@ func TestCheckCondition(t *testing.T) {
                        "!=",
                        expect{true, false},
                },
+               {reflect.ValueOf(true), reflect.ValueOf(false), "!=", expect{true, false}},
                {reflect.ValueOf(123), reflect.ValueOf(nil), "!=", expect{true, false}},
                {reflect.ValueOf(456), reflect.ValueOf(123), ">=", expect{true, false}},
                {reflect.ValueOf("foo"), reflect.ValueOf("bar"), ">=", expect{true, false}},
@@ -799,8 +801,12 @@ func TestCheckCondition(t *testing.T) {
                {reflect.ValueOf("foo"), reflect.Value{}, "", expect{false, false}},
                {reflect.ValueOf((*TstX)(nil)), reflect.ValueOf("foo"), "", expect{false, false}},
                {reflect.ValueOf("foo"), reflect.ValueOf((*TstX)(nil)), "", expect{false, false}},
+               {reflect.ValueOf(true), reflect.ValueOf("foo"), "", expect{false, false}},
+               {reflect.ValueOf("foo"), reflect.ValueOf(true), "", expect{false, false}},
                {reflect.ValueOf("foo"), reflect.ValueOf(map[int]string{}), "", expect{false, false}},
                {reflect.ValueOf("foo"), reflect.ValueOf([]int{1, 2}), "", expect{false, false}},
+               {reflect.ValueOf((*TstX)(nil)), reflect.ValueOf((*TstX)(nil)), ">", expect{false, false}},
+               {reflect.ValueOf(true), reflect.ValueOf(false), ">", expect{false, false}},
                {reflect.ValueOf(123), reflect.ValueOf([]int{}), "in", expect{false, false}},
                {reflect.ValueOf(123), reflect.ValueOf(123), "op", expect{false, true}},
        } {
@@ -1024,6 +1030,31 @@ func TestWhere(t *testing.T) {
                        key: "b", op: ">", match: nil,
                        expect: []map[string]int{},
                },
+               {
+                       sequence: []map[string]bool{
+                               {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
+                       },
+                       key: "b", op: "", match: true,
+                       expect: []map[string]bool{
+                               {"c": true, "b": true},
+                       },
+               },
+               {
+                       sequence: []map[string]bool{
+                               {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
+                       },
+                       key: "b", op: "!=", match: true,
+                       expect: []map[string]bool{
+                               {"a": true, "b": false}, {"d": true, "b": false},
+                       },
+               },
+               {
+                       sequence: []map[string]bool{
+                               {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
+                       },
+                       key: "b", op: ">", match: false,
+                       expect: []map[string]bool{},
+               },
                {sequence: (*[]TstX)(nil), key: "A", match: "a", expect: false},
                {sequence: TstX{A: "a", B: "b"}, key: "A", match: "a", expect: false},
                {sequence: []map[string]*TstX{{"foo": nil}}, key: "foo.B", match: "d", expect: false},