Skip to content

Implement getField CEL function #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 buf/validate/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_cel_cpp//eval/public:cel_function_adapter",
"@com_google_cel_cpp//eval/public:cel_function_registry",
"@com_google_cel_cpp//eval/public/containers:field_access",
"@com_google_cel_cpp//eval/public/containers:container_backed_map_impl",
"@com_googlesource_code_re2//:re2",
],
Expand Down
33 changes: 33 additions & 0 deletions buf/validate/internal/extra_func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "eval/public/cel_function_adapter.h"
#include "eval/public/cel_value.h"
#include "eval/public/containers/container_backed_map_impl.h"
#include "eval/public/containers/field_access.h"
#include "google/protobuf/arena.h"
#include "re2/re2.h"

Expand All @@ -52,6 +53,32 @@ bool isPathValid(const std::string_view path) {

namespace cel = google::api::expr::runtime;

cel::CelValue getField(
google::protobuf::Arena* arena, cel::CelValue msgval, cel::CelValue nameval) {
if (!msgval.IsMessage()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
arena, absl::StatusCode::kInvalidArgument, "expected a message value for first argument");
return cel::CelValue::CreateError(error);
}
if (!nameval.IsString()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
arena, absl::StatusCode::kInvalidArgument, "expected a string value for second argument");
return cel::CelValue::CreateError(error);
}
const auto* message = msgval.MessageOrDie();
auto name = nameval.StringOrDie();
const auto* field = message->GetDescriptor()->FindFieldByName(name.value());
if (field == nullptr) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
arena, absl::StatusCode::kInvalidArgument, "no such field");
return cel::CelValue::CreateError(error);
}
if (cel::CelValue result; cel::CreateValueFromSingleField(message, field, arena, &result).ok()) {
return result;
}
return cel::CelValue::CreateNull();
}

cel::CelValue isNan(google::protobuf::Arena* arena, cel::CelValue rhs) {
if (!rhs.IsDouble()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
Expand Down Expand Up @@ -352,6 +379,12 @@ absl::Status RegisterExtraFuncs(
if (!status.ok()) {
return status;
}
auto getFieldStatus =
cel::FunctionAdapter<cel::CelValue, cel::CelValue, cel::CelValue>::CreateAndRegister(
"getField", false, &getField, &registry);
if (!getFieldStatus.ok()) {
return getFieldStatus;
}
auto isNanStatus = cel::FunctionAdapter<cel::CelValue, cel::CelValue>::CreateAndRegister(
"isNan", true, &isNan, &registry);
if (!isNanStatus.ok()) {
Expand Down
Loading