From ab93c07773bbf50b64dea59637f3f94c45e04623 Mon Sep 17 00:00:00 2001 From: Anand Krishnamoorthi <35780660+anakrish@users.noreply.github.com> Date: Fri, 4 Apr 2025 15:44:16 -0700 Subject: [PATCH] fix: C# EvalRule (#387) - Fix EvalRule to call EvalRule instead of EvalQuery - Also fix clippy errors Signed-off-by: Anand Krishnamoorthi --- README.md | 4 ++-- bindings/csharp/Regorus/Regorus.cs | 2 +- bindings/csharp/Regorus/Regorus.csproj | 2 +- bindings/csharp/TestApp/Program.cs | 19 +++++++++++++++---- bindings/csharp/TestApp/TestApp.csproj | 2 +- src/builtins/encoding.rs | 2 +- src/number.rs | 25 +++++-------------------- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c12f3c7..0b7f062 100644 --- a/README.md +++ b/README.md @@ -107,11 +107,11 @@ Regorus can be used from a variety of languages: - *C*: C binding is generated using [cbindgen](https://github.com/mozilla/cbindgen). [corrosion-rs](https://github.com/corrosion-rs/corrosion) can be used to seamlessly use Regorous - in your CMake based projects. See [bindings/c](https://github.com/microsoft/regorus/tree/main/bindings/c). + in your CMake based projects. See [bindings/c](https://github.com/microsoft/regorus/tree/main/bindings/c). - *C freestanding*: [bindings/c_no_std](https://github.com/microsoft/regorus/tree/main/bindings/c_no_std) shows how to use Regorus from C environments without a libc. - *C++*: C++ binding is generated using [cbindgen](https://github.com/mozilla/cbindgen). [corrosion-rs](https://github.com/corrosion-rs/corrosion) can be used to seamlessly use Regorous - in your CMake based projects. See [bindings/cpp](https://github.com/microsoft/regorus/tree/main/bindings/cpp). + in your CMake based projects. See [bindings/cpp](https://github.com/microsoft/regorus/tree/main/bindings/cpp). - *C#*: C# binding is generated using [csbindgen](https://github.com/Cysharp/csbindgen). See [bindings/csharp](https://github.com/microsoft/regorus/tree/main/bindings/csharp) for an example of how to build and use Regorus in your C# projects. - *Golang*: The C bindings are exposed to Golang via [CGo](https://pkg.go.dev/cmd/cgo). See [bindings/go](https://github.com/microsoft/regorus/tree/main/bindings/go) for an example of how to build and use Regorus in your Go projects. - *Python*: Python bindings are generated using [pyo3](https://github.com/PyO3/pyo3). Wheels are created using [maturin](https://github.com/PyO3/maturin). See [bindings/python](https://github.com/microsoft/regorus/tree/main/bindings/python). diff --git a/bindings/csharp/Regorus/Regorus.cs b/bindings/csharp/Regorus/Regorus.cs index 69c5e9a..e711d8c 100644 --- a/bindings/csharp/Regorus/Regorus.cs +++ b/bindings/csharp/Regorus/Regorus.cs @@ -168,7 +168,7 @@ namespace Regorus var ruleBytes = NullTerminatedUTF8Bytes(rule); fixed (byte* rulePtr = ruleBytes) { - return CheckAndDropResult(Regorus.Internal.API.regorus_engine_eval_query(E, rulePtr)); + return CheckAndDropResult(Regorus.Internal.API.regorus_engine_eval_rule(E, rulePtr)); } } diff --git a/bindings/csharp/Regorus/Regorus.csproj b/bindings/csharp/Regorus/Regorus.csproj index a974a48..81c7a8e 100644 --- a/bindings/csharp/Regorus/Regorus.csproj +++ b/bindings/csharp/Regorus/Regorus.csproj @@ -8,7 +8,7 @@ 10.0 - 0.4.0 + 0.5.0 $(VersionSuffix) README.md diff --git a/bindings/csharp/TestApp/Program.cs b/bindings/csharp/TestApp/Program.cs index 565470b..b7e8617 100644 --- a/bindings/csharp/TestApp/Program.cs +++ b/bindings/csharp/TestApp/Program.cs @@ -42,7 +42,7 @@ w.Restart(); // Set input and eval rule. engine.SetInputFromJsonFile("../../../tests/aci/input.json"); -var value = engine.EvalQuery("data.framework.mount_overlay"); +var value = engine.EvalRule("data.framework.mount_overlay"); #if NET8_0_OR_GREATER var valueDoc = System.Text.Json.JsonDocument.Parse(value); @@ -59,7 +59,7 @@ var evalTicks = w.ElapsedTicks; Console.WriteLine("Engine creation took {0} msecs", (newEngineTicks * nanosecPerTick) / (1000.0 * 1000.0)); Console.WriteLine("Load policies and data took {0} msecs", (loadPoliciesTicks * nanosecPerTick) / (1000.0 * 1000.0)); -Console.WriteLine("EvalQuery took {0} msecs", (evalTicks * nanosecPerTick) / (1000.0 * 1000.0)); +Console.WriteLine("EvalRule took {0} msecs", (evalTicks * nanosecPerTick) / (1000.0 * 1000.0)); engine = new Regorus.Engine(); engine.AddPolicy( @@ -67,5 +67,16 @@ engine.AddPolicy( "package test\nx = 1\nmessage = `Hello`"); engine.SetEnableCoverage(true); -Console.WriteLine("{0}", engine.EvalRule("data.test.message")); -Console.WriteLine("{0}", engine.GetCoverageReportPretty()); +Console.WriteLine("data.test.message: {0}", engine.EvalRule("data.test.message")); +Console.WriteLine("Coverage Report:\n{0}", engine.GetCoverageReportPretty()); + +if (engine.EvalRule("data.test.message") != "\"Hello\"") +{ + Console.WriteLine("Failure."); + System.Environment.Exit(1); +} +else +{ + Console.WriteLine("Success."); +} + diff --git a/bindings/csharp/TestApp/TestApp.csproj b/bindings/csharp/TestApp/TestApp.csproj index 4f8ddc0..faff408 100644 --- a/bindings/csharp/TestApp/TestApp.csproj +++ b/bindings/csharp/TestApp/TestApp.csproj @@ -11,6 +11,6 @@ - + diff --git a/src/builtins/encoding.rs b/src/builtins/encoding.rs index 4a71811..73b8888 100644 --- a/src/builtins/encoding.rs +++ b/src/builtins/encoding.rs @@ -224,7 +224,7 @@ fn urlquery_decode( let mut query_str = "".to_owned(); for (k, v) in url.query_pairs() { query_str += &k; - if v != "" { + if !v.is_empty() { query_str += "="; query_str += &v; } diff --git a/src/number.rs b/src/number.rs index ce78e11..afe0104 100644 --- a/src/number.rs +++ b/src/number.rs @@ -135,40 +135,28 @@ impl From for Number { impl Number { pub fn as_u128(&self) -> Option { match self { - Big(b) if b.is_integer() => match u128::try_from(&b.d) { - Ok(v) => Some(v), - _ => None, - }, + Big(b) if b.is_integer() => u128::try_from(&b.d).ok(), _ => None, } } pub fn as_i128(&self) -> Option { match self { - Big(b) if b.is_integer() => match i128::try_from(&b.d) { - Ok(v) => Some(v), - _ => None, - }, + Big(b) if b.is_integer() => i128::try_from(&b.d).ok(), _ => None, } } pub fn as_u64(&self) -> Option { match self { - Big(b) if b.is_integer() => match u64::try_from(&b.d) { - Ok(v) => Some(v), - _ => None, - }, + Big(b) if b.is_integer() => u64::try_from(&b.d).ok(), _ => None, } } pub fn as_i64(&self) -> Option { match self { - Big(b) if b.is_integer() => match i64::try_from(&b.d) { - Ok(v) => Some(v), - _ => None, - }, + Big(b) if b.is_integer() => i64::try_from(&b.d).ok(), _ => None, } } @@ -317,10 +305,7 @@ impl Number { fn ensure_integer(&self) -> Option { match self { - Big(a) if a.is_integer() => match BigInt::try_from(&a.d) { - Ok(v) => Some(v), - _ => None, - }, + Big(a) if a.is_integer() => BigInt::try_from(&a.d).ok(), _ => None, } }