| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"reflect" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/hmaps" |
| 24 |
"github.com/gohugoio/hugo/common/hreflect" |
| 25 |
"github.com/gohugoio/hugo/common/hstrings" |
| 26 |
"github.com/gohugoio/hugo/compare" |
| 27 |
) |
| 28 |
|
| 29 |
// Where returns a filtered subset of collection c. |
| 30 |
func (ns *Namespace) Where(ctx context.Context, c, key any, args ...any) (any, error) { |
| 31 |
seqv, isNil := hreflect.Indirect(reflect.ValueOf(c)) |
| 32 |
if isNil { |
| 33 |
return nil, errors.New("can't iterate over a nil value of type " + reflect.ValueOf(c).Type().String()) |
| 34 |
} |
| 35 |
|
| 36 |
mv, op, err := parseWhereArgs(args...) |
| 37 |
if err != nil { |
| 38 |
return nil, err |
| 39 |
} |
| 40 |
|
| 41 |
ctxv := reflect.ValueOf(ctx) |
| 42 |
|
| 43 |
var path []string |
| 44 |
kv := reflect.ValueOf(key) |
| 45 |
if kv.Kind() == reflect.String { |
| 46 |
path = strings.Split(strings.Trim(kv.String(), "."), ".") |
| 47 |
} |
| 48 |
|
| 49 |
switch seqv.Kind() { |
| 50 |
case reflect.Array, reflect.Slice: |
| 51 |
return ns.checkWhereArray(ctxv, seqv, kv, mv, path, op) |
| 52 |
case reflect.Map: |
| 53 |
return ns.checkWhereMap(ctxv, seqv, kv, mv, path, op) |
| 54 |
default: |
| 55 |
return nil, fmt.Errorf("can't iterate over %T", c) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error) { |
| 60 |
v, vIsNil := hreflect.Indirect(v) |
| 61 |
if !v.IsValid() { |
| 62 |
vIsNil = true |
| 63 |
} |
| 64 |
|
| 65 |
mv, mvIsNil := hreflect.Indirect(mv) |
| 66 |
if !mv.IsValid() { |
| 67 |
mvIsNil = true |
| 68 |
} |
| 69 |
if vIsNil || mvIsNil { |
| 70 |
switch op { |
| 71 |
case "", "=", "==", "eq": |
| 72 |
return vIsNil == mvIsNil, nil |
| 73 |
case "!=", "<>", "ne": |
| 74 |
return vIsNil != mvIsNil, nil |
| 75 |
} |
| 76 |
return false, nil |
| 77 |
} |
| 78 |
|
| 79 |
if v.Kind() == reflect.Bool && mv.Kind() == reflect.Bool { |
| 80 |
switch op { |
| 81 |
case "", "=", "==", "eq": |
| 82 |
return v.Bool() == mv.Bool(), nil |
| 83 |
case "!=", "<>", "ne": |
| 84 |
return v.Bool() != mv.Bool(), nil |
| 85 |
} |
| 86 |
return false, nil |
| 87 |
} |
| 88 |
|
| 89 |
switch op { |
| 90 |
case "", "=", "==", "eq", "!=", "<>", "ne": |
| 91 |
if eq, ok := tryEq(v, mv); ok { |
| 92 |
if op == "!=" || op == "<>" || op == "ne" { |
| 93 |
return !eq, nil |
| 94 |
} |
| 95 |
return eq, nil |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
var ivp, imvp *int64 |
| 100 |
var fvp, fmvp *float64 |
| 101 |
var svp, smvp *string |
| 102 |
var slv, slmv any |
| 103 |
var ima []int64 |
| 104 |
var fma []float64 |
| 105 |
var sma []string |
| 106 |
|
| 107 |
if mv.Kind() == v.Kind() { |
| 108 |
switch v.Kind() { |
| 109 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 110 |
iv := v.Int() |
| 111 |
ivp = &iv |
| 112 |
imv := mv.Int() |
| 113 |
imvp = &imv |
| 114 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 115 |
iv := int64(v.Uint()) |
| 116 |
ivp = &iv |
| 117 |
imv := int64(mv.Uint()) |
| 118 |
imvp = &imv |
| 119 |
case reflect.String: |
| 120 |
sv := v.String() |
| 121 |
svp = &sv |
| 122 |
smv := mv.String() |
| 123 |
smvp = &smv |
| 124 |
case reflect.Float64: |
| 125 |
fv := v.Float() |
| 126 |
fvp = &fv |
| 127 |
fmv := mv.Float() |
| 128 |
fmvp = &fmv |
| 129 |
case reflect.Struct: |
| 130 |
if hreflect.IsTime(v.Type()) { |
| 131 |
iv := ns.toTimeUnix(v) |
| 132 |
ivp = &iv |
| 133 |
imv := ns.toTimeUnix(mv) |
| 134 |
imvp = &imv |
| 135 |
} |
| 136 |
case reflect.Array, reflect.Slice: |
| 137 |
slv = v.Interface() |
| 138 |
slmv = mv.Interface() |
| 139 |
} |
| 140 |
} else if hreflect.IsNumber(v.Kind()) && hreflect.IsNumber(mv.Kind()) { |
| 141 |
fv, err := hreflect.ToFloat64E(v) |
| 142 |
if err != nil { |
| 143 |
return false, err |
| 144 |
} |
| 145 |
fvp = &fv |
| 146 |
fmv, err := hreflect.ToFloat64E(mv) |
| 147 |
if err != nil { |
| 148 |
return false, err |
| 149 |
} |
| 150 |
fmvp = &fmv |
| 151 |
} else { |
| 152 |
if mv.Kind() != reflect.Array && mv.Kind() != reflect.Slice { |
| 153 |
return false, nil |
| 154 |
} |
| 155 |
|
| 156 |
if mv.Len() == 0 { |
| 157 |
if op == "not in" { |
| 158 |
return true, nil |
| 159 |
} |
| 160 |
return false, nil |
| 161 |
} |
| 162 |
|
| 163 |
if v.Kind() != reflect.Interface && mv.Type().Elem().Kind() != reflect.Interface && mv.Type().Elem() != v.Type() && v.Kind() != reflect.Array && v.Kind() != reflect.Slice { |
| 164 |
return false, nil |
| 165 |
} |
| 166 |
switch v.Kind() { |
| 167 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 168 |
iv := v.Int() |
| 169 |
ivp = &iv |
| 170 |
for i := range mv.Len() { |
| 171 |
if anInt, err := hreflect.ToInt64E(mv.Index(i)); err == nil { |
| 172 |
ima = append(ima, anInt) |
| 173 |
} |
| 174 |
} |
| 175 |
case reflect.String: |
| 176 |
sv := v.String() |
| 177 |
svp = &sv |
| 178 |
for i := range mv.Len() { |
| 179 |
if aString, err := hreflect.ToStringE(mv.Index(i)); err == nil { |
| 180 |
sma = append(sma, aString) |
| 181 |
} |
| 182 |
} |
| 183 |
case reflect.Float64: |
| 184 |
fv := v.Float() |
| 185 |
fvp = &fv |
| 186 |
for i := range mv.Len() { |
| 187 |
if aFloat, err := hreflect.ToFloat64E(mv.Index(i)); err == nil { |
| 188 |
fma = append(fma, aFloat) |
| 189 |
} |
| 190 |
} |
| 191 |
case reflect.Struct: |
| 192 |
if hreflect.IsTime(v.Type()) { |
| 193 |
iv := ns.toTimeUnix(v) |
| 194 |
ivp = &iv |
| 195 |
for i := range mv.Len() { |
| 196 |
ima = append(ima, ns.toTimeUnix(mv.Index(i))) |
| 197 |
} |
| 198 |
} |
| 199 |
case reflect.Array, reflect.Slice: |
| 200 |
slv = v.Interface() |
| 201 |
slmv = mv.Interface() |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
switch op { |
| 206 |
case "", "=", "==", "eq": |
| 207 |
switch { |
| 208 |
case ivp != nil && imvp != nil: |
| 209 |
return *ivp == *imvp, nil |
| 210 |
case svp != nil && smvp != nil: |
| 211 |
return *svp == *smvp, nil |
| 212 |
case fvp != nil && fmvp != nil: |
| 213 |
return *fvp == *fmvp, nil |
| 214 |
} |
| 215 |
case "!=", "<>", "ne": |
| 216 |
switch { |
| 217 |
case ivp != nil && imvp != nil: |
| 218 |
return *ivp != *imvp, nil |
| 219 |
case svp != nil && smvp != nil: |
| 220 |
return *svp != *smvp, nil |
| 221 |
case fvp != nil && fmvp != nil: |
| 222 |
return *fvp != *fmvp, nil |
| 223 |
} |
| 224 |
case ">=", "ge": |
| 225 |
switch { |
| 226 |
case ivp != nil && imvp != nil: |
| 227 |
return *ivp >= *imvp, nil |
| 228 |
case svp != nil && smvp != nil: |
| 229 |
return *svp >= *smvp, nil |
| 230 |
case fvp != nil && fmvp != nil: |
| 231 |
return *fvp >= *fmvp, nil |
| 232 |
} |
| 233 |
case ">", "gt": |
| 234 |
switch { |
| 235 |
case ivp != nil && imvp != nil: |
| 236 |
return *ivp > *imvp, nil |
| 237 |
case svp != nil && smvp != nil: |
| 238 |
return *svp > *smvp, nil |
| 239 |
case fvp != nil && fmvp != nil: |
| 240 |
return *fvp > *fmvp, nil |
| 241 |
} |
| 242 |
case "<=", "le": |
| 243 |
switch { |
| 244 |
case ivp != nil && imvp != nil: |
| 245 |
return *ivp <= *imvp, nil |
| 246 |
case svp != nil && smvp != nil: |
| 247 |
return *svp <= *smvp, nil |
| 248 |
case fvp != nil && fmvp != nil: |
| 249 |
return *fvp <= *fmvp, nil |
| 250 |
} |
| 251 |
case "<", "lt": |
| 252 |
switch { |
| 253 |
case ivp != nil && imvp != nil: |
| 254 |
return *ivp < *imvp, nil |
| 255 |
case svp != nil && smvp != nil: |
| 256 |
return *svp < *smvp, nil |
| 257 |
case fvp != nil && fmvp != nil: |
| 258 |
return *fvp < *fmvp, nil |
| 259 |
} |
| 260 |
case "in", "not in": |
| 261 |
var r bool |
| 262 |
switch { |
| 263 |
case ivp != nil && len(ima) > 0: |
| 264 |
r, _ = ns.In(ima, *ivp) |
| 265 |
case fvp != nil && len(fma) > 0: |
| 266 |
r, _ = ns.In(fma, *fvp) |
| 267 |
case svp != nil: |
| 268 |
if len(sma) > 0 { |
| 269 |
r, _ = ns.In(sma, *svp) |
| 270 |
} else if smvp != nil { |
| 271 |
r, _ = ns.In(*smvp, *svp) |
| 272 |
} |
| 273 |
default: |
| 274 |
return false, nil |
| 275 |
} |
| 276 |
if op == "not in" { |
| 277 |
return !r, nil |
| 278 |
} |
| 279 |
return r, nil |
| 280 |
case "intersect": |
| 281 |
r, err := ns.Intersect(slv, slmv) |
| 282 |
if err != nil { |
| 283 |
return false, err |
| 284 |
} |
| 285 |
|
| 286 |
if reflect.TypeOf(r).Kind() == reflect.Slice { |
| 287 |
s := reflect.ValueOf(r) |
| 288 |
|
| 289 |
if s.Len() > 0 { |
| 290 |
return true, nil |
| 291 |
} |
| 292 |
return false, nil |
| 293 |
} |
| 294 |
return false, errors.New("invalid intersect values") |
| 295 |
case "like": |
| 296 |
if svp != nil && smvp != nil { |
| 297 |
re, err := hstrings.GetOrCompileRegexp(*smvp) |
| 298 |
if err != nil { |
| 299 |
return false, err |
| 300 |
} |
| 301 |
if re.MatchString(*svp) { |
| 302 |
return true, nil |
| 303 |
} |
| 304 |
return false, nil |
| 305 |
} |
| 306 |
default: |
| 307 |
return false, errors.New("no such operator") |
| 308 |
} |
| 309 |
return false, nil |
| 310 |
} |
| 311 |
|
| 312 |
func evaluateSubElem(ctx, obj reflect.Value, elemName string) (reflect.Value, error) { |
| 313 |
if !obj.IsValid() { |
| 314 |
return zero, errors.New("can't evaluate an invalid value") |
| 315 |
} |
| 316 |
|
| 317 |
typ := obj.Type() |
| 318 |
obj, isNil := hreflect.Indirect(obj) |
| 319 |
|
| 320 |
// first, check whether obj has a method. In this case, obj is |
| 321 |
// a struct or its pointer. If obj is a struct, |
| 322 |
// to check all T and *T method, use obj pointer type Value |
| 323 |
objPtr := obj |
| 324 |
if !hreflect.IsInterfaceOrPointer(objPtr.Kind()) && objPtr.CanAddr() { |
| 325 |
objPtr = objPtr.Addr() |
| 326 |
} |
| 327 |
|
| 328 |
mt := hreflect.GetMethodByNameForType(objPtr.Type(), elemName) |
| 329 |
if mt.Func.IsValid() { |
| 330 |
// Receiver is the first argument. |
| 331 |
args := []reflect.Value{objPtr} |
| 332 |
num := mt.Type.NumIn() |
| 333 |
maxNumIn := 1 |
| 334 |
if num > 1 && hreflect.IsContextType(mt.Type.In(1)) { |
| 335 |
args = append(args, ctx) |
| 336 |
maxNumIn = 2 |
| 337 |
} |
| 338 |
|
| 339 |
switch { |
| 340 |
case mt.PkgPath != "": |
| 341 |
return zero, fmt.Errorf("%s is an unexported method of type %s", elemName, typ) |
| 342 |
case mt.Type.NumIn() > maxNumIn: |
| 343 |
return zero, fmt.Errorf("%s is a method of type %s but requires more than %d parameter", elemName, typ, maxNumIn) |
| 344 |
case mt.Type.NumOut() == 0: |
| 345 |
return zero, fmt.Errorf("%s is a method of type %s but returns no output", elemName, typ) |
| 346 |
case mt.Type.NumOut() > 2: |
| 347 |
return zero, fmt.Errorf("%s is a method of type %s but returns more than 2 outputs", elemName, typ) |
| 348 |
case mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType): |
| 349 |
return zero, fmt.Errorf("%s is a method of type %s but only returns an error type", elemName, typ) |
| 350 |
case mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType): |
| 351 |
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) |
| 352 |
} |
| 353 |
res := mt.Func.Call(args) |
| 354 |
if len(res) == 2 && !res[1].IsNil() { |
| 355 |
return zero, fmt.Errorf("error at calling a method %s of type %s: %s", elemName, typ, res[1].Interface().(error)) |
| 356 |
} |
| 357 |
return res[0], nil |
| 358 |
} |
| 359 |
|
| 360 |
// elemName isn't a method so next start to check whether it is |
| 361 |
// a struct field or a map value. In both cases, it mustn't be |
| 362 |
// a nil value |
| 363 |
if isNil { |
| 364 |
return zero, fmt.Errorf("can't evaluate a nil pointer of type %s by a struct field or map key name %s", typ, elemName) |
| 365 |
} |
| 366 |
obj = reflect.Indirect(obj) |
| 367 |
switch obj.Kind() { |
| 368 |
case reflect.Struct: |
| 369 |
ft, ok := obj.Type().FieldByName(elemName) |
| 370 |
if ok { |
| 371 |
if ft.PkgPath != "" && !ft.Anonymous { |
| 372 |
return zero, fmt.Errorf("%s is an unexported field of struct type %s", elemName, typ) |
| 373 |
} |
| 374 |
return obj.FieldByIndex(ft.Index), nil |
| 375 |
} |
| 376 |
return zero, fmt.Errorf("%s isn't a field of struct type %s", elemName, typ) |
| 377 |
case reflect.Map: |
| 378 |
kv := reflect.ValueOf(elemName) |
| 379 |
if kv.Type().AssignableTo(obj.Type().Key()) { |
| 380 |
return obj.MapIndex(kv), nil |
| 381 |
} |
| 382 |
return zero, fmt.Errorf("%s isn't a key of map type %s", elemName, typ) |
| 383 |
} |
| 384 |
return zero, fmt.Errorf("%s is neither a struct field, a method nor a map element of type %s", elemName, typ) |
| 385 |
} |
| 386 |
|
| 387 |
// parseWhereArgs parses the end arguments to the where function. Return a |
| 388 |
// match value and an operator, if one is defined. |
| 389 |
func parseWhereArgs(args ...any) (mv reflect.Value, op string, err error) { |
| 390 |
switch len(args) { |
| 391 |
case 1: |
| 392 |
mv = reflect.ValueOf(args[0]) |
| 393 |
case 2: |
| 394 |
var ok bool |
| 395 |
if op, ok = args[0].(string); !ok { |
| 396 |
err = errors.New("operator argument must be string type") |
| 397 |
return |
| 398 |
} |
| 399 |
op = strings.TrimSpace(strings.ToLower(op)) |
| 400 |
mv = reflect.ValueOf(args[1]) |
| 401 |
default: |
| 402 |
err = errors.New("can't evaluate the array by no match argument or more than or equal to two arguments") |
| 403 |
} |
| 404 |
return |
| 405 |
} |
| 406 |
|
| 407 |
// elemResolver resolves a sub-element from a reflect.Value. |
| 408 |
// Built once before the loop to avoid repeated type checks and reflect.ValueOf allocations. |
| 409 |
type elemResolver func(reflect.Value) (reflect.Value, error) |
| 410 |
|
| 411 |
// newElemResolver returns a resolver optimized for the given element type and path. |
| 412 |
// Returns nil if optimization isn't possible, in which case the caller should |
| 413 |
// fall back to evaluateSubElem. |
| 414 |
func (ns *Namespace) newElemResolver(ctxv reflect.Value, elemType reflect.Type, path []string) elemResolver { |
| 415 |
if elemType.Kind() == reflect.Interface { |
| 416 |
if len(path) != 1 { |
| 417 |
return nil |
| 418 |
} |
| 419 |
return ns.newInterfaceMethodResolver(ctxv, elemType, path[0]) |
| 420 |
} |
| 421 |
|
| 422 |
baseType := elemType |
| 423 |
isPtr := baseType.Kind() == reflect.Pointer |
| 424 |
if isPtr { |
| 425 |
baseType = baseType.Elem() |
| 426 |
} |
| 427 |
|
| 428 |
if baseType == reflect.TypeFor[hmaps.Params]() { |
| 429 |
return func(v reflect.Value) (reflect.Value, error) { |
| 430 |
if isPtr { |
| 431 |
if v.IsNil() { |
| 432 |
return zero, nil |
| 433 |
} |
| 434 |
v = v.Elem() |
| 435 |
} |
| 436 |
params := v.Interface().(hmaps.Params) |
| 437 |
return reflect.ValueOf(params.GetNested(path...)), nil |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if len(path) != 1 { |
| 442 |
return nil |
| 443 |
} |
| 444 |
name := path[0] |
| 445 |
|
| 446 |
// Check for method first, matching evaluateSubElem order. |
| 447 |
ptrType := baseType |
| 448 |
if !hreflect.IsInterfaceOrPointer(ptrType.Kind()) { |
| 449 |
ptrType = reflect.PointerTo(baseType) |
| 450 |
} |
| 451 |
mt := hreflect.GetMethodByNameForType(ptrType, name) |
| 452 |
if mt.Func.IsValid() { |
| 453 |
return ns.newMethodResolver(ctxv, elemType, mt) |
| 454 |
} |
| 455 |
|
| 456 |
switch baseType.Kind() { |
| 457 |
case reflect.Map: |
| 458 |
if baseType.Key().Kind() == reflect.String { |
| 459 |
kv := reflect.ValueOf(name) |
| 460 |
return func(v reflect.Value) (reflect.Value, error) { |
| 461 |
if isPtr { |
| 462 |
if v.IsNil() { |
| 463 |
return zero, nil |
| 464 |
} |
| 465 |
v = v.Elem() |
| 466 |
} |
| 467 |
return v.MapIndex(kv), nil |
| 468 |
} |
| 469 |
} |
| 470 |
case reflect.Struct: |
| 471 |
ft, ok := baseType.FieldByName(name) |
| 472 |
if ok { |
| 473 |
if ft.PkgPath != "" && !ft.Anonymous { |
| 474 |
return func(v reflect.Value) (reflect.Value, error) { |
| 475 |
return zero, fmt.Errorf("%s is an unexported field of struct type %s", name, elemType) |
| 476 |
} |
| 477 |
} |
| 478 |
idx := ft.Index |
| 479 |
return func(v reflect.Value) (reflect.Value, error) { |
| 480 |
if isPtr { |
| 481 |
if v.IsNil() { |
| 482 |
return zero, nil |
| 483 |
} |
| 484 |
v = v.Elem() |
| 485 |
} |
| 486 |
return v.FieldByIndex(idx), nil |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
return nil |
| 492 |
} |
| 493 |
|
| 494 |
func (ns *Namespace) newMethodResolver(ctxv reflect.Value, elemType reflect.Type, mt reflect.Method) elemResolver { |
| 495 |
if mt.PkgPath != "" { |
| 496 |
return func(v reflect.Value) (reflect.Value, error) { |
| 497 |
return zero, fmt.Errorf("%s is an unexported method of type %s", mt.Name, elemType) |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
numIn := mt.Type.NumIn() |
| 502 |
maxNumIn := 1 |
| 503 |
needsCtx := numIn > 1 && hreflect.IsContextType(mt.Type.In(1)) |
| 504 |
if needsCtx { |
| 505 |
maxNumIn = 2 |
| 506 |
} |
| 507 |
|
| 508 |
switch { |
| 509 |
case mt.Type.NumIn() > maxNumIn: |
| 510 |
return nil |
| 511 |
case mt.Type.NumOut() == 0: |
| 512 |
return nil |
| 513 |
case mt.Type.NumOut() > 2: |
| 514 |
return nil |
| 515 |
case mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType): |
| 516 |
return nil |
| 517 |
case mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType): |
| 518 |
return nil |
| 519 |
} |
| 520 |
|
| 521 |
fn := mt.Func |
| 522 |
hasErrOut := mt.Type.NumOut() == 2 |
| 523 |
isPtr := elemType.Kind() == reflect.Pointer |
| 524 |
|
| 525 |
var callArgs []reflect.Value |
| 526 |
if needsCtx { |
| 527 |
callArgs = make([]reflect.Value, 2) |
| 528 |
callArgs[1] = ctxv |
| 529 |
} else { |
| 530 |
callArgs = make([]reflect.Value, 1) |
| 531 |
} |
| 532 |
|
| 533 |
return func(v reflect.Value) (reflect.Value, error) { |
| 534 |
if isPtr && v.IsNil() { |
| 535 |
return zero, nil |
| 536 |
} |
| 537 |
recv := v |
| 538 |
if !isPtr && !hreflect.IsInterfaceOrPointer(recv.Kind()) && recv.CanAddr() { |
| 539 |
recv = recv.Addr() |
| 540 |
} |
| 541 |
callArgs[0] = recv |
| 542 |
res := fn.Call(callArgs) |
| 543 |
if hasErrOut && !res[1].IsNil() { |
| 544 |
return zero, res[1].Interface().(error) |
| 545 |
} |
| 546 |
return res[0], nil |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
// newInterfaceMethodResolver returns a method resolver or nil if not possible, in which the caller should fall back to evaluateSubElem. |
| 551 |
func (ns *Namespace) newInterfaceMethodResolver(ctxv reflect.Value, ifaceType reflect.Type, name string) elemResolver { |
| 552 |
mt, ok := ifaceType.MethodByName(name) |
| 553 |
if !ok { |
| 554 |
return nil |
| 555 |
} |
| 556 |
|
| 557 |
// For interface methods, Type does not include the receiver. |
| 558 |
mType := mt.Type |
| 559 |
numIn := mType.NumIn() |
| 560 |
maxNumIn := 0 |
| 561 |
needsCtx := numIn > 0 && hreflect.IsContextType(mType.In(0)) |
| 562 |
if needsCtx { |
| 563 |
maxNumIn = 1 |
| 564 |
} |
| 565 |
|
| 566 |
switch { |
| 567 |
case mType.NumIn() > maxNumIn: |
| 568 |
return nil |
| 569 |
case mType.NumOut() == 0: |
| 570 |
return nil |
| 571 |
case mType.NumOut() > 2: |
| 572 |
return nil |
| 573 |
case mType.NumOut() == 1 && mType.Out(0).Implements(errorType): |
| 574 |
return nil |
| 575 |
case mType.NumOut() == 2 && !mType.Out(1).Implements(errorType): |
| 576 |
return nil |
| 577 |
} |
| 578 |
|
| 579 |
index := mt.Index |
| 580 |
hasErrOut := mType.NumOut() == 2 |
| 581 |
|
| 582 |
var callArgs []reflect.Value |
| 583 |
if needsCtx { |
| 584 |
callArgs = []reflect.Value{ctxv} |
| 585 |
} |
| 586 |
|
| 587 |
return func(v reflect.Value) (reflect.Value, error) { |
| 588 |
if v.IsNil() { |
| 589 |
return zero, nil |
| 590 |
} |
| 591 |
res := v.Method(index).Call(callArgs) |
| 592 |
if hasErrOut && !res[1].IsNil() { |
| 593 |
return zero, res[1].Interface().(error) |
| 594 |
} |
| 595 |
return res[0], nil |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
// checkWhereArray handles the where-matching logic when the seqv value is an |
| 600 |
// Array or Slice. |
| 601 |
func (ns *Namespace) checkWhereArray(ctxv, seqv, kv, mv reflect.Value, path []string, op string) (any, error) { |
| 602 |
rv := reflect.MakeSlice(seqv.Type(), 0, 0) |
| 603 |
|
| 604 |
var resolve elemResolver |
| 605 |
if kv.Kind() == reflect.String && len(path) > 0 { |
| 606 |
resolve = ns.newElemResolver(ctxv, seqv.Type().Elem(), path) |
| 607 |
} |
| 608 |
|
| 609 |
for i := range seqv.Len() { |
| 610 |
var vvv reflect.Value |
| 611 |
rvv := seqv.Index(i) |
| 612 |
|
| 613 |
if resolve != nil { |
| 614 |
var err error |
| 615 |
vvv, err = resolve(rvv) |
| 616 |
if err != nil { |
| 617 |
return nil, err |
| 618 |
} |
| 619 |
} else if kv.Kind() == reflect.String { |
| 620 |
if params, ok := rvv.Interface().(hmaps.Params); ok { |
| 621 |
vvv = reflect.ValueOf(params.GetNested(path...)) |
| 622 |
} else { |
| 623 |
vvv = rvv |
| 624 |
for i, elemName := range path { |
| 625 |
var err error |
| 626 |
vvv, err = evaluateSubElem(ctxv, vvv, elemName) |
| 627 |
if err != nil { |
| 628 |
continue |
| 629 |
} |
| 630 |
|
| 631 |
if i < len(path)-1 && vvv.IsValid() { |
| 632 |
if params, ok := vvv.Interface().(hmaps.Params); ok { |
| 633 |
vvv = reflect.ValueOf(params.GetNested(path[i+1:]...)) |
| 634 |
break |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
} else { |
| 640 |
vv, _ := hreflect.Indirect(rvv) |
| 641 |
if vv.Kind() == reflect.Map && kv.Type().AssignableTo(vv.Type().Key()) { |
| 642 |
vvv = vv.MapIndex(kv) |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
if ok, err := ns.checkCondition(vvv, mv, op); ok { |
| 647 |
rv = reflect.Append(rv, rvv) |
| 648 |
} else if err != nil { |
| 649 |
return nil, err |
| 650 |
} |
| 651 |
} |
| 652 |
return rv.Interface(), nil |
| 653 |
} |
| 654 |
|
| 655 |
// checkWhereMap handles the where-matching logic when the seqv value is a Map. |
| 656 |
func (ns *Namespace) checkWhereMap(ctxv, seqv, kv, mv reflect.Value, path []string, op string) (any, error) { |
| 657 |
rv := reflect.MakeMap(seqv.Type()) |
| 658 |
k := reflect.New(seqv.Type().Key()).Elem() |
| 659 |
elemv := reflect.New(seqv.Type().Elem()).Elem() |
| 660 |
iter := seqv.MapRange() |
| 661 |
for iter.Next() { |
| 662 |
k.SetIterKey(iter) |
| 663 |
elemv.SetIterValue(iter) |
| 664 |
switch elemv.Kind() { |
| 665 |
case reflect.Array, reflect.Slice: |
| 666 |
r, err := ns.checkWhereArray(ctxv, elemv, kv, mv, path, op) |
| 667 |
if err != nil { |
| 668 |
return nil, err |
| 669 |
} |
| 670 |
|
| 671 |
switch rr := reflect.ValueOf(r); rr.Kind() { |
| 672 |
case reflect.Slice: |
| 673 |
if rr.Len() > 0 { |
| 674 |
rv.SetMapIndex(k, elemv) |
| 675 |
} |
| 676 |
} |
| 677 |
case reflect.Interface: |
| 678 |
elemvv, isNil := hreflect.Indirect(elemv) |
| 679 |
if isNil { |
| 680 |
continue |
| 681 |
} |
| 682 |
|
| 683 |
switch elemvv.Kind() { |
| 684 |
case reflect.Array, reflect.Slice: |
| 685 |
r, err := ns.checkWhereArray(ctxv, elemvv, kv, mv, path, op) |
| 686 |
if err != nil { |
| 687 |
return nil, err |
| 688 |
} |
| 689 |
|
| 690 |
switch rr := reflect.ValueOf(r); rr.Kind() { |
| 691 |
case reflect.Slice: |
| 692 |
if rr.Len() > 0 { |
| 693 |
rv.SetMapIndex(k, elemv) |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
} |
| 699 |
return rv.Interface(), nil |
| 700 |
} |
| 701 |
|
| 702 |
// toString returns the string value if possible, "" if not. |
| 703 |
|
| 704 |
// tryEq returns the equality of v and mv via the compare.Eqer interface |
| 705 |
// if either side implements it. The second return value reports whether |
| 706 |
// such a comparison was possible. |
| 707 |
func tryEq(v, mv reflect.Value) (bool, bool) { |
| 708 |
if !v.CanInterface() || !mv.CanInterface() { |
| 709 |
return false, false |
| 710 |
} |
| 711 |
vi, mvi := v.Interface(), mv.Interface() |
| 712 |
if e, ok := vi.(compare.Eqer); ok { |
| 713 |
return e.Eq(mvi), true |
| 714 |
} |
| 715 |
if e, ok := mvi.(compare.Eqer); ok { |
| 716 |
return e.Eq(vi), true |
| 717 |
} |
| 718 |
return false, false |
| 719 |
} |
| 720 |
|
| 721 |
func (ns *Namespace) toTimeUnix(v reflect.Value) int64 { |
| 722 |
t, ok := hreflect.AsTime(v, ns.loc) |
| 723 |
if !ok { |
| 724 |
panic("coding error: argument must be time.Time type reflect Value") |
| 725 |
} |
| 726 |
return t.Unix() |
| 727 |
} |