| 1 | // Copyright 2018 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 | "errors" |
| 19 | "fmt" |
| 20 | |
| 21 | "github.com/google/cel-go/common/types/ref" |
| 22 | ) |
| 23 | |
| 24 | // Activation used to resolve identifiers by name and references by id. |
| 25 | // |
| 26 | // An Activation is the primary mechanism by which a caller supplies input into a CEL program. |
| 27 | type Activation interface { |
| 28 | // ResolveName returns a value from the activation by qualified name, or false if the name |
| 29 | // could not be found. |
| 30 | ResolveName(name string) (any, bool) |
| 31 | |
| 32 | // Parent returns the parent of the current activation, may be nil. |
| 33 | // If non-nil, the parent will be searched during resolve calls. |
| 34 | Parent() Activation |
| 35 | } |
| 36 | |
| 37 | // EmptyActivation returns a variable-free activation. |
| 38 | func EmptyActivation() Activation { |
| 39 | return emptyActivation{} |
| 40 | } |
| 41 | |
| 42 | // emptyActivation is a variable-free activation. |
| 43 | type emptyActivation struct{} |
| 44 | |
| 45 | func (emptyActivation) ResolveName(string) (any, bool) { return nil, false } |
| 46 | func (emptyActivation) Parent() Activation { return nil } |
| 47 | |
| 48 | // NewActivation returns an activation based on a map-based binding where the map keys are |
| 49 | // expected to be qualified names used with ResolveName calls. |
| 50 | // |
| 51 | // The input `bindings` may either be of type `Activation` or `map[string]any`. |
| 52 | // |
| 53 | // Lazy bindings may be supplied within the map-based input in either of the following forms: |
| 54 | // - func() any |
| 55 | // - func() ref.Val |
| 56 | // |
| 57 | // The output of the lazy binding will overwrite the variable reference in the internal map. |
| 58 | // |
| 59 | // Values which are not represented as ref.Val types on input may be adapted to a ref.Val using |
| 60 | // the types.Adapter configured in the environment. |
| 61 | func NewActivation(bindings any) (Activation, error) { |
| 62 | if bindings == nil { |
| 63 | return nil, errors.New("bindings must be non-nil") |
| 64 | } |
| 65 | a, isActivation := bindings.(Activation) |
| 66 | if isActivation { |
| 67 | return a, nil |
| 68 | } |
| 69 | m, isMap := bindings.(map[string]any) |
| 70 | if !isMap { |
| 71 | return nil, fmt.Errorf( |
| 72 | "activation input must be an activation or map[string]interface: got %T", |
| 73 | bindings) |
| 74 | } |
| 75 | return &mapActivation{bindings: m}, nil |
| 76 | } |
| 77 | |
| 78 | // mapActivation which implements Activation and maps of named values. |
| 79 | // |
| 80 | // Named bindings may lazily supply values by providing a function which accepts no arguments and |
| 81 | // produces an interface value. |
| 82 | type mapActivation struct { |
| 83 | bindings map[string]any |
| 84 | } |
| 85 | |
| 86 | // Parent implements the Activation interface method. |
| 87 | func (a *mapActivation) Parent() Activation { |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // ResolveName implements the Activation interface method. |
| 92 | func (a *mapActivation) ResolveName(name string) (any, bool) { |
| 93 | obj, found := a.bindings[name] |
| 94 | if !found { |
| 95 | return nil, false |
| 96 | } |
| 97 | fn, isLazy := obj.(func() ref.Val) |
| 98 | if isLazy { |
| 99 | obj = fn() |
| 100 | a.bindings[name] = obj |
| 101 | } |
| 102 | fnRaw, isLazy := obj.(func() any) |
| 103 | if isLazy { |
| 104 | obj = fnRaw() |
| 105 | a.bindings[name] = obj |
| 106 | } |
| 107 | return obj, found |
| 108 | } |
| 109 | |
| 110 | // hierarchicalActivation which implements Activation and contains a parent and |
| 111 | // child activation. |
| 112 | type hierarchicalActivation struct { |
| 113 | parent Activation |
| 114 | child Activation |
| 115 | } |
| 116 | |
| 117 | // Parent implements the Activation interface method. |
| 118 | func (a *hierarchicalActivation) Parent() Activation { |
| 119 | return a.parent |
| 120 | } |
| 121 | |
| 122 | // ResolveName implements the Activation interface method. |
| 123 | func (a *hierarchicalActivation) ResolveName(name string) (any, bool) { |
| 124 | if object, found := a.child.ResolveName(name); found { |
| 125 | return object, found |
| 126 | } |
| 127 | return a.parent.ResolveName(name) |
| 128 | } |
| 129 | |
| 130 | // NewHierarchicalActivation takes two activations and produces a new one which prioritizes |
| 131 | // resolution in the child first and parent(s) second. |
| 132 | func NewHierarchicalActivation(parent Activation, child Activation) Activation { |
| 133 | return &hierarchicalActivation{parent, child} |
| 134 | } |
| 135 | |
| 136 | // NewPartialActivation returns an Activation which contains a list of AttributePattern values |
| 137 | // representing field and index operations that should result in a 'types.Unknown' result. |
| 138 | // |
| 139 | // The `bindings` value may be any value type supported by the interpreter.NewActivation call, |
| 140 | // but is typically either an existing Activation or map[string]any. |
| 141 | func NewPartialActivation(bindings any, |
| 142 | unknowns ...*AttributePattern) (PartialActivation, error) { |
| 143 | a, err := NewActivation(bindings) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | return &partActivation{Activation: a, unknowns: unknowns}, nil |
| 148 | } |
| 149 | |
| 150 | // PartialActivation extends the Activation interface with a set of UnknownAttributePatterns. |
| 151 | type PartialActivation interface { |
| 152 | Activation |
| 153 | |
| 154 | // UnknownAttributePaths returns a set of AttributePattern values which match Attribute |
| 155 | // expressions for data accesses whose values are not yet known. |
| 156 | UnknownAttributePatterns() []*AttributePattern |
| 157 | } |
| 158 | |
| 159 | // partialActivationConverter indicates whether an Activation implementation supports conversion to a PartialActivation |
| 160 | type partialActivationConverter interface { |
| 161 | // AsPartialActivation converts the current activation to a PartialActivation |
| 162 | AsPartialActivation() (PartialActivation, bool) |
| 163 | } |
| 164 | |
| 165 | // partActivation is the default implementations of the PartialActivation interface. |
| 166 | type partActivation struct { |
| 167 | Activation |
| 168 | unknowns []*AttributePattern |
| 169 | } |
| 170 | |
| 171 | // UnknownAttributePatterns implements the PartialActivation interface method. |
| 172 | func (a *partActivation) UnknownAttributePatterns() []*AttributePattern { |
| 173 | return a.unknowns |
| 174 | } |
| 175 | |
| 176 | // AsPartialActivation returns the partActivation as a PartialActivation interface. |
| 177 | func (a *partActivation) AsPartialActivation() (PartialActivation, bool) { |
| 178 | return a, true |
| 179 | } |
| 180 | |
| 181 | // AsPartialActivation walks the activation hierarchy and returns the first PartialActivation, if found. |
| 182 | func AsPartialActivation(vars Activation) (PartialActivation, bool) { |
| 183 | // Only internal activation instances may implement this interface |
| 184 | if pv, ok := vars.(partialActivationConverter); ok { |
| 185 | return pv.AsPartialActivation() |
| 186 | } |
| 187 | // Since Activations may be hierarchical, test whether a parent converts to a PartialActivation |
| 188 | if vars.Parent() != nil { |
| 189 | return AsPartialActivation(vars.Parent()) |
| 190 | } |
| 191 | return nil, false |
| 192 | } |