From a628731aa3cbde442588f05fbd89745d03a3c0f7 Mon Sep 17 00:00:00 2001 From: Alfred Fuller Date: Wed, 23 Aug 2023 12:47:16 -0700 Subject: [PATCH 1/2] Add isInf and isNan --- bazel/deps.bzl | 6 ++-- buf/validate/internal/extra_func.cc | 55 +++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/bazel/deps.bzl b/bazel/deps.bzl index a220ad6..a600dfb 100644 --- a/bazel/deps.bzl +++ b/bazel/deps.bzl @@ -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", ], }, } diff --git a/buf/validate/internal/extra_func.cc b/buf/validate/internal/extra_func.cc index 5eb2f28..a273b81 100644 --- a/buf/validate/internal/extra_func.cc +++ b/buf/validate/internal/extra_func.cc @@ -14,13 +14,11 @@ #include "buf/validate/internal/extra_func.h" +#include #include #include #include "absl/strings/match.h" -#include -#include - #include "absl/strings/str_split.h" #include "buf/validate/internal/string_format.h" #include "eval/public/cel_function_adapter.h" @@ -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( + 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( + arena, absl::StatusCode::kInvalidArgument, "expected a double value"); + return cel::CelValue::CreateError(error); + } + if (!sign.IsInt64()) { + auto* error = google::protobuf::Arena::Create( + 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( @@ -250,6 +283,22 @@ absl::Status RegisterExtraFuncs( if (!status.ok()) { return status; } + auto isNanStatus = cel::FunctionAdapter::CreateAndRegister( + "isNan", true, &isNan, ®istry); + if (!isNanStatus.ok()) { + return isNanStatus; + } + auto isInfXStatus = + cel::FunctionAdapter::CreateAndRegister( + "isInf", true, &isInfX, ®istry); + if (!isInfXStatus.ok()) { + return isInfXStatus; + } + auto isInfStatus = cel::FunctionAdapter::CreateAndRegister( + "isInf", true, &isInf, ®istry); + if (!isInfStatus.ok()) { + return isInfStatus; + } auto uniqueStatus = cel::FunctionAdapter::CreateAndRegister( "unique", true, &unique, ®istry); if (!uniqueStatus.ok()) { From 83ee750ea13e26861186a4dbce5cd8f3c534db38 Mon Sep 17 00:00:00 2001 From: Alfred Fuller Date: Wed, 23 Aug 2023 12:48:36 -0700 Subject: [PATCH 2/2] update to v0.4.0 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9babb12..bbd3410 100644 --- a/Makefile +++ b/Makefile @@ -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