tpl/collections: Add Pages support to Intersect and Union

This enables `AND` (`intersect`)  and `OR` (`union`)  filters when combined with `where`.

Example:

```go
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
```

The above fetches regular pages not of `page` or `about` type unless they are pinned. And finally, we exclude all pages with no `images` set in Page params.

Fixes #3174
This commit is contained in:
Bjørn Erik Pedersen
2017-07-03 10:32:10 +02:00
parent d12cf5a25d
commit ccdd08d57a
5 changed files with 227 additions and 146 deletions

View File

@@ -18,7 +18,6 @@ import (
"fmt"
"reflect"
"strings"
"time"
)
// Where returns a filtered subset of a given data type.
@@ -404,6 +403,16 @@ func toInt(v reflect.Value) (int64, error) {
return -1, errors.New("unable to convert value to int")
}
func toUint(v reflect.Value) (uint64, error) {
switch v.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint(), nil
case reflect.Interface:
return toUint(v.Elem())
}
return 0, errors.New("unable to convert value to uint")
}
// toString returns the string value if possible, "" if not.
func toString(v reflect.Value) (string, error) {
switch v.Kind() {
@@ -415,12 +424,6 @@ func toString(v reflect.Value) (string, error) {
return "", errors.New("unable to convert value to string")
}
var (
zero reflect.Value
errorType = reflect.TypeOf((*error)(nil)).Elem()
timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
)
func toTimeUnix(v reflect.Value) int64 {
if v.Kind() == reflect.Interface {
return toTimeUnix(v.Elem())