| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package interpreter |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/google/cel-go/common/containers" |
| 22 |
"github.com/google/cel-go/common/types" |
| 23 |
"github.com/google/cel-go/common/types/ref" |
| 24 |
"github.com/google/cel-go/common/types/traits" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
type AttributeFactory interface { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
AbsoluteAttribute(id int64, names ...string) NamespacedAttribute |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
ConditionalAttribute(id int64, expr Interpretable, t, f Attribute) Attribute |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
MaybeAttribute(id int64, name string) Attribute |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
RelativeAttribute(id int64, operand Interpretable) Attribute |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
NewQualifier(objType *types.Type, qualID int64, val any, opt bool) (Qualifier, error) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
type Qualifier interface { |
| 68 |
|
| 69 |
ID() int64 |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
IsOptional() bool |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
Qualify(vars Activation, obj any) (any, error) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
type ConstantQualifier interface { |
| 93 |
Qualifier |
| 94 |
|
| 95 |
|
| 96 |
Value() ref.Val |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
type Attribute interface { |
| 102 |
Qualifier |
| 103 |
|
| 104 |
|
| 105 |
AddQualifier(Qualifier) (Attribute, error) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
Resolve(Activation) (any, error) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
type NamespacedAttribute interface { |
| 119 |
Attribute |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
CandidateVariableNames() []string |
| 124 |
|
| 125 |
|
| 126 |
Qualifiers() []Qualifier |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
type AttrFactoryOption func(*attrFactory) *attrFactory |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func EnableErrorOnBadPresenceTest(value bool) AttrFactoryOption { |
| 135 |
return func(fac *attrFactory) *attrFactory { |
| 136 |
fac.errorOnBadPresenceTest = value |
| 137 |
return fac |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
func NewAttributeFactory(cont *containers.Container, a types.Adapter, p types.Provider, opts ...AttrFactoryOption) AttributeFactory { |
| 145 |
fac := &attrFactory{ |
| 146 |
container: cont, |
| 147 |
adapter: a, |
| 148 |
provider: p, |
| 149 |
} |
| 150 |
for _, o := range opts { |
| 151 |
fac = o(fac) |
| 152 |
} |
| 153 |
return fac |
| 154 |
} |
| 155 |
|
| 156 |
type attrFactory struct { |
| 157 |
container *containers.Container |
| 158 |
adapter types.Adapter |
| 159 |
provider types.Provider |
| 160 |
|
| 161 |
errorOnBadPresenceTest bool |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func (r *attrFactory) AbsoluteAttribute(id int64, names ...string) NamespacedAttribute { |
| 169 |
disambiguateNames := false |
| 170 |
for idx, name := range names { |
| 171 |
if strings.HasPrefix(name, ".") { |
| 172 |
disambiguateNames = true |
| 173 |
names[idx] = strings.TrimPrefix(name, ".") |
| 174 |
} |
| 175 |
} |
| 176 |
return &absoluteAttribute{ |
| 177 |
id: id, |
| 178 |
namespaceNames: names, |
| 179 |
disambiguateNames: disambiguateNames, |
| 180 |
qualifiers: []Qualifier{}, |
| 181 |
adapter: r.adapter, |
| 182 |
provider: r.provider, |
| 183 |
fac: r, |
| 184 |
errorOnBadPresenceTest: r.errorOnBadPresenceTest, |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
func (r *attrFactory) ConditionalAttribute(id int64, expr Interpretable, t, f Attribute) Attribute { |
| 191 |
return &conditionalAttribute{ |
| 192 |
id: id, |
| 193 |
expr: expr, |
| 194 |
truthy: t, |
| 195 |
falsy: f, |
| 196 |
adapter: r.adapter, |
| 197 |
fac: r, |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
func (r *attrFactory) MaybeAttribute(id int64, name string) Attribute { |
| 204 |
var names []string |
| 205 |
|
| 206 |
|
| 207 |
if strings.HasPrefix(name, ".") { |
| 208 |
names = append(names, name) |
| 209 |
} else { |
| 210 |
|
| 211 |
names = r.container.ResolveCandidateNames(name) |
| 212 |
} |
| 213 |
return &maybeAttribute{ |
| 214 |
id: id, |
| 215 |
attrs: []NamespacedAttribute{ |
| 216 |
r.AbsoluteAttribute(id, names...), |
| 217 |
}, |
| 218 |
adapter: r.adapter, |
| 219 |
provider: r.provider, |
| 220 |
fac: r, |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (r *attrFactory) RelativeAttribute(id int64, operand Interpretable) Attribute { |
| 226 |
return &relativeAttribute{ |
| 227 |
id: id, |
| 228 |
operand: operand, |
| 229 |
qualifiers: []Qualifier{}, |
| 230 |
adapter: r.adapter, |
| 231 |
fac: r, |
| 232 |
errorOnBadPresenceTest: r.errorOnBadPresenceTest, |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
func (r *attrFactory) NewQualifier(objType *types.Type, qualID int64, val any, opt bool) (Qualifier, error) { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
str, isStr := val.(string) |
| 242 |
if isStr && objType != nil && objType.Kind() == types.StructKind { |
| 243 |
ft, found := r.provider.FindStructFieldType(objType.TypeName(), str) |
| 244 |
if found && ft.IsSet != nil && ft.GetFrom != nil { |
| 245 |
return &fieldQualifier{ |
| 246 |
id: qualID, |
| 247 |
Name: str, |
| 248 |
FieldType: ft, |
| 249 |
adapter: r.adapter, |
| 250 |
optional: opt, |
| 251 |
}, nil |
| 252 |
} |
| 253 |
} |
| 254 |
return newQualifier(r.adapter, qualID, val, opt, r.errorOnBadPresenceTest) |
| 255 |
} |
| 256 |
|
| 257 |
type absoluteAttribute struct { |
| 258 |
id int64 |
| 259 |
|
| 260 |
|
| 261 |
namespaceNames []string |
| 262 |
|
| 263 |
disambiguateNames bool |
| 264 |
|
| 265 |
qualifiers []Qualifier |
| 266 |
adapter types.Adapter |
| 267 |
provider types.Provider |
| 268 |
fac AttributeFactory |
| 269 |
|
| 270 |
errorOnBadPresenceTest bool |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
func (a *absoluteAttribute) ID() int64 { |
| 275 |
qualCount := len(a.qualifiers) |
| 276 |
if qualCount == 0 { |
| 277 |
return a.id |
| 278 |
} |
| 279 |
return a.qualifiers[qualCount-1].ID() |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
func (a *absoluteAttribute) IsOptional() bool { |
| 286 |
return false |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
func (a *absoluteAttribute) AddQualifier(qual Qualifier) (Attribute, error) { |
| 291 |
a.qualifiers = append(a.qualifiers, qual) |
| 292 |
return a, nil |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
func (a *absoluteAttribute) CandidateVariableNames() []string { |
| 297 |
return a.namespaceNames |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
func (a *absoluteAttribute) Qualifiers() []Qualifier { |
| 302 |
return a.qualifiers |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
func (a *absoluteAttribute) Qualify(vars Activation, obj any) (any, error) { |
| 307 |
return attrQualify(a.fac, vars, obj, a) |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
func (a *absoluteAttribute) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 312 |
return attrQualifyIfPresent(a.fac, vars, obj, a, presenceOnly) |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
func (a *absoluteAttribute) String() string { |
| 317 |
return fmt.Sprintf("id: %v, names: %v", a.id, a.namespaceNames) |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
func (a *absoluteAttribute) Resolve(vars Activation) (any, error) { |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
var inputVars Activation |
| 334 |
if a.disambiguateNames { |
| 335 |
inputVars = vars |
| 336 |
wrapped, ok := inputVars.(activationWrapper) |
| 337 |
for ok { |
| 338 |
inputVars = wrapped.Unwrap() |
| 339 |
wrapped, ok = inputVars.(activationWrapper) |
| 340 |
} |
| 341 |
} |
| 342 |
for _, nm := range a.namespaceNames { |
| 343 |
|
| 344 |
|
| 345 |
v := vars |
| 346 |
if a.disambiguateNames { |
| 347 |
v = inputVars |
| 348 |
} |
| 349 |
obj, found := v.ResolveName(nm) |
| 350 |
if found { |
| 351 |
if celErr, ok := obj.(*types.Err); ok { |
| 352 |
return nil, celErr.Unwrap() |
| 353 |
} |
| 354 |
obj, isOpt, err := applyQualifiers(v, obj, a.qualifiers) |
| 355 |
if err != nil { |
| 356 |
return nil, err |
| 357 |
} |
| 358 |
if isOpt { |
| 359 |
val := a.adapter.NativeToValue(obj) |
| 360 |
if types.IsUnknown(val) { |
| 361 |
return val, nil |
| 362 |
} |
| 363 |
return types.OptionalOf(val), nil |
| 364 |
} |
| 365 |
return obj, nil |
| 366 |
} |
| 367 |
|
| 368 |
typ, found := a.provider.FindIdent(nm) |
| 369 |
if found { |
| 370 |
if len(a.qualifiers) == 0 { |
| 371 |
return typ, nil |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
var attrNames strings.Builder |
| 376 |
for i, nm := range a.namespaceNames { |
| 377 |
if i != 0 { |
| 378 |
attrNames.WriteString(", ") |
| 379 |
} |
| 380 |
attrNames.WriteString(nm) |
| 381 |
} |
| 382 |
return nil, missingAttribute(attrNames.String()) |
| 383 |
} |
| 384 |
|
| 385 |
type conditionalAttribute struct { |
| 386 |
id int64 |
| 387 |
expr Interpretable |
| 388 |
truthy Attribute |
| 389 |
falsy Attribute |
| 390 |
adapter types.Adapter |
| 391 |
fac AttributeFactory |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
func (a *conditionalAttribute) ID() int64 { |
| 396 |
|
| 397 |
if a.truthy.ID() == a.falsy.ID() { |
| 398 |
return a.truthy.ID() |
| 399 |
} |
| 400 |
|
| 401 |
return a.id |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
func (a *conditionalAttribute) IsOptional() bool { |
| 408 |
return false |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
func (a *conditionalAttribute) AddQualifier(qual Qualifier) (Attribute, error) { |
| 414 |
_, err := a.truthy.AddQualifier(qual) |
| 415 |
if err != nil { |
| 416 |
return nil, err |
| 417 |
} |
| 418 |
_, err = a.falsy.AddQualifier(qual) |
| 419 |
if err != nil { |
| 420 |
return nil, err |
| 421 |
} |
| 422 |
return a, nil |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
func (a *conditionalAttribute) Qualify(vars Activation, obj any) (any, error) { |
| 427 |
return attrQualify(a.fac, vars, obj, a) |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
func (a *conditionalAttribute) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 432 |
return attrQualifyIfPresent(a.fac, vars, obj, a, presenceOnly) |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
func (a *conditionalAttribute) Resolve(vars Activation) (any, error) { |
| 437 |
val := a.expr.Eval(vars) |
| 438 |
if val == types.True { |
| 439 |
return a.truthy.Resolve(vars) |
| 440 |
} |
| 441 |
if val == types.False { |
| 442 |
return a.falsy.Resolve(vars) |
| 443 |
} |
| 444 |
if types.IsUnknown(val) { |
| 445 |
return val, nil |
| 446 |
} |
| 447 |
return nil, types.MaybeNoSuchOverloadErr(val).(*types.Err) |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
func (a *conditionalAttribute) String() string { |
| 452 |
return fmt.Sprintf("id: %v, truthy attribute: %v, falsy attribute: %v", a.id, a.truthy, a.falsy) |
| 453 |
} |
| 454 |
|
| 455 |
type maybeAttribute struct { |
| 456 |
id int64 |
| 457 |
attrs []NamespacedAttribute |
| 458 |
adapter types.Adapter |
| 459 |
provider types.Provider |
| 460 |
fac AttributeFactory |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
func (a *maybeAttribute) ID() int64 { |
| 465 |
return a.attrs[0].ID() |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
func (a *maybeAttribute) IsOptional() bool { |
| 472 |
return false |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
func (a *maybeAttribute) AddQualifier(qual Qualifier) (Attribute, error) { |
| 500 |
str := "" |
| 501 |
isStr := false |
| 502 |
cq, isConst := qual.(ConstantQualifier) |
| 503 |
if isConst { |
| 504 |
str, isStr = cq.Value().Value().(string) |
| 505 |
} |
| 506 |
var augmentedNames []string |
| 507 |
|
| 508 |
for _, attr := range a.attrs { |
| 509 |
if isStr && len(attr.Qualifiers()) == 0 { |
| 510 |
candidateVars := attr.CandidateVariableNames() |
| 511 |
augmentedNames = make([]string, len(candidateVars)) |
| 512 |
for i, name := range candidateVars { |
| 513 |
augmentedNames[i] = fmt.Sprintf("%s.%s", name, str) |
| 514 |
} |
| 515 |
} |
| 516 |
_, err := attr.AddQualifier(qual) |
| 517 |
if err != nil { |
| 518 |
return nil, err |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
if len(augmentedNames) != 0 { |
| 523 |
a.attrs = append([]NamespacedAttribute{a.fac.AbsoluteAttribute(qual.ID(), augmentedNames...)}, a.attrs...) |
| 524 |
} |
| 525 |
return a, nil |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
func (a *maybeAttribute) Qualify(vars Activation, obj any) (any, error) { |
| 530 |
return attrQualify(a.fac, vars, obj, a) |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
func (a *maybeAttribute) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 535 |
return attrQualifyIfPresent(a.fac, vars, obj, a, presenceOnly) |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
func (a *maybeAttribute) Resolve(vars Activation) (any, error) { |
| 541 |
var maybeErr error |
| 542 |
for _, attr := range a.attrs { |
| 543 |
obj, err := attr.Resolve(vars) |
| 544 |
|
| 545 |
if err != nil { |
| 546 |
resErr, ok := err.(*resolutionError) |
| 547 |
if !ok { |
| 548 |
return nil, err |
| 549 |
} |
| 550 |
|
| 551 |
if !resErr.isMissingAttribute() { |
| 552 |
return nil, err |
| 553 |
} |
| 554 |
|
| 555 |
if maybeErr == nil { |
| 556 |
maybeErr = resErr |
| 557 |
} |
| 558 |
|
| 559 |
continue |
| 560 |
} |
| 561 |
return obj, nil |
| 562 |
} |
| 563 |
|
| 564 |
return nil, maybeErr |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
func (a *maybeAttribute) String() string { |
| 569 |
return fmt.Sprintf("id: %v, attributes: %v", a.id, a.attrs) |
| 570 |
} |
| 571 |
|
| 572 |
type relativeAttribute struct { |
| 573 |
id int64 |
| 574 |
operand Interpretable |
| 575 |
qualifiers []Qualifier |
| 576 |
adapter types.Adapter |
| 577 |
fac AttributeFactory |
| 578 |
|
| 579 |
errorOnBadPresenceTest bool |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
func (a *relativeAttribute) ID() int64 { |
| 584 |
qualCount := len(a.qualifiers) |
| 585 |
if qualCount == 0 { |
| 586 |
return a.id |
| 587 |
} |
| 588 |
return a.qualifiers[qualCount-1].ID() |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
func (a *relativeAttribute) IsOptional() bool { |
| 595 |
return false |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
func (a *relativeAttribute) AddQualifier(qual Qualifier) (Attribute, error) { |
| 600 |
a.qualifiers = append(a.qualifiers, qual) |
| 601 |
return a, nil |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
func (a *relativeAttribute) Qualify(vars Activation, obj any) (any, error) { |
| 606 |
return attrQualify(a.fac, vars, obj, a) |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
func (a *relativeAttribute) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 611 |
return attrQualifyIfPresent(a.fac, vars, obj, a, presenceOnly) |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
func (a *relativeAttribute) Resolve(vars Activation) (any, error) { |
| 616 |
|
| 617 |
v := a.operand.Eval(vars) |
| 618 |
if types.IsError(v) { |
| 619 |
return nil, v.(*types.Err) |
| 620 |
} |
| 621 |
if types.IsUnknown(v) { |
| 622 |
return v, nil |
| 623 |
} |
| 624 |
obj, isOpt, err := applyQualifiers(vars, v, a.qualifiers) |
| 625 |
if err != nil { |
| 626 |
return nil, err |
| 627 |
} |
| 628 |
if isOpt { |
| 629 |
val := a.adapter.NativeToValue(obj) |
| 630 |
if types.IsUnknown(val) { |
| 631 |
return val, nil |
| 632 |
} |
| 633 |
return types.OptionalOf(val), nil |
| 634 |
} |
| 635 |
return obj, nil |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
func (a *relativeAttribute) String() string { |
| 640 |
return fmt.Sprintf("id: %v, operand: %v", a.id, a.operand) |
| 641 |
} |
| 642 |
|
| 643 |
func newQualifier(adapter types.Adapter, id int64, v any, opt, errorOnBadPresenceTest bool) (Qualifier, error) { |
| 644 |
var qual Qualifier |
| 645 |
switch val := v.(type) { |
| 646 |
case Attribute: |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
return &attrQualifier{ |
| 652 |
id: id, |
| 653 |
Attribute: val, |
| 654 |
optional: opt, |
| 655 |
}, nil |
| 656 |
case string: |
| 657 |
qual = &stringQualifier{ |
| 658 |
id: id, |
| 659 |
value: val, |
| 660 |
celValue: types.String(val), |
| 661 |
adapter: adapter, |
| 662 |
optional: opt, |
| 663 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 664 |
} |
| 665 |
case int: |
| 666 |
qual = &intQualifier{ |
| 667 |
id: id, |
| 668 |
value: int64(val), |
| 669 |
celValue: types.Int(val), |
| 670 |
adapter: adapter, |
| 671 |
optional: opt, |
| 672 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 673 |
} |
| 674 |
case int32: |
| 675 |
qual = &intQualifier{ |
| 676 |
id: id, |
| 677 |
value: int64(val), |
| 678 |
celValue: types.Int(val), |
| 679 |
adapter: adapter, |
| 680 |
optional: opt, |
| 681 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 682 |
} |
| 683 |
case int64: |
| 684 |
qual = &intQualifier{ |
| 685 |
id: id, |
| 686 |
value: val, |
| 687 |
celValue: types.Int(val), |
| 688 |
adapter: adapter, |
| 689 |
optional: opt, |
| 690 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 691 |
} |
| 692 |
case uint: |
| 693 |
qual = &uintQualifier{ |
| 694 |
id: id, |
| 695 |
value: uint64(val), |
| 696 |
celValue: types.Uint(val), |
| 697 |
adapter: adapter, |
| 698 |
optional: opt, |
| 699 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 700 |
} |
| 701 |
case uint32: |
| 702 |
qual = &uintQualifier{ |
| 703 |
id: id, |
| 704 |
value: uint64(val), |
| 705 |
celValue: types.Uint(val), |
| 706 |
adapter: adapter, |
| 707 |
optional: opt, |
| 708 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 709 |
} |
| 710 |
case uint64: |
| 711 |
qual = &uintQualifier{ |
| 712 |
id: id, |
| 713 |
value: val, |
| 714 |
celValue: types.Uint(val), |
| 715 |
adapter: adapter, |
| 716 |
optional: opt, |
| 717 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 718 |
} |
| 719 |
case bool: |
| 720 |
qual = &boolQualifier{ |
| 721 |
id: id, |
| 722 |
value: val, |
| 723 |
celValue: types.Bool(val), |
| 724 |
adapter: adapter, |
| 725 |
optional: opt, |
| 726 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 727 |
} |
| 728 |
case float32: |
| 729 |
qual = &doubleQualifier{ |
| 730 |
id: id, |
| 731 |
value: float64(val), |
| 732 |
celValue: types.Double(val), |
| 733 |
adapter: adapter, |
| 734 |
optional: opt, |
| 735 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 736 |
} |
| 737 |
case float64: |
| 738 |
qual = &doubleQualifier{ |
| 739 |
id: id, |
| 740 |
value: val, |
| 741 |
celValue: types.Double(val), |
| 742 |
adapter: adapter, |
| 743 |
optional: opt, |
| 744 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 745 |
} |
| 746 |
case types.String: |
| 747 |
qual = &stringQualifier{ |
| 748 |
id: id, |
| 749 |
value: string(val), |
| 750 |
celValue: val, |
| 751 |
adapter: adapter, |
| 752 |
optional: opt, |
| 753 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 754 |
} |
| 755 |
case types.Int: |
| 756 |
qual = &intQualifier{ |
| 757 |
id: id, |
| 758 |
value: int64(val), |
| 759 |
celValue: val, |
| 760 |
adapter: adapter, |
| 761 |
optional: opt, |
| 762 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 763 |
} |
| 764 |
case types.Uint: |
| 765 |
qual = &uintQualifier{ |
| 766 |
id: id, |
| 767 |
value: uint64(val), |
| 768 |
celValue: val, |
| 769 |
adapter: adapter, |
| 770 |
optional: opt, |
| 771 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 772 |
} |
| 773 |
case types.Bool: |
| 774 |
qual = &boolQualifier{ |
| 775 |
id: id, |
| 776 |
value: bool(val), |
| 777 |
celValue: val, |
| 778 |
adapter: adapter, |
| 779 |
optional: opt, |
| 780 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 781 |
} |
| 782 |
case types.Double: |
| 783 |
qual = &doubleQualifier{ |
| 784 |
id: id, |
| 785 |
value: float64(val), |
| 786 |
celValue: val, |
| 787 |
adapter: adapter, |
| 788 |
optional: opt, |
| 789 |
errorOnBadPresenceTest: errorOnBadPresenceTest, |
| 790 |
} |
| 791 |
case *types.Unknown: |
| 792 |
qual = &unknownQualifier{id: id, value: val} |
| 793 |
default: |
| 794 |
if q, ok := v.(Qualifier); ok { |
| 795 |
return q, nil |
| 796 |
} |
| 797 |
return nil, fmt.Errorf("invalid qualifier type: %T", v) |
| 798 |
} |
| 799 |
return qual, nil |
| 800 |
} |
| 801 |
|
| 802 |
type attrQualifier struct { |
| 803 |
id int64 |
| 804 |
Attribute |
| 805 |
optional bool |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
func (q *attrQualifier) ID() int64 { |
| 811 |
return q.id |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
func (q *attrQualifier) IsOptional() bool { |
| 816 |
return q.optional |
| 817 |
} |
| 818 |
|
| 819 |
type stringQualifier struct { |
| 820 |
id int64 |
| 821 |
value string |
| 822 |
celValue ref.Val |
| 823 |
adapter types.Adapter |
| 824 |
optional bool |
| 825 |
errorOnBadPresenceTest bool |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
func (q *stringQualifier) ID() int64 { |
| 830 |
return q.id |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
func (q *stringQualifier) IsOptional() bool { |
| 835 |
return q.optional |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
func (q *stringQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 840 |
val, _, err := q.qualifyInternal(vars, obj, false, false) |
| 841 |
return val, err |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
func (q *stringQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 846 |
return q.qualifyInternal(vars, obj, true, presenceOnly) |
| 847 |
} |
| 848 |
|
| 849 |
func (q *stringQualifier) qualifyInternal(vars Activation, obj any, presenceTest, presenceOnly bool) (any, bool, error) { |
| 850 |
s := q.value |
| 851 |
switch o := obj.(type) { |
| 852 |
case map[string]any: |
| 853 |
obj, isKey := o[s] |
| 854 |
if isKey { |
| 855 |
return obj, true, nil |
| 856 |
} |
| 857 |
case map[string]string: |
| 858 |
obj, isKey := o[s] |
| 859 |
if isKey { |
| 860 |
return obj, true, nil |
| 861 |
} |
| 862 |
case map[string]int: |
| 863 |
obj, isKey := o[s] |
| 864 |
if isKey { |
| 865 |
return obj, true, nil |
| 866 |
} |
| 867 |
case map[string]int32: |
| 868 |
obj, isKey := o[s] |
| 869 |
if isKey { |
| 870 |
return obj, true, nil |
| 871 |
} |
| 872 |
case map[string]int64: |
| 873 |
obj, isKey := o[s] |
| 874 |
if isKey { |
| 875 |
return obj, true, nil |
| 876 |
} |
| 877 |
case map[string]uint: |
| 878 |
obj, isKey := o[s] |
| 879 |
if isKey { |
| 880 |
return obj, true, nil |
| 881 |
} |
| 882 |
case map[string]uint32: |
| 883 |
obj, isKey := o[s] |
| 884 |
if isKey { |
| 885 |
return obj, true, nil |
| 886 |
} |
| 887 |
case map[string]uint64: |
| 888 |
obj, isKey := o[s] |
| 889 |
if isKey { |
| 890 |
return obj, true, nil |
| 891 |
} |
| 892 |
case map[string]float32: |
| 893 |
obj, isKey := o[s] |
| 894 |
if isKey { |
| 895 |
return obj, true, nil |
| 896 |
} |
| 897 |
case map[string]float64: |
| 898 |
obj, isKey := o[s] |
| 899 |
if isKey { |
| 900 |
return obj, true, nil |
| 901 |
} |
| 902 |
case map[string]bool: |
| 903 |
obj, isKey := o[s] |
| 904 |
if isKey { |
| 905 |
return obj, true, nil |
| 906 |
} |
| 907 |
default: |
| 908 |
return refQualify(q.adapter, obj, q.celValue, presenceTest, presenceOnly, q.errorOnBadPresenceTest) |
| 909 |
} |
| 910 |
if presenceTest { |
| 911 |
return nil, false, nil |
| 912 |
} |
| 913 |
return nil, false, missingKey(q.celValue) |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
func (q *stringQualifier) Value() ref.Val { |
| 918 |
return q.celValue |
| 919 |
} |
| 920 |
|
| 921 |
type intQualifier struct { |
| 922 |
id int64 |
| 923 |
value int64 |
| 924 |
celValue ref.Val |
| 925 |
adapter types.Adapter |
| 926 |
optional bool |
| 927 |
errorOnBadPresenceTest bool |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
func (q *intQualifier) ID() int64 { |
| 932 |
return q.id |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
func (q *intQualifier) IsOptional() bool { |
| 937 |
return q.optional |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
func (q *intQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 942 |
val, _, err := q.qualifyInternal(vars, obj, false, false) |
| 943 |
return val, err |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
func (q *intQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 948 |
return q.qualifyInternal(vars, obj, true, presenceOnly) |
| 949 |
} |
| 950 |
|
| 951 |
func (q *intQualifier) qualifyInternal(vars Activation, obj any, presenceTest, presenceOnly bool) (any, bool, error) { |
| 952 |
i := q.value |
| 953 |
var isMap bool |
| 954 |
switch o := obj.(type) { |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
case map[int]any: |
| 960 |
isMap = true |
| 961 |
obj, isKey := o[int(i)] |
| 962 |
if isKey { |
| 963 |
return obj, true, nil |
| 964 |
} |
| 965 |
case map[int32]any: |
| 966 |
isMap = true |
| 967 |
obj, isKey := o[int32(i)] |
| 968 |
if isKey { |
| 969 |
return obj, true, nil |
| 970 |
} |
| 971 |
case map[int64]any: |
| 972 |
isMap = true |
| 973 |
obj, isKey := o[i] |
| 974 |
if isKey { |
| 975 |
return obj, true, nil |
| 976 |
} |
| 977 |
case []any: |
| 978 |
isIndex := i >= 0 && i < int64(len(o)) |
| 979 |
if isIndex { |
| 980 |
return o[i], true, nil |
| 981 |
} |
| 982 |
case []string: |
| 983 |
isIndex := i >= 0 && i < int64(len(o)) |
| 984 |
if isIndex { |
| 985 |
return o[i], true, nil |
| 986 |
} |
| 987 |
case []int: |
| 988 |
isIndex := i >= 0 && i < int64(len(o)) |
| 989 |
if isIndex { |
| 990 |
return o[i], true, nil |
| 991 |
} |
| 992 |
case []int32: |
| 993 |
isIndex := i >= 0 && i < int64(len(o)) |
| 994 |
if isIndex { |
| 995 |
return o[i], true, nil |
| 996 |
} |
| 997 |
case []int64: |
| 998 |
isIndex := i >= 0 && i < int64(len(o)) |
| 999 |
if isIndex { |
| 1000 |
return o[i], true, nil |
| 1001 |
} |
| 1002 |
case []uint: |
| 1003 |
isIndex := i >= 0 && i < int64(len(o)) |
| 1004 |
if isIndex { |
| 1005 |
return o[i], true, nil |
| 1006 |
} |
| 1007 |
case []uint32: |
| 1008 |
isIndex := i >= 0 && i < int64(len(o)) |
| 1009 |
if isIndex { |
| 1010 |
return o[i], true, nil |
| 1011 |
} |
| 1012 |
case []uint64: |
| 1013 |
isIndex := i >= 0 && i < int64(len(o)) |
| 1014 |
if isIndex { |
| 1015 |
return o[i], true, nil |
| 1016 |
} |
| 1017 |
case []float32: |
| 1018 |
isIndex := i >= 0 && i < int64(len(o)) |
| 1019 |
if isIndex { |
| 1020 |
return o[i], true, nil |
| 1021 |
} |
| 1022 |
case []float64: |
| 1023 |
isIndex := i >= 0 && i < int64(len(o)) |
| 1024 |
if isIndex { |
| 1025 |
return o[i], true, nil |
| 1026 |
} |
| 1027 |
case []bool: |
| 1028 |
isIndex := i >= 0 && i < int64(len(o)) |
| 1029 |
if isIndex { |
| 1030 |
return o[i], true, nil |
| 1031 |
} |
| 1032 |
default: |
| 1033 |
return refQualify(q.adapter, obj, q.celValue, presenceTest, presenceOnly, q.errorOnBadPresenceTest) |
| 1034 |
} |
| 1035 |
if presenceTest { |
| 1036 |
return nil, false, nil |
| 1037 |
} |
| 1038 |
if isMap { |
| 1039 |
return nil, false, missingKey(q.celValue) |
| 1040 |
} |
| 1041 |
return nil, false, missingIndex(q.celValue) |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
func (q *intQualifier) Value() ref.Val { |
| 1046 |
return q.celValue |
| 1047 |
} |
| 1048 |
|
| 1049 |
type uintQualifier struct { |
| 1050 |
id int64 |
| 1051 |
value uint64 |
| 1052 |
celValue ref.Val |
| 1053 |
adapter types.Adapter |
| 1054 |
optional bool |
| 1055 |
errorOnBadPresenceTest bool |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
func (q *uintQualifier) ID() int64 { |
| 1060 |
return q.id |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
func (q *uintQualifier) IsOptional() bool { |
| 1065 |
return q.optional |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
func (q *uintQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 1070 |
val, _, err := q.qualifyInternal(vars, obj, false, false) |
| 1071 |
return val, err |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
func (q *uintQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 1076 |
return q.qualifyInternal(vars, obj, true, presenceOnly) |
| 1077 |
} |
| 1078 |
|
| 1079 |
func (q *uintQualifier) qualifyInternal(vars Activation, obj any, presenceTest, presenceOnly bool) (any, bool, error) { |
| 1080 |
u := q.value |
| 1081 |
switch o := obj.(type) { |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
case map[uint]any: |
| 1087 |
obj, isKey := o[uint(u)] |
| 1088 |
if isKey { |
| 1089 |
return obj, true, nil |
| 1090 |
} |
| 1091 |
case map[uint32]any: |
| 1092 |
obj, isKey := o[uint32(u)] |
| 1093 |
if isKey { |
| 1094 |
return obj, true, nil |
| 1095 |
} |
| 1096 |
case map[uint64]any: |
| 1097 |
obj, isKey := o[u] |
| 1098 |
if isKey { |
| 1099 |
return obj, true, nil |
| 1100 |
} |
| 1101 |
default: |
| 1102 |
return refQualify(q.adapter, obj, q.celValue, presenceTest, presenceOnly, q.errorOnBadPresenceTest) |
| 1103 |
} |
| 1104 |
if presenceTest { |
| 1105 |
return nil, false, nil |
| 1106 |
} |
| 1107 |
return nil, false, missingKey(q.celValue) |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
func (q *uintQualifier) Value() ref.Val { |
| 1112 |
return q.celValue |
| 1113 |
} |
| 1114 |
|
| 1115 |
type boolQualifier struct { |
| 1116 |
id int64 |
| 1117 |
value bool |
| 1118 |
celValue ref.Val |
| 1119 |
adapter types.Adapter |
| 1120 |
optional bool |
| 1121 |
errorOnBadPresenceTest bool |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
func (q *boolQualifier) ID() int64 { |
| 1126 |
return q.id |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
func (q *boolQualifier) IsOptional() bool { |
| 1131 |
return q.optional |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
func (q *boolQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 1136 |
val, _, err := q.qualifyInternal(vars, obj, false, false) |
| 1137 |
return val, err |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
func (q *boolQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 1142 |
return q.qualifyInternal(vars, obj, true, presenceOnly) |
| 1143 |
} |
| 1144 |
|
| 1145 |
func (q *boolQualifier) qualifyInternal(vars Activation, obj any, presenceTest, presenceOnly bool) (any, bool, error) { |
| 1146 |
b := q.value |
| 1147 |
switch o := obj.(type) { |
| 1148 |
case map[bool]any: |
| 1149 |
obj, isKey := o[b] |
| 1150 |
if isKey { |
| 1151 |
return obj, true, nil |
| 1152 |
} |
| 1153 |
default: |
| 1154 |
return refQualify(q.adapter, obj, q.celValue, presenceTest, presenceOnly, q.errorOnBadPresenceTest) |
| 1155 |
} |
| 1156 |
if presenceTest { |
| 1157 |
return nil, false, nil |
| 1158 |
} |
| 1159 |
return nil, false, missingKey(q.celValue) |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
func (q *boolQualifier) Value() ref.Val { |
| 1164 |
return q.celValue |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
type fieldQualifier struct { |
| 1171 |
id int64 |
| 1172 |
Name string |
| 1173 |
FieldType *types.FieldType |
| 1174 |
adapter types.Adapter |
| 1175 |
optional bool |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
func (q *fieldQualifier) ID() int64 { |
| 1180 |
return q.id |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
func (q *fieldQualifier) IsOptional() bool { |
| 1185 |
return q.optional |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
func (q *fieldQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 1190 |
if rv, ok := obj.(ref.Val); ok { |
| 1191 |
obj = rv.Value() |
| 1192 |
} |
| 1193 |
val, err := q.FieldType.GetFrom(obj) |
| 1194 |
if err != nil { |
| 1195 |
return nil, err |
| 1196 |
} |
| 1197 |
return val, nil |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
func (q *fieldQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 1202 |
if rv, ok := obj.(ref.Val); ok { |
| 1203 |
obj = rv.Value() |
| 1204 |
} |
| 1205 |
if !q.FieldType.IsSet(obj) { |
| 1206 |
return nil, false, nil |
| 1207 |
} |
| 1208 |
if presenceOnly { |
| 1209 |
return nil, true, nil |
| 1210 |
} |
| 1211 |
val, err := q.FieldType.GetFrom(obj) |
| 1212 |
if err != nil { |
| 1213 |
return nil, false, err |
| 1214 |
} |
| 1215 |
return val, true, nil |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
func (q *fieldQualifier) Value() ref.Val { |
| 1220 |
return types.String(q.Name) |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
type doubleQualifier struct { |
| 1229 |
id int64 |
| 1230 |
value float64 |
| 1231 |
celValue ref.Val |
| 1232 |
adapter types.Adapter |
| 1233 |
optional bool |
| 1234 |
errorOnBadPresenceTest bool |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
func (q *doubleQualifier) ID() int64 { |
| 1239 |
return q.id |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
func (q *doubleQualifier) IsOptional() bool { |
| 1244 |
return q.optional |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
func (q *doubleQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 1249 |
val, _, err := q.qualifyInternal(vars, obj, false, false) |
| 1250 |
return val, err |
| 1251 |
} |
| 1252 |
|
| 1253 |
func (q *doubleQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 1254 |
return q.qualifyInternal(vars, obj, true, presenceOnly) |
| 1255 |
} |
| 1256 |
|
| 1257 |
func (q *doubleQualifier) qualifyInternal(vars Activation, obj any, presenceTest, presenceOnly bool) (any, bool, error) { |
| 1258 |
return refQualify(q.adapter, obj, q.celValue, presenceTest, presenceOnly, q.errorOnBadPresenceTest) |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
func (q *doubleQualifier) Value() ref.Val { |
| 1263 |
return q.celValue |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
type unknownQualifier struct { |
| 1269 |
id int64 |
| 1270 |
value *types.Unknown |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
func (q *unknownQualifier) ID() int64 { |
| 1275 |
return q.id |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
func (q *unknownQualifier) IsOptional() bool { |
| 1280 |
return false |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
func (q *unknownQualifier) Qualify(vars Activation, obj any) (any, error) { |
| 1285 |
return q.value, nil |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
func (q *unknownQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) { |
| 1290 |
return q.value, true, nil |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
func (q *unknownQualifier) Value() ref.Val { |
| 1295 |
return q.value |
| 1296 |
} |
| 1297 |
|
| 1298 |
func applyQualifiers(vars Activation, obj any, qualifiers []Qualifier) (any, bool, error) { |
| 1299 |
optObj, isOpt := obj.(*types.Optional) |
| 1300 |
if isOpt { |
| 1301 |
if !optObj.HasValue() { |
| 1302 |
return optObj, false, nil |
| 1303 |
} |
| 1304 |
obj = optObj.GetValue().Value() |
| 1305 |
} |
| 1306 |
|
| 1307 |
var err error |
| 1308 |
for _, qual := range qualifiers { |
| 1309 |
var qualObj any |
| 1310 |
isOpt = isOpt || qual.IsOptional() |
| 1311 |
if isOpt { |
| 1312 |
var present bool |
| 1313 |
qualObj, present, err = qual.QualifyIfPresent(vars, obj, false) |
| 1314 |
if err != nil { |
| 1315 |
return nil, false, err |
| 1316 |
} |
| 1317 |
if !present { |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
return types.OptionalNone, false, nil |
| 1322 |
} |
| 1323 |
} else { |
| 1324 |
qualObj, err = qual.Qualify(vars, obj) |
| 1325 |
if err != nil { |
| 1326 |
return nil, false, err |
| 1327 |
} |
| 1328 |
} |
| 1329 |
obj = qualObj |
| 1330 |
} |
| 1331 |
return obj, isOpt, nil |
| 1332 |
} |
| 1333 |
|
| 1334 |
|
| 1335 |
func attrQualify(fac AttributeFactory, vars Activation, obj any, qualAttr Attribute) (any, error) { |
| 1336 |
val, err := qualAttr.Resolve(vars) |
| 1337 |
if err != nil { |
| 1338 |
return nil, err |
| 1339 |
} |
| 1340 |
qual, err := fac.NewQualifier(nil, qualAttr.ID(), val, qualAttr.IsOptional()) |
| 1341 |
if err != nil { |
| 1342 |
return nil, err |
| 1343 |
} |
| 1344 |
return qual.Qualify(vars, obj) |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
func attrQualifyIfPresent(fac AttributeFactory, vars Activation, obj any, qualAttr Attribute, |
| 1350 |
presenceOnly bool) (any, bool, error) { |
| 1351 |
val, err := qualAttr.Resolve(vars) |
| 1352 |
if err != nil { |
| 1353 |
return nil, false, err |
| 1354 |
} |
| 1355 |
qual, err := fac.NewQualifier(nil, qualAttr.ID(), val, qualAttr.IsOptional()) |
| 1356 |
if err != nil { |
| 1357 |
return nil, false, err |
| 1358 |
} |
| 1359 |
return qual.QualifyIfPresent(vars, obj, presenceOnly) |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
func refQualify(adapter types.Adapter, obj any, idx ref.Val, presenceTest, presenceOnly, errorOnBadPresenceTest bool) (ref.Val, bool, error) { |
| 1365 |
celVal := adapter.NativeToValue(obj) |
| 1366 |
switch v := celVal.(type) { |
| 1367 |
case *types.Unknown: |
| 1368 |
return v, true, nil |
| 1369 |
case *types.Err: |
| 1370 |
return nil, false, v |
| 1371 |
case traits.Mapper: |
| 1372 |
val, found := v.Find(idx) |
| 1373 |
|
| 1374 |
|
| 1375 |
if types.IsError(val) { |
| 1376 |
return nil, false, val.(*types.Err) |
| 1377 |
} |
| 1378 |
if found { |
| 1379 |
return val, true, nil |
| 1380 |
} |
| 1381 |
if presenceTest { |
| 1382 |
return nil, false, nil |
| 1383 |
} |
| 1384 |
return nil, false, missingKey(idx) |
| 1385 |
case traits.Lister: |
| 1386 |
|
| 1387 |
|
| 1388 |
i, err := types.IndexOrError(idx) |
| 1389 |
if err != nil { |
| 1390 |
return nil, false, err |
| 1391 |
} |
| 1392 |
celIndex := types.Int(i) |
| 1393 |
if i >= 0 && celIndex < v.Size().(types.Int) { |
| 1394 |
return v.Get(idx), true, nil |
| 1395 |
} |
| 1396 |
if presenceTest { |
| 1397 |
return nil, false, nil |
| 1398 |
} |
| 1399 |
return nil, false, missingIndex(idx) |
| 1400 |
case traits.Indexer: |
| 1401 |
if presenceTest { |
| 1402 |
ft, ok := v.(traits.FieldTester) |
| 1403 |
if ok { |
| 1404 |
presence := ft.IsSet(idx) |
| 1405 |
if types.IsError(presence) { |
| 1406 |
return nil, false, presence.(*types.Err) |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
if presenceOnly || presence == types.False { |
| 1411 |
return nil, presence == types.True, nil |
| 1412 |
} |
| 1413 |
} |
| 1414 |
} |
| 1415 |
val := v.Get(idx) |
| 1416 |
if types.IsError(val) { |
| 1417 |
return nil, false, val.(*types.Err) |
| 1418 |
} |
| 1419 |
return val, true, nil |
| 1420 |
default: |
| 1421 |
if presenceTest && !errorOnBadPresenceTest { |
| 1422 |
return nil, false, nil |
| 1423 |
} |
| 1424 |
return nil, false, missingKey(idx) |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
type resolutionError struct { |
| 1431 |
missingAttribute string |
| 1432 |
missingIndex ref.Val |
| 1433 |
missingKey ref.Val |
| 1434 |
} |
| 1435 |
|
| 1436 |
func (e *resolutionError) isMissingAttribute() bool { |
| 1437 |
return e.missingAttribute != "" |
| 1438 |
} |
| 1439 |
|
| 1440 |
func missingIndex(missing ref.Val) *resolutionError { |
| 1441 |
return &resolutionError{ |
| 1442 |
missingIndex: missing, |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
func missingKey(missing ref.Val) *resolutionError { |
| 1447 |
return &resolutionError{ |
| 1448 |
missingKey: missing, |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
func missingAttribute(attr string) *resolutionError { |
| 1453 |
return &resolutionError{ |
| 1454 |
missingAttribute: attr, |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
|
| 1459 |
func (e *resolutionError) Error() string { |
| 1460 |
if e.missingKey != nil { |
| 1461 |
return fmt.Sprintf("no such key: %v", e.missingKey) |
| 1462 |
} |
| 1463 |
if e.missingIndex != nil { |
| 1464 |
return fmt.Sprintf("index out of bounds: %v", e.missingIndex) |
| 1465 |
} |
| 1466 |
if e.missingAttribute != "" { |
| 1467 |
return fmt.Sprintf("no such attribute(s): %s", e.missingAttribute) |
| 1468 |
} |
| 1469 |
return "invalid attribute" |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
func (e *resolutionError) Is(err error) bool { |
| 1474 |
return err.Error() == e.Error() |
| 1475 |
} |