fix: C# EvalRule (#387)

- Fix EvalRule to call EvalRule instead of EvalQuery
- Also fix clippy errors

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2025-04-04 15:44:16 -07:00
committed by GitHub
parent 2749e820c4
commit ab93c07773
7 changed files with 26 additions and 30 deletions

View File

@@ -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).

View File

@@ -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));
}
}

View File

@@ -8,7 +8,7 @@
<LangVersion>10.0</LangVersion>
<!-- See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-pack -->
<VersionPrefix>0.4.0</VersionPrefix>
<VersionPrefix>0.5.0</VersionPrefix>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

View File

@@ -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.");
}

View File

@@ -11,6 +11,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="regorus" Version="0.4.0-preview.1"/>
<PackageReference Include="regorus" Version="0.5.0"/>
</ItemGroup>
</Project>

View File

@@ -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;
}

View File

@@ -135,40 +135,28 @@ impl From<f64> for Number {
impl Number {
pub fn as_u128(&self) -> Option<u128> {
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<i128> {
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<u64> {
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<i64> {
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<BigInt> {
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,
}
}