forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_utility.cc
More file actions
114 lines (100 loc) · 3.71 KB
/
Copy pathattribute_utility.cc
File metadata and controls
114 lines (100 loc) · 3.71 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
#include "eval/eval/attribute_utility.h"
#include "absl/status/status.h"
#include "eval/public/cel_value.h"
#include "eval/public/unknown_attribute_set.h"
#include "eval/public/unknown_set.h"
#include "absl/status/statusor.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
using google::protobuf::Arena;
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()) ==
CelAttributePattern::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 == CelAttributePattern::MatchType::FULL ||
(use_partial &&
current_match == CelAttributePattern::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 CelValue> args, const UnknownSet* initial_set) const {
const UnknownSet* result = initial_set;
for (const auto& value : args) {
if (!value.IsUnknownSet()) continue;
auto current_set = value.UnknownSetOrDie();
if (result == nullptr) {
result = current_set;
} else {
result = Arena::Create<UnknownSet>(arena_, *result, *current_set);
}
}
return result;
}
// 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.
UnknownAttributeSet AttributeUtility::CheckForUnknowns(
absl::Span<const AttributeTrail> args, bool use_partial) const {
std::vector<const CelAttribute*> unknown_attrs;
for (auto trail : args) {
if (CheckForUnknown(trail, use_partial)) {
unknown_attrs.push_back(trail.attribute());
}
}
return UnknownAttributeSet(unknown_attrs);
}
// 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 CelValue> args, absl::Span<const AttributeTrail> attrs,
const UnknownSet* initial_set, bool use_partial) const {
UnknownAttributeSet attr_set = CheckForUnknowns(attrs, use_partial);
if (!attr_set.attributes().empty()) {
if (initial_set != nullptr) {
initial_set =
Arena::Create<UnknownSet>(arena_, *initial_set, UnknownSet(attr_set));
} else {
initial_set = Arena::Create<UnknownSet>(arena_, attr_set);
}
}
return MergeUnknowns(args, initial_set);
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google