summaryrefslogtreecommitdiffstats
path: root/tpl/collections
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-19 08:58:12 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-19 08:58:12 +0200
commit7fbfedf01367ff076c3c875b183789b769b99241 (patch)
treed7c5451a97637f021b457fcf7630492024e440ca /tpl/collections
parent06f56fc983d460506d39b3a6f638b1632af07073 (diff)
tpl/collections: Return error on invalid input in in
See #5875
Diffstat (limited to 'tpl/collections')
-rw-r--r--tpl/collections/collections.go13
-rw-r--r--tpl/collections/collections_test.go7
-rw-r--r--tpl/collections/where.go8
3 files changed, 16 insertions, 12 deletions
diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go
index 69d0f7042..3839ad472 100644
--- a/tpl/collections/collections.go
+++ b/tpl/collections/collections.go
@@ -242,17 +242,16 @@ func (ns *Namespace) First(limit interface{}, seq interface{}) (interface{}, err
}
// In returns whether v is in the set l. l may be an array or slice.
-func (ns *Namespace) In(l interface{}, v interface{}) bool {
+func (ns *Namespace) In(l interface{}, v interface{}) (bool, error) {
if l == nil || v == nil {
- return false
+ return false, nil
}
lv := reflect.ValueOf(l)
vv := reflect.ValueOf(v)
if !vv.Type().Comparable() {
- // TODO(bep) consider adding error to the signature.
- return false
+ return false, errors.Errorf("value to check must be comparable: %T", v)
}
// Normalize numeric types to float64 etc.
@@ -269,15 +268,15 @@ func (ns *Namespace) In(l interface{}, v interface{}) bool {
lvvk := normalize(lvv)
if lvvk == vvk {
- return true
+ return true, nil
}
}
case reflect.String:
if vv.Type() == lv.Type() && strings.Contains(lv.String(), vv.String()) {
- return true
+ return true, nil
}
}
- return false
+ return false, nil
}
// Intersect returns the common elements in the given sets, l1 and l2. l1 and
diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go
index c87490b2c..137c6fa3a 100644
--- a/tpl/collections/collections_test.go
+++ b/tpl/collections/collections_test.go
@@ -313,9 +313,14 @@ func TestIn(t *testing.T) {
errMsg := fmt.Sprintf("[%d] %v", i, test)
- result := ns.In(test.l1, test.l2)
+ result, err := ns.In(test.l1, test.l2)
+ assert.NoError(err)
assert.Equal(test.expect, result, errMsg)
}
+
+ // Slices are not comparable
+ _, err := ns.In([]string{"a", "b"}, []string{"a", "b"})
+ assert.Error(err)
}
type testPage struct {
diff --git a/tpl/collections/where.go b/tpl/collections/where.go
index 2c5dc7f3f..d515d019f 100644
--- a/tpl/collections/where.go
+++ b/tpl/collections/where.go
@@ -225,14 +225,14 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
var r bool
switch {
case ivp != nil && len(ima) > 0:
- r = ns.In(ima, *ivp)
+ r, _ = ns.In(ima, *ivp)
case fvp != nil && len(fma) > 0:
- r = ns.In(fma, *fvp)
+ r, _ = ns.In(fma, *fvp)
case svp != nil:
if len(sma) > 0 {
- r = ns.In(sma, *svp)
+ r, _ = ns.In(sma, *svp)
} else if smvp != nil {
- r = ns.In(*smvp, *svp)
+ r, _ = ns.In(*smvp, *svp)
}
default:
return false, nil