Debian Codegraph @

debian/missing-sources/github.com/google/cel-go/interpreter/attributes.go

raw symbols SCIP
1 // Copyright 2019 Google LLC
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 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 // AttributeFactory provides methods creating Attribute and Qualifier values.
28 type AttributeFactory interface {
29 // AbsoluteAttribute creates an attribute that refers to a top-level variable name.
30 //
31 // Checked expressions generate absolute attribute with a single name.
32 // Parse-only expressions may have more than one possible absolute identifier when the
33 // expression is created within a container, e.g. package or namespace.
34 //
35 // When there is more than one name supplied to the AbsoluteAttribute call, the names
36 // must be in CEL's namespace resolution order. The name arguments provided here are
37 // returned in the same order as they were provided by the NamespacedAttribute
38 // CandidateVariableNames method.
39 AbsoluteAttribute(id int64, names ...string) NamespacedAttribute
40
41 // ConditionalAttribute creates an attribute with two Attribute branches, where the Attribute
42 // that is resolved depends on the boolean evaluation of the input 'expr'.
43 ConditionalAttribute(id int64, expr Interpretable, t, f Attribute) Attribute
44
45 // MaybeAttribute creates an attribute that refers to either a field selection or a namespaced
46 // variable name.
47 //
48 // Only expressions which have not been type-checked may generate oneof attributes.
49 MaybeAttribute(id int64, name string) Attribute
50
51 // RelativeAttribute creates an attribute whose value is a qualification of a dynamic
52 // computation rather than a static variable reference.
53 RelativeAttribute(id int64, operand Interpretable) Attribute
54
55 // NewQualifier creates a qualifier on the target object with a given value.
56 //
57 // The 'val' may be an Attribute or any proto-supported map key type: bool, int, string, uint.
58 //
59 // The qualifier may consider the object type being qualified, if present. If absent, the
60 // qualification should be considered dynamic and the qualification should still work, though
61 // it may be sub-optimal.
62 NewQualifier(objType *types.Type, qualID int64, val any, opt bool) (Qualifier, error)
63 }
64
65 // Qualifier marker interface for designating different qualifier values and where they appear
66 // within field selections and index call expressions (`_[_]`).
67 type Qualifier interface {
68 // ID where the qualifier appears within an expression.
69 ID() int64
70
71 // IsOptional specifies whether the qualifier is optional.
72 // Instead of a direct qualification, an optional qualifier will be resolved via QualifyIfPresent
73 // rather than Qualify. A non-optional qualifier may also be resolved through QualifyIfPresent if
74 // the object to qualify is itself optional.
75 IsOptional() bool
76
77 // Qualify performs a qualification, e.g. field selection, on the input object and returns
78 // the value of the access and whether the value was set. A non-nil value with a false presence
79 // test result indicates that the value being returned is the default value.
80 Qualify(vars Activation, obj any) (any, error)
81
82 // QualifyIfPresent qualifies the object if the qualifier is declared or defined on the object.
83 // The 'presenceOnly' flag indicates that the value is not necessary, just a boolean status as
84 // to whether the qualifier is present.
85 QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error)
86 }
87
88 // ConstantQualifier interface embeds the Qualifier interface and provides an option to inspect the
89 // qualifier's constant value.
90 //
91 // Non-constant qualifiers are of Attribute type.
92 type ConstantQualifier interface {
93 Qualifier
94
95 // Value returns the constant value associated with the qualifier.
96 Value() ref.Val
97 }
98
99 // Attribute values are a variable or value with an optional set of qualifiers, such as field, key,
100 // or index accesses.
101 type Attribute interface {
102 Qualifier
103
104 // AddQualifier adds a qualifier on the Attribute or error if the qualification is not a valid qualifier type.
105 AddQualifier(Qualifier) (Attribute, error)
106
107 // Resolve returns the value of the Attribute and whether it was present given an Activation.
108 // For objects which support safe traversal, the value may be non-nil and the presence flag be false.
109 //
110 // If an error is encountered during attribute resolution, it will be returned immediately.
111 // If the attribute cannot be resolved within the Activation, the result must be: `nil`, `error`
112 // with the error indicating which variable was missing.
113 Resolve(Activation) (any, error)
114 }
115
116 // NamespacedAttribute values are a variable within a namespace, and an optional set of qualifiers
117 // such as field, key, or index accesses.
118 type NamespacedAttribute interface {
119 Attribute
120
121 // CandidateVariableNames returns the possible namespaced variable names for this Attribute in
122 // the CEL namespace resolution order.
123 CandidateVariableNames() []string
124
125 // Qualifiers returns the list of qualifiers associated with the Attribute.
126 Qualifiers() []Qualifier
127 }
128
129 // AttrFactoryOption specifies a functional option for configuring an attribute factory.
130 type AttrFactoryOption func(*attrFactory) *attrFactory
131
132 // EnableErrorOnBadPresenceTest error generation when a presence test or optional field selection
133 // is performed on a primitive type.
134 func EnableErrorOnBadPresenceTest(value bool) AttrFactoryOption {
135 return func(fac *attrFactory) *attrFactory {
136 fac.errorOnBadPresenceTest = value
137 return fac
138 }
139 }
140
141 // NewAttributeFactory returns a default AttributeFactory which is produces Attribute values
142 // capable of resolving types by simple names and qualify the values using the supported qualifier
143 // types: bool, int, string, and uint.
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 // AbsoluteAttribute refers to a variable value and an optional qualifier path.
165 //
166 // The namespaceNames represent the names the variable could have based on namespace
167 // resolution rules.
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 // ConditionalAttribute supports the case where an attribute selection may occur on a conditional
189 // expression, e.g. (cond ? a : b).c
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 // MaybeAttribute collects variants of unchecked AbsoluteAttribute values which could either be
202 // direct variable accesses or some combination of variable access with qualification.
203 func (r *attrFactory) MaybeAttribute(id int64, name string) Attribute {
204 var names []string
205 // When there's a single name with a dot prefix, it indicates that the 'maybe' attribute is a
206 // globally namespaced identifier.
207 if strings.HasPrefix(name, ".") {
208 names = append(names, name)
209 } else {
210 // In all other cases, the candidate names should be inferred.
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 // RelativeAttribute refers to an expression and an optional qualifier path.
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 // NewQualifier is an implementation of the AttributeFactory interface.
237 func (r *attrFactory) NewQualifier(objType *types.Type, qualID int64, val any, opt bool) (Qualifier, error) {
238 // Before creating a new qualifier check to see if this is a protobuf message field access.
239 // If so, use the precomputed GetFrom qualification method rather than the standard
240 // stringQualifier.
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 // namespaceNames represent the names the variable could have based on declared container
260 // (package) of the expression.
261 namespaceNames []string
262 // disambiguateNames indicates whether the namespaceNames require disambiguation with local variables.
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 // ID implements the Attribute interface method.
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 // IsOptional returns trivially false for an attribute as the attribute represents a fully
283 // qualified variable name. If the attribute is used in an optional manner, then an attrQualifier
284 // is created and marks the attribute as optional.
285 func (a *absoluteAttribute) IsOptional() bool {
286 return false
287 }
288
289 // AddQualifier implements the Attribute interface method.
290 func (a *absoluteAttribute) AddQualifier(qual Qualifier) (Attribute, error) {
291 a.qualifiers = append(a.qualifiers, qual)
292 return a, nil
293 }
294
295 // CandidateVariableNames implements the NamespaceAttribute interface method.
296 func (a *absoluteAttribute) CandidateVariableNames() []string {
297 return a.namespaceNames
298 }
299
300 // Qualifiers returns the list of Qualifier instances associated with the namespaced attribute.
301 func (a *absoluteAttribute) Qualifiers() []Qualifier {
302 return a.qualifiers
303 }
304
305 // Qualify is an implementation of the Qualifier interface method.
306 func (a *absoluteAttribute) Qualify(vars Activation, obj any) (any, error) {
307 return attrQualify(a.fac, vars, obj, a)
308 }
309
310 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // String implements the Stringer interface method.
316 func (a *absoluteAttribute) String() string {
317 return fmt.Sprintf("id: %v, names: %v", a.id, a.namespaceNames)
318 }
319
320 // Resolve returns the resolved Attribute value given the Activation, or error if the Attribute
321 // variable is not found, or if its Qualifiers cannot be applied successfully.
322 //
323 // If the variable name cannot be found as an Activation variable or in the TypeProvider as
324 // a type, then the result is `nil`, `error` with the error indicating the name of the first
325 // variable searched as missing.
326 func (a *absoluteAttribute) Resolve(vars Activation) (any, error) {
327 // unwrap any local activations to ensure that we reach the variables provided as input
328 // to the expression in the event that we need to disambiguate between global and local
329 // variables.
330 //
331 // Presently, only dynamic and constant slot activations created during comprehensions
332 // support 'unwrapping', which is consistent with how local variables are introduced into CEL.
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 // If the variable is found, process it. Otherwise, wait until the checks to
344 // determine whether the type is unknown before returning.
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 // Attempt to resolve the qualified type name if the name is not a variable identifier.
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 // ID is an implementation of the Attribute interface method.
395 func (a *conditionalAttribute) ID() int64 {
396 // There's a field access after the conditional.
397 if a.truthy.ID() == a.falsy.ID() {
398 return a.truthy.ID()
399 }
400 // Otherwise return the conditional id as the consistent id being tracked.
401 return a.id
402 }
403
404 // IsOptional returns trivially false for an attribute as the attribute represents a fully
405 // qualified variable name. If the attribute is used in an optional manner, then an attrQualifier
406 // is created and marks the attribute as optional.
407 func (a *conditionalAttribute) IsOptional() bool {
408 return false
409 }
410
411 // AddQualifier appends the same qualifier to both sides of the conditional, in effect managing
412 // the qualification of alternate attributes.
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 // Qualify is an implementation of the Qualifier interface method.
426 func (a *conditionalAttribute) Qualify(vars Activation, obj any) (any, error) {
427 return attrQualify(a.fac, vars, obj, a)
428 }
429
430 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // Resolve evaluates the condition, and then resolves the truthy or falsy branch accordingly.
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 // String is an implementation of the Stringer interface method.
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 // ID is an implementation of the Attribute interface method.
464 func (a *maybeAttribute) ID() int64 {
465 return a.attrs[0].ID()
466 }
467
468 // IsOptional returns trivially false for an attribute as the attribute represents a fully
469 // qualified variable name. If the attribute is used in an optional manner, then an attrQualifier
470 // is created and marks the attribute as optional.
471 func (a *maybeAttribute) IsOptional() bool {
472 return false
473 }
474
475 // AddQualifier adds a qualifier to each possible attribute variant, and also creates
476 // a new namespaced variable from the qualified value.
477 //
478 // The algorithm for building the maybe attribute is as follows:
479 //
480 // 1. Create a maybe attribute from a simple identifier when it occurs in a parsed-only expression
481 //
482 // mb = MaybeAttribute(<id>, "a")
483 //
484 // Initializing the maybe attribute creates an absolute attribute internally which includes the
485 // possible namespaced names of the attribute. In this example, let's assume we are in namespace
486 // 'ns', then the maybe is either one of the following variable names:
487 //
488 // possible variables names -- ns.a, a
489 //
490 // 2. Adding a qualifier to the maybe means that the variable name could be a longer qualified
491 // name, or a field selection on one of the possible variable names produced earlier:
492 //
493 // mb.AddQualifier("b")
494 //
495 // possible variables names -- ns.a.b, a.b
496 // possible field selection -- ns.a['b'], a['b']
497 //
498 // If none of the attributes within the maybe resolves a value, the result is an error.
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 // First add the qualifier to all existing attributes in the oneof.
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 // Next, ensure the most specific variable / type reference is searched first.
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 // Qualify is an implementation of the Qualifier interface method.
529 func (a *maybeAttribute) Qualify(vars Activation, obj any) (any, error) {
530 return attrQualify(a.fac, vars, obj, a)
531 }
532
533 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // Resolve follows the variable resolution rules to determine whether the attribute is a variable
539 // or a field selection.
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 // Return an error if one is encountered.
545 if err != nil {
546 resErr, ok := err.(*resolutionError)
547 if !ok {
548 return nil, err
549 }
550 // If this was not a missing variable error, return it.
551 if !resErr.isMissingAttribute() {
552 return nil, err
553 }
554 // When the variable is missing in a maybe attribute we defer erroring.
555 if maybeErr == nil {
556 maybeErr = resErr
557 }
558 // Continue attempting to resolve possible variables.
559 continue
560 }
561 return obj, nil
562 }
563 // Else, produce a no such attribute error.
564 return nil, maybeErr
565 }
566
567 // String is an implementation of the Stringer interface method.
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 // ID is an implementation of the Attribute interface method.
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 // IsOptional returns trivially false for an attribute as the attribute represents a fully
592 // qualified variable name. If the attribute is used in an optional manner, then an attrQualifier
593 // is created and marks the attribute as optional.
594 func (a *relativeAttribute) IsOptional() bool {
595 return false
596 }
597
598 // AddQualifier implements the Attribute interface method.
599 func (a *relativeAttribute) AddQualifier(qual Qualifier) (Attribute, error) {
600 a.qualifiers = append(a.qualifiers, qual)
601 return a, nil
602 }
603
604 // Qualify is an implementation of the Qualifier interface method.
605 func (a *relativeAttribute) Qualify(vars Activation, obj any) (any, error) {
606 return attrQualify(a.fac, vars, obj, a)
607 }
608
609 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // Resolve expression value and qualifier relative to the expression result.
615 func (a *relativeAttribute) Resolve(vars Activation) (any, error) {
616 // First, evaluate the operand.
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 // String is an implementation of the Stringer interface method.
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 // Note, attributes are initially identified as non-optional since they represent a top-level
648 // field access; however, when used as a relative qualifier, e.g. a[?b.c], then an attrQualifier
649 // is created which intercepts the IsOptional check for the attribute in order to return the
650 // correct result.
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 // ID implements the Qualifier interface method and returns the qualification instruction id
809 // rather than the attribute id.
810 func (q *attrQualifier) ID() int64 {
811 return q.id
812 }
813
814 // IsOptional implements the Qualifier interface method.
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 // ID is an implementation of the Qualifier interface method.
829 func (q *stringQualifier) ID() int64 {
830 return q.id
831 }
832
833 // IsOptional implements the Qualifier interface method.
834 func (q *stringQualifier) IsOptional() bool {
835 return q.optional
836 }
837
838 // Qualify implements the Qualifier interface method.
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 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // Value implements the ConstantQualifier interface
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 // ID is an implementation of the Qualifier interface method.
931 func (q *intQualifier) ID() int64 {
932 return q.id
933 }
934
935 // IsOptional implements the Qualifier interface method.
936 func (q *intQualifier) IsOptional() bool {
937 return q.optional
938 }
939
940 // Qualify implements the Qualifier interface method.
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 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // The specialized map types supported by an int qualifier are considerably fewer than the set
956 // of specialized map types supported by string qualifiers since they are less frequently used
957 // than string-based map keys. Additional specializations may be added in the future if
958 // desired.
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 // Value implements the ConstantQualifier interface
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 // ID is an implementation of the Qualifier interface method.
1059 func (q *uintQualifier) ID() int64 {
1060 return q.id
1061 }
1062
1063 // IsOptional implements the Qualifier interface method.
1064 func (q *uintQualifier) IsOptional() bool {
1065 return q.optional
1066 }
1067
1068 // Qualify implements the Qualifier interface method.
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 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // The specialized map types supported by a uint qualifier are considerably fewer than the set
1083 // of specialized map types supported by string qualifiers since they are less frequently used
1084 // than string-based map keys. Additional specializations may be added in the future if
1085 // desired.
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 // Value implements the ConstantQualifier interface
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 // ID is an implementation of the Qualifier interface method.
1125 func (q *boolQualifier) ID() int64 {
1126 return q.id
1127 }
1128
1129 // IsOptional implements the Qualifier interface method.
1130 func (q *boolQualifier) IsOptional() bool {
1131 return q.optional
1132 }
1133
1134 // Qualify implements the Qualifier interface method.
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 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // Value implements the ConstantQualifier interface
1163 func (q *boolQualifier) Value() ref.Val {
1164 return q.celValue
1165 }
1166
1167 // fieldQualifier indicates that the qualification is a well-defined field with a known
1168 // field type. When the field type is known this can be used to improve the speed and
1169 // efficiency of field resolution.
1170 type fieldQualifier struct {
1171 id int64
1172 Name string
1173 FieldType *types.FieldType
1174 adapter types.Adapter
1175 optional bool
1176 }
1177
1178 // ID is an implementation of the Qualifier interface method.
1179 func (q *fieldQualifier) ID() int64 {
1180 return q.id
1181 }
1182
1183 // IsOptional implements the Qualifier interface method.
1184 func (q *fieldQualifier) IsOptional() bool {
1185 return q.optional
1186 }
1187
1188 // Qualify implements the Qualifier interface method.
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 // QualifyIfPresent is an implementation of the Qualifier interface method.
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 // Value implements the ConstantQualifier interface
1219 func (q *fieldQualifier) Value() ref.Val {
1220 return types.String(q.Name)
1221 }
1222
1223 // doubleQualifier qualifies a CEL object, map, or list using a double value.
1224 //
1225 // This qualifier is used for working with dynamic data like JSON or protobuf.Any where the value
1226 // type may not be known ahead of time and may not conform to the standard types supported as valid
1227 // protobuf map key types.
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 // ID is an implementation of the Qualifier interface method.
1238 func (q *doubleQualifier) ID() int64 {
1239 return q.id
1240 }
1241
1242 // IsOptional implements the Qualifier interface method.
1243 func (q *doubleQualifier) IsOptional() bool {
1244 return q.optional
1245 }
1246
1247 // Qualify implements the Qualifier interface method.
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 // Value implements the ConstantQualifier interface
1262 func (q *doubleQualifier) Value() ref.Val {
1263 return q.celValue
1264 }
1265
1266 // unknownQualifier is a simple qualifier which always returns a preconfigured set of unknown values
1267 // for any value subject to qualification. This is consistent with CEL's unknown handling elsewhere.
1268 type unknownQualifier struct {
1269 id int64
1270 value *types.Unknown
1271 }
1272
1273 // ID is an implementation of the Qualifier interface method.
1274 func (q *unknownQualifier) ID() int64 {
1275 return q.id
1276 }
1277
1278 // IsOptional returns trivially false as an the unknown value is always returned.
1279 func (q *unknownQualifier) IsOptional() bool {
1280 return false
1281 }
1282
1283 // Qualify returns the unknown value associated with this qualifier.
1284 func (q *unknownQualifier) Qualify(vars Activation, obj any) (any, error) {
1285 return q.value, nil
1286 }
1287
1288 // QualifyIfPresent is an implementation of the Qualifier interface method.
1289 func (q *unknownQualifier) QualifyIfPresent(vars Activation, obj any, presenceOnly bool) (any, bool, error) {
1290 return q.value, true, nil
1291 }
1292
1293 // Value implements the ConstantQualifier interface
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 // We return optional none here with a presence of 'false' as the layers
1319 // above will attempt to call types.OptionalOf() on a present value if any
1320 // of the qualifiers is optional.
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 // attrQualify performs a qualification using the result of an attribute evaluation.
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 // attrQualifyIfPresent conditionally performs the qualification of the result of attribute is present
1348 // on the target object.
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 // refQualify attempts to convert the value to a CEL value and then uses reflection methods to try and
1363 // apply the qualifier with the option to presence test field accesses before retrieving field values.
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 // If the index is of the wrong type for the map, then it is possible
1374 // for the Find call to produce an error.
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 // If the index argument is not a valid numeric type, then it is possible
1387 // for the index operation to produce an error.
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 // If not found or presence only test, then return.
1409 // Otherwise, if found, obtain the value later on.
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 // resolutionError is a custom error type which encodes the different error states which may
1429 // occur during attribute resolution.
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 // Error implements the error interface method.
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 // Is implements the errors.Is() method used by more recent versions of Go.
1473 func (e *resolutionError) Is(err error) bool {
1474 return err.Error() == e.Error()
1475 }