-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathattribute_utility.cc
More file actions
145 lines (127 loc) · 5.09 KB
/
Copy pathattribute_utility.cc
File metadata and controls
145 lines (127 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "eval/eval/attribute_utility.h"
#include <utility>
#include "base/attribute_set.h"
#include "base/values/unknown_value.h"
#include "extensions/protobuf/memory_manager.h"
namespace google::api::expr::runtime {
bool AttributeUtility::CheckForMissingAttribute(
const AttributeTrail& trail) const {
if (trail.empty()) {
return false;
}
for (const auto& pattern : missing_attribute_patterns_) {
// (b/161297249) Preserving existing behavior for now, will add a streamz
// for partial match, follow up with tightening up which fields are exposed
// to the condition (w/ ajay and jim)
if (pattern.IsMatch(trail.attribute()) ==
cel::AttributePattern::MatchType::FULL) {
return true;
}
}
return false;
}
// Checks whether particular corresponds to any patterns that define unknowns.
bool AttributeUtility::CheckForUnknown(const AttributeTrail& trail,
bool use_partial) const {
if (trail.empty()) {
return false;
}
for (const auto& pattern : unknown_patterns_) {
auto current_match = pattern.IsMatch(trail.attribute());
if (current_match == cel::AttributePattern::MatchType::FULL ||
(use_partial &&
current_match == cel::AttributePattern::MatchType::PARTIAL)) {
return true;
}
}
return false;
}
// Creates merged UnknownAttributeSet.
// Scans over the args collection, merges any UnknownSets found in
// it together with initial_set (if initial_set is not null).
// Returns pointer to merged set or nullptr, if there were no sets to merge.
const UnknownSet* AttributeUtility::MergeUnknowns(
absl::Span<const cel::Handle<cel::Value>> args,
const UnknownSet* initial_set) const {
absl::optional<UnknownSet> result_set;
for (const auto& value : args) {
if (!value->Is<cel::UnknownValue>()) continue;
const auto& current_set = value.As<cel::UnknownValue>();
if (!result_set.has_value()) {
if (initial_set != nullptr) {
result_set.emplace(*initial_set);
} else {
result_set.emplace();
}
}
cel::base_internal::UnknownSetAccess::Add(
*result_set, UnknownSet(current_set->attribute_set(),
current_set->function_result_set()));
}
if (!result_set.has_value()) {
return initial_set;
}
return google::protobuf::Arena::Create<UnknownSet>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(memory_manager_),
std::move(result_set).value());
}
// Creates merged UnknownAttributeSet.
// Scans over the args collection, determines if there matches to unknown
// patterns, merges attributes together with those from initial_set
// (if initial_set is not null).
// Returns pointer to merged set or nullptr, if there were no sets to merge.
cel::AttributeSet AttributeUtility::CheckForUnknowns(
absl::Span<const AttributeTrail> args, bool use_partial) const {
cel::AttributeSet attribute_set;
for (const auto& trail : args) {
if (CheckForUnknown(trail, use_partial)) {
attribute_set.Add(trail.attribute());
}
}
return attribute_set;
}
// Creates merged UnknownAttributeSet.
// Merges together attributes from UnknownAttributeSets found in the args
// collection, attributes from attr that match unknown pattern
// patterns, and attributes from initial_set
// (if initial_set is not null).
// Returns pointer to merged set or nullptr, if there were no sets to merge.
const UnknownSet* AttributeUtility::MergeUnknowns(
absl::Span<const cel::Handle<cel::Value>> args,
absl::Span<const AttributeTrail> attrs, const UnknownSet* initial_set,
bool use_partial) const {
cel::AttributeSet attr_set = CheckForUnknowns(attrs, use_partial);
if (!attr_set.empty()) {
UnknownSet result_set(std::move(attr_set));
if (initial_set != nullptr) {
cel::base_internal::UnknownSetAccess::Add(result_set, *initial_set);
}
for (const auto& value : args) {
if (!value->Is<cel::UnknownValue>()) {
continue;
}
const auto& unknown_value = value.As<cel::UnknownValue>();
cel::base_internal::UnknownSetAccess::Add(
result_set, UnknownSet(unknown_value->attribute_set(),
unknown_value->function_result_set()));
}
return google::protobuf::Arena::Create<UnknownSet>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(memory_manager_),
std::move(result_set));
}
return MergeUnknowns(args, initial_set);
}
const UnknownSet* AttributeUtility::CreateUnknownSet(
cel::Attribute attr) const {
return google::protobuf::Arena::Create<UnknownSet>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(memory_manager_),
UnknownAttributeSet({std::move(attr)}));
}
const UnknownSet* AttributeUtility::CreateUnknownSet(
const cel::FunctionDescriptor& fn_descriptor, int64_t expr_id,
absl::Span<const cel::Handle<cel::Value>> args) const {
return google::protobuf::Arena::Create<UnknownSet>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(memory_manager_),
cel::FunctionResultSet(cel::FunctionResult(fn_descriptor, expr_id)));
}
} // namespace google::api::expr::runtime