diff --git a/common/BUILD b/common/BUILD index 37dab5e05..da3295b5f 100644 --- a/common/BUILD +++ b/common/BUILD @@ -1021,11 +1021,30 @@ cc_test( cc_library( name = "standard_definitions", + srcs = [ + "standard_definitions.cc", + ], hdrs = [ "standard_definitions.h", ], deps = [ + "@com_google_absl//absl/base", "@com_google_absl//absl/strings:string_view", + "@com_google_absl//absl/types:span", + ], +) + +cc_test( + name = "standard_definitions_test", + srcs = [ + "standard_definitions_test.cc", + ], + deps = [ + ":standard_definitions", + "//internal:testing", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings:string_view", + "@com_google_absl//absl/types:span", ], ) diff --git a/common/standard_definitions.cc b/common/standard_definitions.cc new file mode 100644 index 000000000..38ad59dff --- /dev/null +++ b/common/standard_definitions.cc @@ -0,0 +1,235 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "common/standard_definitions.h" + +#include +#include + +#include "absl/base/call_once.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" + +namespace cel { + +namespace { + +static absl::once_flag all_overloads_sorted_flag; + +} // namespace + +absl::Span StandardOverloadIds::GetAllIds() { + static std::array kAll = { + // Add operator _+_ + kAddInt, + kAddUint, + kAddDouble, + kAddDurationDuration, + kAddDurationTimestamp, + kAddTimestampDuration, + kAddString, + kAddBytes, + kAddList, + // Subtract operator _-_ + kSubtractInt, + kSubtractUint, + kSubtractDouble, + kSubtractDurationDuration, + kSubtractTimestampDuration, + kSubtractTimestampTimestamp, + // Multiply operator _*_ + kMultiplyInt, + kMultiplyUint, + kMultiplyDouble, + // Division operator _/_ + kDivideInt, + kDivideUint, + kDivideDouble, + // Modulo operator _%_ + kModuloInt, + kModuloUint, + // Negation operator -_ + kNegateInt, + kNegateDouble, + // Logical operators + kNot, + kAnd, + kOr, + kConditional, + // Comprehension logic + kNotStrictlyFalse, + kNotStrictlyFalseDeprecated, + // Equality operators + kEquals, + kNotEquals, + // Relational operators + kLessBool, + kLessString, + kLessBytes, + kLessDuration, + kLessTimestamp, + kLessInt, + kLessIntUint, + kLessIntDouble, + kLessDouble, + kLessDoubleInt, + kLessDoubleUint, + kLessUint, + kLessUintInt, + kLessUintDouble, + kGreaterBool, + kGreaterString, + kGreaterBytes, + kGreaterDuration, + kGreaterTimestamp, + kGreaterInt, + kGreaterIntUint, + kGreaterIntDouble, + kGreaterDouble, + kGreaterDoubleInt, + kGreaterDoubleUint, + kGreaterUint, + kGreaterUintInt, + kGreaterUintDouble, + kGreaterEqualsBool, + kGreaterEqualsString, + kGreaterEqualsBytes, + kGreaterEqualsDuration, + kGreaterEqualsTimestamp, + kGreaterEqualsInt, + kGreaterEqualsIntUint, + kGreaterEqualsIntDouble, + kGreaterEqualsDouble, + kGreaterEqualsDoubleInt, + kGreaterEqualsDoubleUint, + kGreaterEqualsUint, + kGreaterEqualsUintInt, + kGreaterEqualsUintDouble, + kLessEqualsBool, + kLessEqualsString, + kLessEqualsBytes, + kLessEqualsDuration, + kLessEqualsTimestamp, + kLessEqualsInt, + kLessEqualsIntUint, + kLessEqualsIntDouble, + kLessEqualsDouble, + kLessEqualsDoubleInt, + kLessEqualsDoubleUint, + kLessEqualsUint, + kLessEqualsUintInt, + kLessEqualsUintDouble, + // Container operators + kIndexList, + kIndexMap, + kInList, + kInMap, + kSizeBytes, + kSizeList, + kSizeMap, + kSizeString, + kSizeBytesMember, + kSizeListMember, + kSizeMapMember, + kSizeStringMember, + // String functions + kContainsString, + kEndsWithString, + kStartsWithString, + // String RE2 functions + kMatches, + kMatchesMember, + // Timestamp / duration accessors + kTimestampToYear, + kTimestampToYearWithTz, + kTimestampToMonth, + kTimestampToMonthWithTz, + kTimestampToDayOfYear, + kTimestampToDayOfYearWithTz, + kTimestampToDayOfMonth, + kTimestampToDayOfMonthWithTz, + kTimestampToDayOfWeek, + kTimestampToDayOfWeekWithTz, + kTimestampToDate, + kTimestampToDateWithTz, + kTimestampToHours, + kTimestampToHoursWithTz, + kDurationToHours, + kTimestampToMinutes, + kTimestampToMinutesWithTz, + kDurationToMinutes, + kTimestampToSeconds, + kTimestampToSecondsWithTz, + kDurationToSeconds, + kTimestampToMilliseconds, + kTimestampToMillisecondsWithTz, + kDurationToMilliseconds, + // Type conversions + kToDyn, + // to_uint + kUintToUint, + kDoubleToUint, + kIntToUint, + kStringToUint, + // to_int + kUintToInt, + kDoubleToInt, + kIntToInt, + kStringToInt, + kTimestampToInt, + kDurationToInt, + // to_double + kDoubleToDouble, + kUintToDouble, + kIntToDouble, + kStringToDouble, + // to_bool + kBoolToBool, + kStringToBool, + // to_bytes + kBytesToBytes, + kStringToBytes, + // to_string + kStringToString, + kBytesToString, + kBoolToString, + kDoubleToString, + kIntToString, + kUintToString, + kDurationToString, + kTimestampToString, + // to_timestamp + kTimestampToTimestamp, + kIntToTimestamp, + kStringToTimestamp, + // to_duration + kDurationToDuration, + kIntToDuration, + kStringToDuration, + // to_type + kToType, + }; + + absl::call_once(all_overloads_sorted_flag, + [] { std::sort(kAll.begin(), kAll.end()); }); + + return kAll; +} + +bool StandardOverloadIds::HasId(absl::string_view id) { + absl::Span all_ids = GetAllIds(); + return std::binary_search(all_ids.begin(), all_ids.end(), id); +} + +} // namespace cel diff --git a/common/standard_definitions.h b/common/standard_definitions.h index 7480b67f4..75102811e 100644 --- a/common/standard_definitions.h +++ b/common/standard_definitions.h @@ -17,6 +17,7 @@ #define THIRD_PARTY_CEL_CPP_COMMON_STANDARD_DEFINITIONS_H_ #include "absl/strings/string_view.h" +#include "absl/types/span.h" namespace cel { @@ -342,6 +343,9 @@ struct StandardOverloadIds { static constexpr absl::string_view kStringToDuration = "string_to_duration"; // to_type static constexpr absl::string_view kToType = "type"; + + static absl::Span GetAllIds(); + static bool HasId(absl::string_view id); }; } // namespace cel diff --git a/common/standard_definitions_test.cc b/common/standard_definitions_test.cc new file mode 100644 index 000000000..c6b06bbf0 --- /dev/null +++ b/common/standard_definitions_test.cc @@ -0,0 +1,42 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "common/standard_definitions.h" + +#include "absl/container/flat_hash_set.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "internal/testing.h" + +namespace cel { +namespace { + +TEST(GetAllIds, ReturnsAllIds) { + absl::Span all_ids = + StandardOverloadIds::GetAllIds(); + absl::flat_hash_set id_set(all_ids.begin(), all_ids.end()); + + EXPECT_EQ(all_ids.size(), id_set.size()); +} + +TEST(HasId, ReturnsTrueForExistingIds) { + for (absl::string_view id : StandardOverloadIds::GetAllIds()) { + EXPECT_TRUE(StandardOverloadIds::HasId(id)); + } + + EXPECT_FALSE(StandardOverloadIds::HasId("foo")); +} + +} // namespace +} // namespace cel