-
Notifications
You must be signed in to change notification settings - Fork 61
Regorus extension to invoke C# code #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Steps to run invoke example | ||
|
|
||
| 1. Install dotnet-script | ||
| 1. `dotnet build` in `bindings/csharp/Regorus` | ||
| 1. `cargo build --release` in `bindings/ffi` | ||
| 1. Copy `bindings/ffi/target/debug/regorus_ffi.dll` to `bindings/csharp/scripts` | ||
| 1. `cd` to `bindings/csharp/scripts` | ||
| 1. Run `dotnet script invoke.csx` | ||
|
|
||
| All at once as a single command: | ||
| ``` | ||
| cd bindings/csharp/Regorus && dotnet build && cd ../../.. && cd bindings/ffi && cargo build --release --features rego-extensions,rego-builtin-extensions && cd ../.. && cp bindings/ffi/target/release/regorus_ffi.dll bindings/csharp/scripts && cd bindings/csharp/scripts && dotnet script invoke.csx && cd ../../.. | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #r "../Regorus/bin/Debug/netstandard2.1/Regorus.dll" | ||
| // No direct reference to regorus_ffi.dll as it's a native DLL | ||
| #r "nuget: Newtonsoft.Json, 13.0.2" | ||
| #r "nuget: System.Data.Common, 4.3.0" | ||
|
|
||
| // Create a new engine | ||
| var engine = new Regorus.Engine(); | ||
|
|
||
| // Enable the invoke extension | ||
| bool enableResult = engine.EnableInvoke(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To catch regressions, I'd prefer a test over a script. |
||
| Console.WriteLine($"Invoke extension enabled: {enableResult}"); | ||
|
|
||
| // Register a callback function | ||
| bool registerResult = engine.RegisterCallback("test_callback", payload => { | ||
| Console.WriteLine($"Called with payload: {payload}"); | ||
|
|
||
| if (payload is System.Text.Json.JsonElement jsonElement) | ||
| { | ||
| // Access properties from JsonElement | ||
| var testValue = jsonElement.GetProperty("value").GetInt32(); | ||
|
|
||
| // Return a response object that will be serialized to JSON | ||
| return new Dictionary<string, object> | ||
| { | ||
| ["value"] = testValue * 2, | ||
| ["message"] = "Processing complete" | ||
| }; | ||
| } | ||
|
|
||
| return null; | ||
| }); | ||
|
|
||
| Console.WriteLine($"Callback registration result: {registerResult}"); | ||
|
|
||
| // Add a policy that uses the callback | ||
| engine.AddPolicy("example.rego", @" | ||
| package example | ||
|
|
||
| import future.keywords.if | ||
|
|
||
| double_value := invoke(""test_callback"", {""value"": 42}).value | ||
| "); | ||
|
|
||
| // Evaluate query | ||
| var result = engine.EvalQuery("data.example.double_value"); | ||
| Console.WriteLine($"Result: {result}"); | ||
|
|
||
| // Unregister the callback when done | ||
| engine.UnregisterCallback("test_callback"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: An expectation on the Rust side is that an Engine is efficient to clone. It is much faster to add policies to an engine and clone it many times than creating many instances of the engine and adding policies to them individually.
Cloning an engine will also clone the extension. And a clone could be used from another thread. This means that the extension must be stateless or must be safe to be called from multiple threads.