Skip to content
Open
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
1 change: 1 addition & 0 deletions extensions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ cc_library(
srcs = ["math_ext.cc"],
hdrs = ["math_ext.h"],
deps = [
":math_ext_decls",
"//common:casting",
"//common:value",
"//eval/public:cel_function_registry",
Expand Down
29 changes: 19 additions & 10 deletions extensions/math_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ Value BitShiftRightUint(uint64_t lhs, int64_t rhs) {
} // namespace

absl::Status RegisterMathExtensionFunctions(FunctionRegistry& registry,
const RuntimeOptions& options) {
const RuntimeOptions& options,
int version) {
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<Value, int64_t>::RegisterGlobalOverload(
kMathMin, Identity<int64_t>, registry)));
Expand Down Expand Up @@ -360,6 +361,9 @@ absl::Status RegisterMathExtensionFunctions(FunctionRegistry& registry,
UnaryFunctionAdapter<absl::StatusOr<Value>,
ListValue>::RegisterGlobalOverload(kMathMax, MaxList,
registry)));
if (version == 0) {
return absl::OkStatus();
}

CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
Expand All @@ -370,15 +374,6 @@ absl::Status RegisterMathExtensionFunctions(FunctionRegistry& registry,
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
"math.round", RoundDouble, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
"math.sqrt", SqrtDouble, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, int64_t>::RegisterGlobalOverload(
"math.sqrt", SqrtInt, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, uint64_t>::RegisterGlobalOverload(
"math.sqrt", SqrtUint, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
"math.trunc", TruncDouble, registry)));
Expand Down Expand Up @@ -453,6 +448,20 @@ absl::Status RegisterMathExtensionFunctions(FunctionRegistry& registry,
(BinaryFunctionAdapter<Value, uint64_t, int64_t>::RegisterGlobalOverload(
"math.bitShiftRight", BitShiftRightUint, registry)));

if (version == 1) {
return absl::OkStatus();
}

CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, double>::RegisterGlobalOverload(
"math.sqrt", SqrtDouble, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, int64_t>::RegisterGlobalOverload(
"math.sqrt", SqrtInt, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<double, uint64_t>::RegisterGlobalOverload(
"math.sqrt", SqrtUint, registry)));

return absl::OkStatus();
}

Expand Down
6 changes: 4 additions & 2 deletions extensions/math_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
#include "absl/status/status.h"
#include "eval/public/cel_function_registry.h"
#include "eval/public/cel_options.h"
#include "extensions/math_ext_decls.h"
#include "runtime/function_registry.h"
#include "runtime/runtime_options.h"

namespace cel::extensions {

// Register extension functions for supporting mathematical operations above
// and beyond the set defined in the CEL standard environment.
absl::Status RegisterMathExtensionFunctions(FunctionRegistry& registry,
const RuntimeOptions& options);
absl::Status RegisterMathExtensionFunctions(
FunctionRegistry& registry, const RuntimeOptions& options,
int version = kMathExtensionLatestVersion);

absl::Status RegisterMathExtensionFunctions(
google::api::expr::runtime::CelFunctionRegistry* registry,
Expand Down
37 changes: 25 additions & 12 deletions extensions/strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,8 @@ absl::Status RegisterStringsDecls(TypeCheckerBuilder& builder, int version) {
} // namespace

absl::Status RegisterStringsFunctions(FunctionRegistry& registry,
const RuntimeOptions& options) {
CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::CreateDescriptor(
"join", /*receiver_style=*/true),
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::WrapFunction(
Join1)));
CEL_RETURN_IF_ERROR(registry.Register(
BinaryFunctionAdapter<absl::StatusOr<Value>, ListValue, StringValue>::
CreateDescriptor("join", /*receiver_style=*/true),
BinaryFunctionAdapter<absl::StatusOr<Value>, ListValue,
StringValue>::WrapFunction(Join2)));
const RuntimeOptions& options,
int version) {
CEL_RETURN_IF_ERROR(registry.Register(
BinaryFunctionAdapter<absl::StatusOr<Value>, StringValue, StringValue>::
CreateDescriptor("split", /*receiver_style=*/true),
Expand Down Expand Up @@ -350,7 +341,6 @@ absl::Status RegisterStringsFunctions(FunctionRegistry& registry,
int64_t>::CreateDescriptor("replace", /*receiver_style=*/true),
QuaternaryFunctionAdapter<absl::StatusOr<Value>, StringValue, StringValue,
StringValue, int64_t>::WrapFunction(Replace2)));
CEL_RETURN_IF_ERROR(RegisterStringFormattingFunctions(registry, options));
CEL_RETURN_IF_ERROR(
(BinaryFunctionAdapter<Value, StringValue,
int64_t>::RegisterMemberOverload("charAt", &CharAt,
Expand Down Expand Up @@ -388,9 +378,32 @@ absl::Status RegisterStringsFunctions(FunctionRegistry& registry,
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<StringValue, StringValue>::RegisterMemberOverload(
"trim", &Trim, registry)));
if (version == 0) {
return absl::OkStatus();
}

CEL_RETURN_IF_ERROR(RegisterStringFormattingFunctions(registry, options));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<StringValue, StringValue>::RegisterGlobalOverload(
"strings.quote", &Quote, registry)));
if (version == 1) {
return absl::OkStatus();
}

CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::CreateDescriptor(
"join", /*receiver_style=*/true),
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::WrapFunction(
Join1)));
CEL_RETURN_IF_ERROR(registry.Register(
BinaryFunctionAdapter<absl::StatusOr<Value>, ListValue, StringValue>::
CreateDescriptor("join", /*receiver_style=*/true),
BinaryFunctionAdapter<absl::StatusOr<Value>, ListValue,
StringValue>::WrapFunction(Join2)));
if (version == 2) {
return absl::OkStatus();
}

CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<StringValue, StringValue>::RegisterMemberOverload(
"reverse", &Reverse, registry)));
Expand Down
5 changes: 3 additions & 2 deletions extensions/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ namespace cel::extensions {
constexpr int kStringsExtensionLatestVersion = 4;

// Register extension functions for strings.
absl::Status RegisterStringsFunctions(FunctionRegistry& registry,
const RuntimeOptions& options);
absl::Status RegisterStringsFunctions(
FunctionRegistry& registry, const RuntimeOptions& options,
int version = kStringsExtensionLatestVersion);

absl::Status RegisterStringsFunctions(
google::api::expr::runtime::CelFunctionRegistry* registry,
Expand Down