Skip to content
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

Add isInf and isNan #40

Merged
merged 2 commits into from
Aug 23, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BIN := .tmp/bin
COPYRIGHT_YEARS := 2023
LICENSE_IGNORE := -e internal/testdata/
LICENSE_HEADER_VERSION := 0294fdbe1ce8649ebaf5e87e8cdd588e33730bbb
PROTOVALIDATE_VERSION ?= v0.3.1
PROTOVALIDATE_VERSION ?= v0.4.0

# Set to use a different compiler. For example, `GO=go1.18rc1 make test`.
GO ?= go
Expand Down
6 changes: 3 additions & 3 deletions bazel/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ _dependencies = {
"patch_args": ["-p1"],
},
"com_github_bufbuild_protovalidate": {
"sha256": "727601c86e8f7b0ea1a00952d431450948933db44f26cf4fab046492f1d1b029",
"strip_prefix": "protovalidate-0.2.7",
"sha256": "a76a17d540f9c3e2f042517059dc50b54dab9eb9350af8901dbb23c135525f31",
"strip_prefix": "protovalidate-0.4.0",
"urls": [
"https://github.com/bufbuild/protovalidate/archive/v0.2.7.tar.gz",
"https://github.com/bufbuild/protovalidate/archive/v0.4.0.tar.gz",
],
},
}
Expand Down
55 changes: 52 additions & 3 deletions buf/validate/internal/extra_func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

#include "buf/validate/internal/extra_func.h"

#include <algorithm>
#include <string>
#include <string_view>

#include "absl/strings/match.h"
#include <algorithm>
#include <string>

#include "absl/strings/str_split.h"
#include "buf/validate/internal/string_format.h"
#include "eval/public/cel_function_adapter.h"
Expand Down Expand Up @@ -52,6 +50,41 @@ bool isPathValid(const std::string_view path) {

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

cel::CelValue isNan(google::protobuf::Arena* arena, cel::CelValue rhs) {
if (!rhs.IsDouble()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
arena, absl::StatusCode::kInvalidArgument, "expected a double value");
return cel::CelValue::CreateError(error);
}
return cel::CelValue::CreateBool(std::isnan(rhs.DoubleOrDie()));
}

cel::CelValue isInfX(google::protobuf::Arena* arena, cel::CelValue rhs, cel::CelValue sign) {
if (!rhs.IsDouble()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
arena, absl::StatusCode::kInvalidArgument, "expected a double value");
return cel::CelValue::CreateError(error);
}
if (!sign.IsInt64()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
arena, absl::StatusCode::kInvalidArgument, "expected an int64 value");
return cel::CelValue::CreateError(error);
}
double value = rhs.DoubleOrDie();
int64_t sign_value = sign.Int64OrDie();
if (sign_value > 0) {
return cel::CelValue::CreateBool(std::isinf(value) && value > 0);
} else if (sign_value < 0) {
return cel::CelValue::CreateBool(std::isinf(value) && value < 0);
} else {
return cel::CelValue::CreateBool(std::isinf(value));
}
}

cel::CelValue isInf(google::protobuf::Arena* arena, cel::CelValue rhs) {
return isInfX(arena, rhs, cel::CelValue::CreateInt64(0));
}

cel::CelValue unique(google::protobuf::Arena* arena, cel::CelValue rhs) {
if (!rhs.IsList()) {
auto* error = google::protobuf::Arena::Create<cel::CelError>(
Expand Down Expand Up @@ -250,6 +283,22 @@ absl::Status RegisterExtraFuncs(
if (!status.ok()) {
return status;
}
auto isNanStatus = cel::FunctionAdapter<cel::CelValue, cel::CelValue>::CreateAndRegister(
"isNan", true, &isNan, &registry);
if (!isNanStatus.ok()) {
return isNanStatus;
}
auto isInfXStatus =
cel::FunctionAdapter<cel::CelValue, cel::CelValue, cel::CelValue>::CreateAndRegister(
"isInf", true, &isInfX, &registry);
if (!isInfXStatus.ok()) {
return isInfXStatus;
}
auto isInfStatus = cel::FunctionAdapter<cel::CelValue, cel::CelValue>::CreateAndRegister(
"isInf", true, &isInf, &registry);
if (!isInfStatus.ok()) {
return isInfStatus;
}
auto uniqueStatus = cel::FunctionAdapter<cel::CelValue, cel::CelValue>::CreateAndRegister(
"unique", true, &unique, &registry);
if (!uniqueStatus.ok()) {
Expand Down