Coverage Report

Created: 2026-10-09 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/eval/public/activation.h
Line
Count
Source
1
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_ACTIVATION_H_
2
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_ACTIVATION_H_
3
4
#include <memory>
5
#include <string>
6
#include <utility>
7
#include <vector>
8
9
#include "absl/base/attributes.h"
10
#include "absl/base/nullability.h"
11
#include "absl/container/flat_hash_map.h"
12
#include "absl/status/status.h"
13
#include "absl/strings/string_view.h"
14
#include "absl/types/optional.h"
15
#include "eval/public/base_activation.h"
16
#include "eval/public/cel_attribute.h"
17
#include "eval/public/cel_function.h"
18
#include "eval/public/cel_value.h"
19
#include "eval/public/cel_value_producer.h"
20
#include "runtime/internal/attribute_matcher.h"
21
#include "google/protobuf/arena.h"
22
23
namespace cel::runtime_internal {
24
class ActivationAttributeMatcherAccess;
25
}
26
27
namespace google::api::expr::runtime {
28
29
// Instance of Activation class is used by evaluator.
30
// It provides binding between references used in expressions
31
// and actual values.
32
class Activation : public BaseActivation {
33
 public:
34
17.3k
  Activation() = default;
35
36
  // Non-copyable/non-assignable
37
  Activation(const Activation&) = delete;
38
  Activation& operator=(const Activation&) = delete;
39
40
  // Move-constructible/move-assignable
41
  Activation(Activation&& other) = default;
42
  Activation& operator=(Activation&& other) = default;
43
44
  // BaseActivation
45
  std::vector<const CelFunction*> FindFunctionOverloads(
46
      absl::string_view name) const override;
47
48
  absl::optional<CelValue> FindValue(absl::string_view name,
49
                                     google::protobuf::Arena* arena) const override;
50
51
  // Insert a function into the activation (ie a lazily bound function). Returns
52
  // a status if the name and shape of the function matches another one that has
53
  // already been bound.
54
  absl::Status InsertFunction(std::unique_ptr<CelFunction> function);
55
56
  // Insert value into Activation.
57
  void InsertValue(absl::string_view name, const CelValue& value);
58
59
  // Insert ValueProducer into Activation.
60
  void InsertValueProducer(absl::string_view name,
61
                           std::unique_ptr<CelValueProducer> value_producer);
62
63
  // Remove functions that have the same name and shape as descriptor. Returns
64
  // true if matching functions were found and removed.
65
  bool RemoveFunctionEntries(const CelFunctionDescriptor& descriptor);
66
67
  // Removes value or producer, returns true if entry with the name was found
68
  bool RemoveValueEntry(absl::string_view name);
69
70
  // Clears a cached value for a value producer, returns if true if entry was
71
  // found and cleared.
72
  bool ClearValueEntry(absl::string_view name);
73
74
  // Clears all cached values for value producers. Returns the number of entries
75
  // cleared.
76
  int ClearCachedValues();
77
78
  // Set missing attribute patterns for evaluation.
79
  //
80
  // If a field access is found to match any of the provided patterns, the
81
  // result is treated as a missing attribute error.
82
  absl::Status SetMissingAttributePatterns(
83
0
      std::vector<CelAttributePattern> missing_attribute_patterns) {
84
0
    missing_attribute_patterns_ = std::move(missing_attribute_patterns);
85
0
    return absl::OkStatus();
86
0
  }
87
88
  const std::vector<CelAttributePattern>& missing_attribute_patterns()
89
0
      const override {
90
0
    return missing_attribute_patterns_;
91
0
  }
92
93
  ABSL_DEPRECATED("Use SetUnknownAttributePatterns")
94
  void set_unknown_attribute_patterns(
95
0
      std::vector<CelAttributePattern> unknown_attribute_patterns) {
96
0
    SetUnknownAttributePatterns(std::move(unknown_attribute_patterns))
97
0
        .IgnoreError();
98
0
  }
99
100
  // Sets the collection of attribute patterns that will be recognized as
101
  // "unknown" values during expression evaluation.
102
  absl::Status SetUnknownAttributePatterns(
103
0
      std::vector<CelAttributePattern> unknown_attribute_patterns) {
104
0
    unknown_attribute_patterns_ = std::move(unknown_attribute_patterns);
105
0
    return absl::OkStatus();
106
0
  }
107
108
  // Return the collection of attribute patterns that determine "unknown"
109
  // values.
110
  const std::vector<CelAttributePattern>& unknown_attribute_patterns()
111
0
      const override {
112
0
    return unknown_attribute_patterns_;
113
0
  }
114
115
 private:
116
  class ValueEntry {
117
   public:
118
    explicit ValueEntry(std::unique_ptr<CelValueProducer> prod)
119
0
        : value_(), producer_(std::move(prod)) {}
120
121
0
    explicit ValueEntry(const CelValue& value) : value_(value), producer_() {}
122
123
    // Retrieve associated CelValue.
124
    // If the value is not set and producer is set,
125
    // obtain and cache value from producer.
126
0
    absl::optional<CelValue> RetrieveValue(google::protobuf::Arena* arena) const {
127
0
      if (!value_.has_value()) {
128
0
        if (producer_) {
129
0
          value_ = producer_->Produce(arena);
130
0
        }
131
0
      }
132
133
0
      return value_;
134
0
    }
135
136
0
    bool ClearValue() {
137
0
      bool result = value_.has_value();
138
0
      value_.reset();
139
0
      return result;
140
0
    }
141
142
0
    bool HasProducer() const { return producer_ != nullptr; }
143
144
   private:
145
    mutable absl::optional<CelValue> value_;
146
    std::unique_ptr<CelValueProducer> producer_;
147
  };
148
149
  friend class cel::runtime_internal::ActivationAttributeMatcherAccess;
150
151
  void SetAttributeMatcher(
152
0
      const cel::runtime_internal::AttributeMatcher* matcher) {
153
0
    attribute_matcher_ = matcher;
154
0
  }
155
156
  void SetAttributeMatcher(
157
0
      std::unique_ptr<const cel::runtime_internal::AttributeMatcher> matcher) {
158
0
    owned_attribute_matcher_ = std::move(matcher);
159
0
    attribute_matcher_ = owned_attribute_matcher_.get();
160
0
  }
161
162
  const cel::runtime_internal::AttributeMatcher* absl_nullable
163
0
  GetAttributeMatcher() const override {
164
0
    return attribute_matcher_;
165
0
  }
166
167
  absl::flat_hash_map<std::string, ValueEntry> value_map_;
168
  absl::flat_hash_map<std::string, std::vector<std::unique_ptr<CelFunction>>>
169
      function_map_;
170
171
  std::vector<CelAttributePattern> missing_attribute_patterns_;
172
  std::vector<CelAttributePattern> unknown_attribute_patterns_;
173
174
  const cel::runtime_internal::AttributeMatcher* attribute_matcher_ = nullptr;
175
  std::unique_ptr<const cel::runtime_internal::AttributeMatcher>
176
      owned_attribute_matcher_;
177
};
178
179
}  // namespace google::api::expr::runtime
180
181
#endif  // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_ACTIVATION_H_