Skip to content

Commit

Permalink
Update bindings to include newer APIs
Browse files Browse the repository at this point in the history
- Java
- Python
- WASM

`arc` feature is turned on for all bindings
Use pretty string instead of colored string.

Signed-off-by: Anand Krishnamoorthi <[email protected]>
  • Loading branch information
anakrish committed May 25, 2024
1 parent 33fe9d5 commit a79ce95
Show file tree
Hide file tree
Showing 25 changed files with 675 additions and 226 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ jobs:
run: |
cd bindings/wasm
wasm-pack build --target nodejs --release
wasm-pack test --release --node
node test.js
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ Cargo.lock
worktrees/

# build folders
**/build
**/build

# Generated C# bindings
**/*.g.cs
2 changes: 1 addition & 1 deletion bindings/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ regorus = { path = "../..", default-features = false }
serde_json = "1.0.113"

[features]
default = ["std", "regorus/full-opa"]
default = ["std", "regorus/arc", "regorus/full-opa"]
std = ["regorus/std"]
custom_allocator = []

Expand Down
87 changes: 0 additions & 87 deletions bindings/ffi/RegorusFFI.g.cs

This file was deleted.

2 changes: 2 additions & 0 deletions bindings/ffi/regorus.ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ RegorusResult regorus_engine_add_policy_from_file(RegorusEngine *engine, const c
/// * `data`: JSON encoded value to be used as policy data.
RegorusResult regorus_engine_add_data_json(RegorusEngine *engine, const char *data);

RegorusResult regorus_engine_get_packages(RegorusEngine *engine);

RegorusResult regorus_engine_add_data_from_json_file(RegorusEngine *engine, const char *path);

/// Clear policy data.
Expand Down
2 changes: 2 additions & 0 deletions bindings/ffi/regorus.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct RegorusResult regorus_engine_add_policy_from_file(struct RegorusEngine *e
*/
struct RegorusResult regorus_engine_add_data_json(struct RegorusEngine *engine, const char *data);

struct RegorusResult regorus_engine_get_packages(struct RegorusEngine *engine);

struct RegorusResult regorus_engine_add_data_from_json_file(struct RegorusEngine *engine,
const char *path);

Expand Down
12 changes: 12 additions & 0 deletions bindings/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ pub extern "C" fn regorus_engine_add_data_json(
}())
}

/// Get list of loaded Rego packages as JSON.
///
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_packages
/// * `data`: JSON encoded value to be used as policy data.
#[no_mangle]
pub extern "C" fn regorus_engine_get_packages(engine: *mut RegorusEngine) -> RegorusResult {
to_regorus_string_result(|| -> Result<String> {
serde_json::to_string_pretty(&to_ref(&engine)?.engine.get_packages()?)
.map_err(anyhow::Error::msg)
}())
}

#[cfg(feature = "std")]
#[no_mangle]
pub extern "C" fn regorus_engine_add_data_from_json_file(
Expand Down
5 changes: 4 additions & 1 deletion bindings/java/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ keywords = ["interpreter", "opa", "policy-as-code", "rego"]
[lib]
crate-type = ["cdylib"]

[features]
default = ["regorus/std", "regorus/full-opa"]

[dependencies]
anyhow = "1.0"
serde_json = "1.0.112"
jni = "0.21.1"
regorus = { path = "../.." }
regorus = { path = "../..", default-features = false, features = ["arc"] }
22 changes: 19 additions & 3 deletions bindings/java/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@
public class Test {
public static void main(String[] args) {
try (Engine engine = new Engine()) {
engine.addPolicy(
var pkg = engine.addPolicy(
"hello.rego",
"package test\nmessage = concat(\", \", [input.message, data.message])"
"package test\nx=1\nmessage = concat(\", \", [input.message, data.message])"
);
System.out.println("Loaded package " + pkg);


engine.addDataJson("{\"message\":\"World!\"}");
engine.setInputJson("{\"message\":\"Hello\"}");
String resJson = engine.evalQuery("data.test.message");

// Evaluate query.
String resJson = engine.evalQuery("data.test.message");
System.out.println(resJson);

// Enable coverage.
engine.setEnableCoverage(true);

// Evaluate rule.
String valueJson = engine.evalRule("data.test.message");
System.out.println(valueJson);

var coverage = engine.getCoverageReport();
System.out.println(coverage);

System.out.println(engine.getCoverageReportPretty());
}
}
}
72 changes: 68 additions & 4 deletions bindings/java/com_microsoft_regorus_Engine.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a79ce95

Please sign in to comment.