diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-26 20:07:15 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-26 20:09:08 +0200 |
commit | 2bea9d0ca19fb442803abf57171e89b9d824b154 (patch) | |
tree | cd7fb655621acdfc97bdbc83145bab069b2e72bc /tpl | |
parent | 1b92c8b713dcaea36516ef86a72a82909c394d78 (diff) |
Revert "tplimpl: return an error on unsupported type in isSet"
This breaks the theme site and lots of themes, so we will have to thinkg a little harder about this one.
This reverts commit 74ea81b885adc64d0194df461cbc85667294d16e.
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/tplimpl/template_funcs.go | 10 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs_test.go | 30 |
2 files changed, 10 insertions, 30 deletions
diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 987a00d98..1ec05b0c7 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -1325,24 +1325,22 @@ func (p pairList) sort() interface{} { // isSet returns whether a given array, channel, slice, or map has a key // defined. -func isSet(a interface{}, key interface{}) (bool, error) { +func isSet(a interface{}, key interface{}) bool { av := reflect.ValueOf(a) kv := reflect.ValueOf(key) switch av.Kind() { case reflect.Array, reflect.Chan, reflect.Slice: if int64(av.Len()) > kv.Int() { - return true, nil + return true } case reflect.Map: if kv.Type() == av.Type().Key() { - return av.MapIndex(kv).IsValid(), nil + return av.MapIndex(kv).IsValid() } - default: - return false, fmt.Errorf("unsupported type %q", av.Kind()) } - return false, nil + return false } // returnWhenSet returns a given value if it set. Otherwise, it returns an diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index 355de182f..aa322dded 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -1070,31 +1070,13 @@ func TestUnion(t *testing.T) { func TestIsSet(t *testing.T) { t.Parallel() + aSlice := []interface{}{1, 2, 3, 5} + aMap := map[string]interface{}{"a": 1, "b": 2} - for _, test := range []struct { - src interface{} - key interface{} - res bool - isErr bool - errStr string - }{ - {[]interface{}{1, 2, 3, 5}, 2, true, false, ""}, - {[]interface{}{1, 2, 3, 5}, 22, false, false, ""}, - - {map[string]interface{}{"a": 1, "b": 2}, "b", true, false, ""}, - {map[string]interface{}{"a": 1, "b": 2}, "bc", false, false, ""}, - - {time.Now(), 1, false, true, `unsupported type "struct"`}, - } { - res, err := isSet(test.src, test.key) - if test.isErr { - assert.EqualError(t, err, test.errStr) - continue - } - - assert.NoError(t, err) - assert.Equal(t, test.res, res) - } + assert.True(t, isSet(aSlice, 2)) + assert.True(t, isSet(aMap, "b")) + assert.False(t, isSet(aSlice, 22)) + assert.False(t, isSet(aMap, "bc")) } func (x *TstX) TstRp() string { |