2017-03-13 18:55:02 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2019-11-21 15:59:38 -05:00
|
|
|
|
2022-03-08 04:06:12 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hreflect"
|
2019-11-21 15:59:38 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Where returns a filtered subset of a given data type.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Where(seq, key any, args ...any) (any, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
seqv, isNil := indirect(reflect.ValueOf(seq))
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value of type " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
mv, op, err := parseWhereArgs(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var path []string
|
|
|
|
kv := reflect.ValueOf(key)
|
|
|
|
if kv.Kind() == reflect.String {
|
|
|
|
path = strings.Split(strings.Trim(kv.String(), "."), ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
return ns.checkWhereArray(seqv, kv, mv, path, op)
|
|
|
|
case reflect.Map:
|
|
|
|
return ns.checkWhereMap(seqv, kv, mv, path, op)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("can't iterate over %v", seq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error) {
|
|
|
|
v, vIsNil := indirect(v)
|
|
|
|
if !v.IsValid() {
|
|
|
|
vIsNil = true
|
|
|
|
}
|
2017-07-27 18:23:49 -04:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
mv, mvIsNil := indirect(mv)
|
|
|
|
if !mv.IsValid() {
|
|
|
|
mvIsNil = true
|
|
|
|
}
|
|
|
|
if vIsNil || mvIsNil {
|
|
|
|
switch op {
|
|
|
|
case "", "=", "==", "eq":
|
|
|
|
return vIsNil == mvIsNil, nil
|
|
|
|
case "!=", "<>", "ne":
|
|
|
|
return vIsNil != mvIsNil, nil
|
|
|
|
}
|
|
|
|
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
|
2018-11-26 19:40:35 -05:00
|
|
|
var fvp, fmvp *float64
|
2017-03-13 18:55:02 -04:00
|
|
|
var svp, smvp *string
|
2022-03-17 17:03:27 -04:00
|
|
|
var slv, slmv any
|
2017-03-13 18:55:02 -04:00
|
|
|
var ima []int64
|
2018-11-26 19:40:35 -05:00
|
|
|
var fma []float64
|
2017-03-13 18:55:02 -04:00
|
|
|
var sma []string
|
2021-04-23 05:05:15 -04:00
|
|
|
|
|
|
|
if mv.Kind() == v.Kind() {
|
2017-03-13 18:55:02 -04:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
iv := v.Int()
|
|
|
|
ivp = &iv
|
|
|
|
imv := mv.Int()
|
|
|
|
imvp = &imv
|
|
|
|
case reflect.String:
|
|
|
|
sv := v.String()
|
|
|
|
svp = &sv
|
|
|
|
smv := mv.String()
|
|
|
|
smvp = &smv
|
2018-11-26 19:40:35 -05:00
|
|
|
case reflect.Float64:
|
|
|
|
fv := v.Float()
|
|
|
|
fvp = &fv
|
|
|
|
fmv := mv.Float()
|
|
|
|
fmvp = &fmv
|
2017-03-13 18:55:02 -04:00
|
|
|
case reflect.Struct:
|
|
|
|
switch v.Type() {
|
|
|
|
case timeType:
|
|
|
|
iv := toTimeUnix(v)
|
|
|
|
ivp = &iv
|
|
|
|
imv := toTimeUnix(mv)
|
|
|
|
imvp = &imv
|
|
|
|
}
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
slv = v.Interface()
|
|
|
|
slmv = mv.Interface()
|
|
|
|
}
|
2019-05-30 05:32:58 -04:00
|
|
|
} else if isNumber(v.Kind()) && isNumber(mv.Kind()) {
|
|
|
|
fv, err := toFloat(v)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
fvp = &fv
|
|
|
|
fmv, err := toFloat(mv)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
fmvp = &fmv
|
2017-03-13 18:55:02 -04:00
|
|
|
} else {
|
|
|
|
if mv.Kind() != reflect.Array && mv.Kind() != reflect.Slice {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if mv.Len() == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-07-27 18:23:49 -04:00
|
|
|
if v.Kind() != reflect.Interface && mv.Type().Elem().Kind() != reflect.Interface && mv.Type().Elem() != v.Type() && v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
|
2017-03-13 18:55:02 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
iv := v.Int()
|
|
|
|
ivp = &iv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
2017-05-01 22:25:39 -04:00
|
|
|
if anInt, err := toInt(mv.Index(i)); err == nil {
|
2017-03-13 18:55:02 -04:00
|
|
|
ima = append(ima, anInt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
sv := v.String()
|
|
|
|
svp = &sv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
2017-05-01 22:25:39 -04:00
|
|
|
if aString, err := toString(mv.Index(i)); err == nil {
|
2017-03-13 18:55:02 -04:00
|
|
|
sma = append(sma, aString)
|
|
|
|
}
|
|
|
|
}
|
2018-11-26 19:40:35 -05:00
|
|
|
case reflect.Float64:
|
|
|
|
fv := v.Float()
|
|
|
|
fvp = &fv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
|
|
|
if aFloat, err := toFloat(mv.Index(i)); err == nil {
|
|
|
|
fma = append(fma, aFloat)
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
case reflect.Struct:
|
|
|
|
switch v.Type() {
|
|
|
|
case timeType:
|
|
|
|
iv := toTimeUnix(v)
|
|
|
|
ivp = &iv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
|
|
|
ima = append(ima, toTimeUnix(mv.Index(i)))
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 18:23:49 -04:00
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
slv = v.Interface()
|
|
|
|
slmv = mv.Interface()
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case "", "=", "==", "eq":
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && imvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *ivp == *imvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil && smvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *svp == *smvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && fmvp != nil:
|
|
|
|
return *fvp == *fmvp, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
case "!=", "<>", "ne":
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && imvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *ivp != *imvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil && smvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *svp != *smvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && fmvp != nil:
|
|
|
|
return *fvp != *fmvp, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
case ">=", "ge":
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && imvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *ivp >= *imvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil && smvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *svp >= *smvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && fmvp != nil:
|
|
|
|
return *fvp >= *fmvp, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
case ">", "gt":
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && imvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *ivp > *imvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil && smvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *svp > *smvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && fmvp != nil:
|
|
|
|
return *fvp > *fmvp, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
case "<=", "le":
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && imvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *ivp <= *imvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil && smvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *svp <= *smvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && fmvp != nil:
|
|
|
|
return *fvp <= *fmvp, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
case "<", "lt":
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && imvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *ivp < *imvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil && smvp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
return *svp < *smvp, nil
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && fmvp != nil:
|
|
|
|
return *fvp < *fmvp, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
case "in", "not in":
|
|
|
|
var r bool
|
2018-11-26 19:40:35 -05:00
|
|
|
switch {
|
|
|
|
case ivp != nil && len(ima) > 0:
|
2019-04-19 02:58:12 -04:00
|
|
|
r, _ = ns.In(ima, *ivp)
|
2018-11-26 19:40:35 -05:00
|
|
|
case fvp != nil && len(fma) > 0:
|
2019-04-19 02:58:12 -04:00
|
|
|
r, _ = ns.In(fma, *fvp)
|
2018-11-26 19:40:35 -05:00
|
|
|
case svp != nil:
|
2017-03-13 18:55:02 -04:00
|
|
|
if len(sma) > 0 {
|
2019-04-19 02:58:12 -04:00
|
|
|
r, _ = ns.In(sma, *svp)
|
2017-03-13 18:55:02 -04:00
|
|
|
} else if smvp != nil {
|
2019-04-19 02:58:12 -04:00
|
|
|
r, _ = ns.In(*smvp, *svp)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2018-11-26 19:40:35 -05:00
|
|
|
default:
|
2017-03-13 18:55:02 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if op == "not in" {
|
|
|
|
return !r, nil
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
case "intersect":
|
|
|
|
r, err := ns.Intersect(slv, slmv)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.TypeOf(r).Kind() == reflect.Slice {
|
|
|
|
s := reflect.ValueOf(r)
|
|
|
|
|
|
|
|
if s.Len() > 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, errors.New("invalid intersect values")
|
|
|
|
default:
|
|
|
|
return false, errors.New("no such operator")
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func evaluateSubElem(obj reflect.Value, elemName string) (reflect.Value, error) {
|
|
|
|
if !obj.IsValid() {
|
|
|
|
return zero, errors.New("can't evaluate an invalid value")
|
|
|
|
}
|
2019-11-21 15:59:38 -05:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
typ := obj.Type()
|
|
|
|
obj, isNil := indirect(obj)
|
|
|
|
|
2019-05-27 16:57:57 -04:00
|
|
|
if obj.Kind() == reflect.Interface {
|
|
|
|
// If obj is an interface, we need to inspect the value it contains
|
|
|
|
// to see the full set of methods and fields.
|
|
|
|
// Indirect returns the value that it points to, which is what's needed
|
|
|
|
// below to be able to reflect on its fields.
|
|
|
|
obj = reflect.Indirect(obj.Elem())
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// first, check whether obj has a method. In this case, obj is
|
2019-05-27 16:57:57 -04:00
|
|
|
// a struct or its pointer. If obj is a struct,
|
2017-03-13 18:55:02 -04:00
|
|
|
// to check all T and *T method, use obj pointer type Value
|
|
|
|
objPtr := obj
|
|
|
|
if objPtr.Kind() != reflect.Interface && objPtr.CanAddr() {
|
|
|
|
objPtr = objPtr.Addr()
|
|
|
|
}
|
2019-11-21 15:59:38 -05:00
|
|
|
|
2022-03-08 04:06:12 -05:00
|
|
|
index := hreflect.GetMethodIndexByName(objPtr.Type(), elemName)
|
|
|
|
if index != -1 {
|
|
|
|
mt := objPtr.Type().Method(index)
|
2018-12-27 23:28:54 -05:00
|
|
|
switch {
|
|
|
|
case mt.PkgPath != "":
|
2017-03-13 18:55:02 -04:00
|
|
|
return zero, fmt.Errorf("%s is an unexported method of type %s", elemName, typ)
|
2018-12-27 23:28:54 -05:00
|
|
|
case mt.Type.NumIn() > 1:
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but requires more than 1 parameter", elemName, typ)
|
|
|
|
case mt.Type.NumOut() == 0:
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but returns no output", elemName, typ)
|
|
|
|
case mt.Type.NumOut() > 2:
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but returns more than 2 outputs", elemName, typ)
|
|
|
|
case mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType):
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but only returns an error type", elemName, typ)
|
|
|
|
case mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType):
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s returning two values but the second value is not an error type", elemName, typ)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
res := objPtr.Method(mt.Index).Call([]reflect.Value{})
|
|
|
|
if len(res) == 2 && !res[1].IsNil() {
|
|
|
|
return zero, fmt.Errorf("error at calling a method %s of type %s: %s", elemName, typ, res[1].Interface().(error))
|
|
|
|
}
|
|
|
|
return res[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// elemName isn't a method so next start to check whether it is
|
|
|
|
// a struct field or a map value. In both cases, it mustn't be
|
|
|
|
// a nil value
|
|
|
|
if isNil {
|
|
|
|
return zero, fmt.Errorf("can't evaluate a nil pointer of type %s by a struct field or map key name %s", typ, elemName)
|
|
|
|
}
|
|
|
|
switch obj.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
ft, ok := obj.Type().FieldByName(elemName)
|
|
|
|
if ok {
|
|
|
|
if ft.PkgPath != "" && !ft.Anonymous {
|
|
|
|
return zero, fmt.Errorf("%s is an unexported field of struct type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
return obj.FieldByIndex(ft.Index), nil
|
|
|
|
}
|
|
|
|
return zero, fmt.Errorf("%s isn't a field of struct type %s", elemName, typ)
|
|
|
|
case reflect.Map:
|
|
|
|
kv := reflect.ValueOf(elemName)
|
|
|
|
if kv.Type().AssignableTo(obj.Type().Key()) {
|
|
|
|
return obj.MapIndex(kv), nil
|
|
|
|
}
|
|
|
|
return zero, fmt.Errorf("%s isn't a key of map type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
return zero, fmt.Errorf("%s is neither a struct field, a method nor a map element of type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWhereArgs parses the end arguments to the where function. Return a
|
|
|
|
// match value and an operator, if one is defined.
|
2022-03-17 17:03:27 -04:00
|
|
|
func parseWhereArgs(args ...any) (mv reflect.Value, op string, err error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
mv = reflect.ValueOf(args[0])
|
|
|
|
case 2:
|
|
|
|
var ok bool
|
|
|
|
if op, ok = args[0].(string); !ok {
|
|
|
|
err = errors.New("operator argument must be string type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op = strings.TrimSpace(strings.ToLower(op))
|
|
|
|
mv = reflect.ValueOf(args[1])
|
|
|
|
default:
|
|
|
|
err = errors.New("can't evaluate the array by no match argument or more than or equal to two arguments")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkWhereArray handles the where-matching logic when the seqv value is an
|
|
|
|
// Array or Slice.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, op string) (any, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
rv := reflect.MakeSlice(seqv.Type(), 0, 0)
|
2019-11-21 15:59:38 -05:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
var vvv reflect.Value
|
|
|
|
rvv := seqv.Index(i)
|
2019-11-21 15:59:38 -05:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
if kv.Kind() == reflect.String {
|
2019-11-21 15:59:38 -05:00
|
|
|
if params, ok := rvv.Interface().(maps.Params); ok {
|
|
|
|
vvv = reflect.ValueOf(params.Get(path...))
|
|
|
|
} else {
|
|
|
|
vvv = rvv
|
2021-04-21 03:08:42 -04:00
|
|
|
for i, elemName := range path {
|
2019-11-21 15:59:38 -05:00
|
|
|
var err error
|
|
|
|
vvv, err = evaluateSubElem(vvv, elemName)
|
2021-04-21 03:08:42 -04:00
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-21 03:08:42 -04:00
|
|
|
|
|
|
|
if i < len(path)-1 && vvv.IsValid() {
|
|
|
|
if params, ok := vvv.Interface().(maps.Params); ok {
|
|
|
|
// The current path element is the map itself, .Params.
|
|
|
|
vvv = reflect.ValueOf(params.Get(path[i+1:]...))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vv, _ := indirect(rvv)
|
|
|
|
if vv.Kind() == reflect.Map && kv.Type().AssignableTo(vv.Type().Key()) {
|
|
|
|
vvv = vv.MapIndex(kv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := ns.checkCondition(vvv, mv, op); ok {
|
|
|
|
rv = reflect.Append(rv, rvv)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv.Interface(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkWhereMap handles the where-matching logic when the seqv value is a Map.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) checkWhereMap(seqv, kv, mv reflect.Value, path []string, op string) (any, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
rv := reflect.MakeMap(seqv.Type())
|
|
|
|
keys := seqv.MapKeys()
|
|
|
|
for _, k := range keys {
|
|
|
|
elemv := seqv.MapIndex(k)
|
|
|
|
switch elemv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
r, err := ns.checkWhereArray(elemv, kv, mv, path, op)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rr := reflect.ValueOf(r); rr.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
if rr.Len() > 0 {
|
|
|
|
rv.SetMapIndex(k, elemv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Interface:
|
|
|
|
elemvv, isNil := indirect(elemv)
|
|
|
|
if isNil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch elemvv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
r, err := ns.checkWhereArray(elemvv, kv, mv, path, op)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rr := reflect.ValueOf(r); rr.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
if rr.Len() > 0 {
|
|
|
|
rv.SetMapIndex(k, elemv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2017-07-03 04:08:55 -04:00
|
|
|
// toFloat returns the float value if possible.
|
2017-05-01 22:25:39 -04:00
|
|
|
func toFloat(v reflect.Value) (float64, error) {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return v.Float(), nil
|
2019-05-30 05:32:58 -04:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return v.Convert(reflect.TypeOf(float64(0))).Float(), nil
|
2017-05-01 22:25:39 -04:00
|
|
|
case reflect.Interface:
|
|
|
|
return toFloat(v.Elem())
|
|
|
|
}
|
|
|
|
return -1, errors.New("unable to convert value to float")
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// toInt returns the int value if possible, -1 if not.
|
2017-07-03 04:08:55 -04:00
|
|
|
// TODO(bep) consolidate all these reflect funcs.
|
2017-05-01 22:25:39 -04:00
|
|
|
func toInt(v reflect.Value) (int64, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2017-05-01 22:25:39 -04:00
|
|
|
return v.Int(), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
case reflect.Interface:
|
|
|
|
return toInt(v.Elem())
|
|
|
|
}
|
2017-05-01 22:25:39 -04:00
|
|
|
return -1, errors.New("unable to convert value to int")
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2017-07-03 04:32:10 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// toString returns the string value if possible, "" if not.
|
2017-05-01 22:25:39 -04:00
|
|
|
func toString(v reflect.Value) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.String:
|
2017-05-01 22:25:39 -04:00
|
|
|
return v.String(), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
case reflect.Interface:
|
|
|
|
return toString(v.Elem())
|
|
|
|
}
|
2017-05-01 22:25:39 -04:00
|
|
|
return "", errors.New("unable to convert value to string")
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func toTimeUnix(v reflect.Value) int64 {
|
|
|
|
if v.Kind() == reflect.Interface {
|
|
|
|
return toTimeUnix(v.Elem())
|
|
|
|
}
|
|
|
|
if v.Type() != timeType {
|
|
|
|
panic("coding error: argument must be time.Time type reflect Value")
|
|
|
|
}
|
2022-03-08 04:06:12 -05:00
|
|
|
return hreflect.GetMethodByName(v, "Unix").Call([]reflect.Value{})[0].Int()
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|