Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ cc_library(
hdrs = [
"function_result.h",
],
deps = [":function_descriptor"],
deps = [
":function_descriptor",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
Expand Down
21 changes: 8 additions & 13 deletions base/function_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
#define THIRD_PARTY_CEL_CPP_BASE_FUNCTION_RESULT_H_

#include <cstdint>
#include <string>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "base/function_descriptor.h"

namespace cel {
Expand All @@ -32,37 +35,29 @@ class FunctionResult final {
FunctionResult& operator=(const FunctionResult&) = default;
FunctionResult& operator=(FunctionResult&&) = default;

FunctionResult(FunctionDescriptor descriptor, int64_t expr_id)
: descriptor_(std::move(descriptor)), expr_id_(expr_id) {}
explicit FunctionResult(std::string_view name) : name_(name) {}

// The descriptor of the called function that return Unknown.
const FunctionDescriptor& descriptor() const { return descriptor_; }

// The id of the |Expr| that triggered the function call step. Provided
// informationally -- if two different |Expr|s generate the same unknown call,
// they will be treated as the same unknown function result.
int64_t call_expr_id() const { return expr_id_; }
absl::string_view name() const ABSL_ATTRIBUTE_LIFETIME_BOUND { return name_; }

// Equality operator provided for testing. Compatible with set less-than
// comparator.
// Compares descriptor then arguments elementwise.
bool IsEqualTo(const FunctionResult& other) const {
return descriptor() == other.descriptor();
return name() == other.name();
}

// TODO(uncreated-issue/5): re-implement argument capture

private:
FunctionDescriptor descriptor_;
int64_t expr_id_;
std::string name_;
};

inline bool operator==(const FunctionResult& lhs, const FunctionResult& rhs) {
return lhs.IsEqualTo(rhs);
}

inline bool operator<(const FunctionResult& lhs, const FunctionResult& rhs) {
return lhs.descriptor() < rhs.descriptor();
return lhs.name() < rhs.name();
}

} // namespace cel
Expand Down
2 changes: 1 addition & 1 deletion eval/eval/attribute_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ UnknownValue AttributeUtility::CreateUnknownSet(
const cel::FunctionDescriptor& fn_descriptor, int64_t expr_id,
absl::Span<const cel::Value> args) const {
return cel::common_internal::MakeUnknownValue(
cel::Unknown(FunctionResultSet(FunctionResult(fn_descriptor, expr_id))));
cel::Unknown(FunctionResultSet(FunctionResult(fn_descriptor.name()))));
}

void AttributeUtility::Add(Accumulator& a, const cel::UnknownValue& v) const {
Expand Down
27 changes: 9 additions & 18 deletions eval/public/unknown_function_result_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ CelFunctionDescriptor kTwoInt("TwoInt", false,
CelFunctionDescriptor kOneInt("OneInt", false, {CelValue::Type::kInt64});

TEST(UnknownFunctionResult, Equals) {
UnknownFunctionResult call1(kTwoInt, /*expr_id=*/0);
UnknownFunctionResult call1(kTwoInt.name());

UnknownFunctionResult call2(kTwoInt, /*expr_id=*/0);
UnknownFunctionResult call2(kTwoInt.name());

EXPECT_TRUE(call1.IsEqualTo(call2));

UnknownFunctionResult call3(kOneInt, /*expr_id=*/0);
UnknownFunctionResult call3(kOneInt.name());

UnknownFunctionResult call4(kOneInt, /*expr_id=*/0);
UnknownFunctionResult call4(kOneInt.name());

EXPECT_TRUE(call3.IsEqualTo(call4));

Expand All @@ -57,25 +57,16 @@ TEST(UnknownFunctionResult, Equals) {
}

TEST(UnknownFunctionResult, InequalDescriptor) {
UnknownFunctionResult call1(kTwoInt, /*expr_id=*/0);
UnknownFunctionResult call1(kTwoInt.name());

UnknownFunctionResult call2(kOneInt, /*expr_id=*/0);
UnknownFunctionResult call2(kOneInt.name());

EXPECT_FALSE(call1.IsEqualTo(call2));

CelFunctionDescriptor one_uint("OneInt", false, {CelValue::Type::kUint64});

UnknownFunctionResult call3(kOneInt, /*expr_id=*/0);

UnknownFunctionResult call4(one_uint, /*expr_id=*/0);

EXPECT_FALSE(call3.IsEqualTo(call4));

UnknownFunctionResultSet call_set({call1, call3, call4});
EXPECT_EQ(call_set.size(), 3);
UnknownFunctionResultSet call_set({call1, call2});
EXPECT_EQ(call_set.size(), 2);
auto it = call_set.begin();
EXPECT_EQ(*it++, call3);
EXPECT_EQ(*it++, call4);
EXPECT_EQ(*it++, call2);
EXPECT_EQ(*it++, call1);
}

Expand Down
2 changes: 1 addition & 1 deletion eval/public/unknown_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using ::testing::UnorderedElementsAre;

UnknownFunctionResultSet MakeFunctionResult(Arena* arena, int64_t id) {
CelFunctionDescriptor desc("OneInt", false, {CelValue::Type::kInt64});
return UnknownFunctionResultSet(UnknownFunctionResult(desc, /*expr_id=*/0));
return UnknownFunctionResultSet(UnknownFunctionResult(desc.name()));
}

UnknownAttributeSet MakeAttribute(Arena* arena, int64_t id) {
Expand Down
2 changes: 1 addition & 1 deletion eval/tests/unknowns_end_to_end_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class UnknownsTest : public testing::Test {

MATCHER_P(FunctionCallIs, fn_name, "") {
const cel::FunctionResult& result = arg;
return result.descriptor().name() == fn_name;
return result.name() == fn_name;
}

MATCHER_P(AttributeIs, attr, "") {
Expand Down
Loading