mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-23 21:53:09 +02:00
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:
@@ -14,26 +14,96 @@
|
||||
package collections
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
func numberToFloat(v reflect.Value) float64 {
|
||||
var (
|
||||
zero reflect.Value
|
||||
errorType = reflect.TypeOf((*error)(nil)).Elem()
|
||||
timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
|
||||
)
|
||||
|
||||
func numberToFloat(v reflect.Value) (float64, error) {
|
||||
switch kind := v.Kind(); {
|
||||
case isFloat(kind):
|
||||
return v.Float()
|
||||
return v.Float(), nil
|
||||
case isInt(kind):
|
||||
return float64(v.Int())
|
||||
case isUInt(kind):
|
||||
return float64(v.Uint())
|
||||
return float64(v.Int()), nil
|
||||
case isUint(kind):
|
||||
return float64(v.Uint()), nil
|
||||
case kind == reflect.Interface:
|
||||
return numberToFloat(v.Elem())
|
||||
default:
|
||||
panic("Invalid type in numberToFloat")
|
||||
return 0, fmt.Errorf("Invalid kind %s in numberToFloat", kind)
|
||||
}
|
||||
}
|
||||
|
||||
// There are potential overflows in this function, but the downconversion of
|
||||
// int64 etc. into int8 etc. is coming from the synthetic unit tests for Union etc.
|
||||
// TODO(bep) We should consider normalizing the slices to int64 etc.
|
||||
func convertNumber(v reflect.Value, to reflect.Kind) (reflect.Value, error) {
|
||||
var n reflect.Value
|
||||
if isFloat(to) {
|
||||
f, err := toFloat(v)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
switch to {
|
||||
case reflect.Float32:
|
||||
n = reflect.ValueOf(float32(f))
|
||||
default:
|
||||
n = reflect.ValueOf(float64(f))
|
||||
}
|
||||
} else if isInt(to) {
|
||||
i, err := toInt(v)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
switch to {
|
||||
case reflect.Int:
|
||||
n = reflect.ValueOf(int(i))
|
||||
case reflect.Int8:
|
||||
n = reflect.ValueOf(int8(i))
|
||||
case reflect.Int16:
|
||||
n = reflect.ValueOf(int16(i))
|
||||
case reflect.Int32:
|
||||
n = reflect.ValueOf(int32(i))
|
||||
case reflect.Int64:
|
||||
n = reflect.ValueOf(int64(i))
|
||||
}
|
||||
} else if isUint(to) {
|
||||
i, err := toUint(v)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
switch to {
|
||||
case reflect.Uint:
|
||||
n = reflect.ValueOf(uint(i))
|
||||
case reflect.Uint8:
|
||||
n = reflect.ValueOf(uint8(i))
|
||||
case reflect.Uint16:
|
||||
n = reflect.ValueOf(uint16(i))
|
||||
case reflect.Uint32:
|
||||
n = reflect.ValueOf(uint32(i))
|
||||
case reflect.Uint64:
|
||||
n = reflect.ValueOf(uint64(i))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if !n.IsValid() {
|
||||
return n, errors.New("invalid values")
|
||||
}
|
||||
|
||||
return n, nil
|
||||
|
||||
}
|
||||
|
||||
func isNumber(kind reflect.Kind) bool {
|
||||
return isInt(kind) || isUInt(kind) || isFloat(kind)
|
||||
return isInt(kind) || isUint(kind) || isFloat(kind)
|
||||
}
|
||||
|
||||
func isInt(kind reflect.Kind) bool {
|
||||
@@ -45,7 +115,7 @@ func isInt(kind reflect.Kind) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func isUInt(kind reflect.Kind) bool {
|
||||
func isUint(kind reflect.Kind) bool {
|
||||
switch kind {
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return true
|
||||
|
Reference in New Issue
Block a user