Coverage Report

Created: 2026-11-12 06:42

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/container/flat_hash_map.h"
11
#include "absl/log/absl_check.h"
12
#include "absl/status/status.h"
13
#include "absl/strings/string_view.h"
14
#include "absl/types/optional.h"
15
#include "absl/types/span.h"
16
#include "base/attribute_matcher.h"
17
#include "eval/public/base_activation.h"
18
#include "eval/public/cel_attribute.h"
19
#include "eval/public/cel_function.h"
20
#include "eval/public/cel_value.h"
21
#include "eval/public/cel_value_producer.h"
22
#include "internal/status_macros.h"
23
#include "google/protobuf/arena.h"
24
25
namespace cel::runtime_internal {
26
class ActivationAttributeMatcherAccess;
27
}
28
29
namespace google::api::expr::runtime {
30
31
// Instance of Activation class is used by evaluator.
32
// It provides binding between references used in expressions
33
// and actual values.
34
class Activation : public BaseActivation {
35
 public:
36
18.9k
  Activation() = default;
37
38
  // Non-copyable/non-assignable
39
  Activation(const Activation&) = delete;
40
  Activation& operator=(const Activation&) = delete;
41
42
  // Move-constructible/move-assignable
43
  Activation(Activation&& other) = default;
44
  Activation& operator=(Activation&& other) = default;
45
46
  // BaseActivation
47
  std::vector<const CelFunction*> FindFunctionOverloads(
48
      absl::string_view name) const override;
49
50
  absl::optional<CelValue> FindValue(absl::string_view name,
51
                                     google::protobuf::Arena* arena) const override;
52
53
  // Insert a function into the activation (ie a lazily bound function). Returns
54
  // a status if the name and shape of the function matches another one that has
55
  // already been bound.
56
  absl::Status InsertFunction(std::unique_ptr<CelFunction> function);
57
58
  // Insert value into Activation.
59
  void InsertValue(absl::string_view name, const CelValue& value);
60
61
  // Insert ValueProducer into Activation.
62
  void InsertValueProducer(absl::string_view name,
63
                           std::unique_ptr<CelValueProducer> value_producer);
64
65
  // Remove functions that have the same name and shape as descriptor. Returns
66
  // true if matching functions were found and removed.
67
  bool RemoveFunctionEntries(const CelFunctionDescriptor& descriptor);
68
69
  // Removes value or producer, returns true if entry with the name was found
70
  bool RemoveValueEntry(absl::string_view name);
71
72
  // Clears a cached value for a value producer, returns if true if entry was
73
  // found and cleared.
74
  bool ClearValueEntry(absl::string_view name);
75
76
  // Clears all cached values for value producers. Returns the number of entries
77
  // cleared.
78
  int ClearCachedValues();
79
80
  // Set missing attribute patterns for evaluation.
81
  //
82
  // If a field access is found to match any of the provided patterns, the
83
  // result is treated as a missing attribute error.
84
  absl::Status SetMissingAttributePatterns(
85
0
      absl::Span<const CelAttributePattern> patterns) {
86
0
    cel::AttributeMatcher matcher;
87
0
    for (const auto& pattern : patterns) {
88
0
      // This function used to return void, thus they could not fail. We use
89
0
      // upsert because it just works most of the time, especially if the caller
90
0
      // provided overlapped attribute patterns. Ideally we would use insert.
91
0
      CEL_RETURN_IF_ERROR(matcher.UpsertAttribute(pattern));
92
0
    }
93
0
    missing_attributes_ = std::move(matcher);
94
0
    return absl::OkStatus();
95
0
  }
96
97
  ABSL_DEPRECATED("Use SetUnknownAttributePatterns")
98
  void set_unknown_attribute_patterns(
99
0
      absl::Span<const CelAttributePattern> patterns) {
100
0
    unknown_attributes_.ClearAttributes();
101
0
    absl::Status status = SetUnknownAttributePatterns(patterns);
102
0
    ABSL_DCHECK_OK(status);
103
0
    status.IgnoreError();
104
0
  }
105
106
  // Sets the collection of attribute patterns that will be recognized as
107
  // "unknown" values during expression evaluation.
108
  absl::Status SetUnknownAttributePatterns(
109
0
      absl::Span<const CelAttributePattern> patterns) {
110
0
    cel::AttributeMatcher matcher;
111
0
    for (const auto& pattern : patterns) {
112
0
      // This function used to return void, thus they could not fail. We use
113
0
      // upsert because it just works most of the time, especially if the caller
114
0
      // provided overlapped attribute patterns. Ideally we would use insert.
115
0
      CEL_RETURN_IF_ERROR(matcher.UpsertAttribute(pattern));
116
0
    }
117
0
    unknown_attributes_ = std::move(matcher);
118
0
    return absl::OkStatus();
119
0
  }
120
121
  absl::Status SetKnownAttributePatterns(
122
0
      absl::Span<const CelAttributePattern> patterns) {
123
0
    cel::AttributeMatcher matcher;
124
0
    for (const auto& pattern : patterns) {
125
0
      // This function used to return void, thus they could not fail. We use
126
0
      // upsert because it just works most of the time, especially if the caller
127
0
      // provided overlapped attribute patterns. Ideally we would use insert.
128
0
      CEL_RETURN_IF_ERROR(matcher.UpsertAttribute(pattern));
129
0
    }
130
0
    known_attributes_ = std::move(matcher);
131
0
    return absl::OkStatus();
132
0
  }
133
134
0
  const cel::AttributeMatcher& GetUnknownAttributeMatcher() const final {
135
0
    return unknown_attributes_;
136
0
  }
137
138
0
  const cel::AttributeMatcher& GetKnownAttributeMatcher() const final {
139
0
    return known_attributes_;
140
0
  }
141
142
0
  const cel::AttributeMatcher& GetMissingAttributeMatcher() const final {
143
0
    return missing_attributes_;
144
0
  }
145
146
 private:
147
  class ValueEntry {
148
   public:
149
    explicit ValueEntry(std::unique_ptr<CelValueProducer> prod)
150
0
        : value_(), producer_(std::move(prod)) {}
151
152
0
    explicit ValueEntry(const CelValue& value) : value_(value), producer_() {}
153
154
    // Retrieve associated CelValue.
155
    // If the value is not set and producer is set,
156
    // obtain and cache value from producer.
157
0
    absl::optional<CelValue> RetrieveValue(google::protobuf::Arena* arena) const {
158
0
      if (!value_.has_value()) {
159
0
        if (producer_) {
160
0
          value_ = producer_->Produce(arena);
161
0
        }
162
0
      }
163
164
0
      return value_;
165
0
    }
166
167
0
    bool ClearValue() {
168
0
      bool result = value_.has_value();
169
0
      value_.reset();
170
0
      return result;
171
0
    }
172
173
0
    bool HasProducer() const { return producer_ != nullptr; }
174
175
   private:
176
    mutable absl::optional<CelValue> value_;
177
    std::unique_ptr<CelValueProducer> producer_;
178
  };
179
180
  friend class cel::runtime_internal::ActivationAttributeMatcherAccess;
181
182
  absl::flat_hash_map<std::string, ValueEntry> value_map_;
183
  absl::flat_hash_map<std::string, std::vector<std::unique_ptr<CelFunction>>>
184
      function_map_;
185
186
  cel::AttributeMatcher unknown_attributes_;
187
  cel::AttributeMatcher known_attributes_;
188
  cel::AttributeMatcher missing_attributes_;
189
};
190
191
}  // namespace google::api::expr::runtime
192
193
#endif  // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_ACTIVATION_H_