Coverage Report

Created: 2026-05-27 07:00

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