Skip to content

Commit

Permalink
Add isInf and isNan (#40)
Browse files Browse the repository at this point in the history
And update to protovalidate v0.4.0
  • Loading branch information
Alfus authored Aug 23, 2023
1 parent 967fca3 commit 7a56344
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
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

0 comments on commit 7a56344

Please sign in to comment.