Skip to content

Commit

Permalink
Update bindings to include newer APIs
Browse files Browse the repository at this point in the history
- c, cpp
- ffi
- 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 cee0210
Show file tree
Hide file tree
Showing 36 changed files with 945 additions and 492 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test-ffi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: bindings/c-cpp

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Test FFI
run: |
cargo build -r
cargo clippy --all-targets --no-deps -- -Dwarnings
working-directory: ./bindings/ffi
4 changes: 3 additions & 1 deletion .github/workflows/test-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
- uses: dtolnay/rust-toolchain@stable

- name: Building binding
run: cargo build --release --manifest-path bindings/java/Cargo.toml
run: |
cargo clippy --all-targets --no-deps -- -Dwarnings
cargo build --release --manifest-path bindings/java/Cargo.toml
- name: Build jar
run: mvn package
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ jobs:
run: |
pip3 install dist/regorus-*.whl
cd bindings/python
cargo clippy --all-targets --no-deps -- -Dwarnings
python3 test.py
1 change: 1 addition & 0 deletions .github/workflows/test-ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ jobs:
- name: Run ruby tests
run: |
cd bindings/ruby
cargo clippy --all-targets --no-deps -- -Dwarnings
bundle exec rake
2 changes: 2 additions & 0 deletions .github/workflows/test-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ jobs:
- name: Test wasm binding
run: |
cd bindings/wasm
cargo clippy --all-targets --no-deps -- -Dwarnings
wasm-pack build --target nodejs --release
wasm-pack test --release --node
node test.js
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ Cargo.lock
worktrees/

# build folders
**/build
**/build

# Generated C# bindings
**/*.g.cs

# Generated C, C++ headers
bindings/ffi/regorus.h
bindings/ffi/regorus.ffi.hpp
2 changes: 1 addition & 1 deletion bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ corrosion_import_crate(
FEATURES "regorus/semver"

# Link statically
CRATE_TYPES "staticlib"
CRATE_TYPES "cdylib"
)

add_executable(regorus_test main.c)
Expand Down
47 changes: 43 additions & 4 deletions bindings/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,61 @@ int main() {
goto error;
regorus_result_drop(r);

// Eval query
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay=x");
// Eval rule.
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay");
if (r.status != RegorusStatusOk)
goto error;

// Print output
printf("%s", r.output);
printf("%s\n", r.output);
regorus_result_drop(r);


// Free the engine.
regorus_engine_drop(engine);

// Create another engine.
engine = regorus_engine_new();

r = regorus_engine_add_policy(
engine,
"test.rego",
"package test\n"
"x = 1\n"
"message = `Hello`"
);

// Evaluate rule.
if (r.status != RegorusStatusOk)
goto error;

r = regorus_engine_set_enable_coverage(engine, true);
regorus_result_drop(r);

r = regorus_engine_eval_query(engine, "data.test.message");
if (r.status != RegorusStatusOk)
goto error;

// Print output
printf("%s\n", r.output);
regorus_result_drop(r);

// Print pretty coverage report.
r = regorus_engine_get_coverage_report_pretty(engine);
if (r.status != RegorusStatusOk)
goto error;

printf("%s\n", r.output);
regorus_result_drop(r);

// Free the engine.
regorus_engine_drop(engine);

return 0;

error:
printf("%s", r.error_message);
regorus_result_drop(r);
regorus_engine_drop(engine);

return 1;
}
2 changes: 1 addition & 1 deletion bindings/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ corrosion_import_crate(
FEATURES "regorus/semver"

# Link statically
CRATE_TYPES "staticlib")
CRATE_TYPES "cdylib")

add_executable(regorus_test main.cpp)
# Add path to <regorus-source-folder>/bindings/ffi
Expand Down
16 changes: 13 additions & 3 deletions bindings/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ void example()
// Create engine
regorus::Engine engine;

engine.set_enable_coverage(true);

// Add policies.
engine.add_policy("objects.rego",R"(package objects
Expand Down Expand Up @@ -67,6 +69,14 @@ f := e["dev"])");
} else {
std::cerr<<result.error()<<std::endl;
}

// Print coverage report
auto result1 = engine.get_coverage_report_pretty();
if (result1) {
std::cout<<result1.output()<<std::endl;
} else {
std::cerr<<result1.error()<<std::endl;
}
}

int main() {
Expand Down Expand Up @@ -99,21 +109,21 @@ int main() {
}
}

// Set input and eval query.
// Set input and eval rule.
{
auto result = engine.set_input_from_json_file("../../../tests/aci/input.json");
if (!result) {
std::cerr<<result.error()<<std::endl;
return -1;
}
}
auto result = engine.eval_query("data.framework.mount_overlay = x");
auto result = engine.eval_rule("data.framework.mount_overlay");
if (!result) {
std::cerr<<result.error()<<std::endl;
return -1;
}

std::cout<<result.output()<<std::endl;

example();
}
20 changes: 20 additions & 0 deletions bindings/cpp/regorus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ namespace regorus {
return Result(regorus_engine_eval_query(engine, query));
}

Result eval_rule(const char* rule) {
return Result(regorus_engine_eval_rule(engine, rule));
}

Result set_enable_coverage(bool enable) {
return Result(regorus_engine_set_enable_coverage(engine, enable));
}

Result clear_coverage_data() {
return Result(regorus_engine_clear_coverage_data(engine));
}

Result get_coverage_report() {
return Result(regorus_engine_get_coverage_report(engine));
}

Result get_coverage_report_pretty() {
return Result(regorus_engine_get_coverage_report_pretty(engine));
}

~Engine() {
regorus_engine_drop(engine);
}
Expand Down
3 changes: 2 additions & 1 deletion bindings/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ regorus = { path = "../..", default-features = false }
serde_json = "1.0.113"

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

[build-dependencies]
Expand Down
87 changes: 0 additions & 87 deletions bindings/ffi/RegorusFFI.g.cs

This file was deleted.

Loading

0 comments on commit cee0210

Please sign in to comment.