This directory contains examples of how to use the library from the different languages we support (other than C++. For C++ examples see the tests or tools directories).
At the moment we support C and C++ only, but we plan to support Rust, Python, and .NET wrappers, with more as demand dictates.
The best way to get started with the C++ API is to look at the rego tool. It exercises most of the API and is a good starting point for understanding how to use the library.
A more in-depth example which performs node matching and other more complex
operations can be see in the TestCase
class from the
test driver.
The C example is a simple command line tool that takes one or more Rego data, module, and input files and evaluates a query against them.
To build it on Linux:
a@host:rego-cpp$ cd examples/c
a@host:c$ mkdir build
a@host:c$ cd build
a@host:build$ cmake .. --preset release-clang
a@host:build$ ninja install
and on Windows:
cd examples\c
mkdir build
cd build
cmake .. --preset release
cmake --build . --config Release --target INSTALL
The resulting executable can be called from within the dist
directory:
a@host:build$ cd dist
a@host:dist$ ./bin/regoc -d examples/scalars.rego -q data.scalars.greeting
"Hello"
The Python example is a simple command line tool that takes zero or more Rego data, module, and input files and evaluates a query against them.
Examples:
a@host:python$ pip install regopy
a@host:python$ python rego.py -d examples/scalars.rego data.scalars.greeting
"Hello"
a@host:python$ python rego.py -d examples/objects.rego data.objects.sites[1].name
"smoke1"
a@host:python$ python rego.py -d examples/data0.json -d examples/data1.json -d examples/objects.rego -i examples/input0.json "[data.one, input.b, data.objects.sites[1]]"
[{"bar":"Foo", "baz":5, "be":true, "bop":23.4}, "20", {"name":"smoke1"}]
a@host:python$ python rego.py "x=5; y=x + (2 - 4 * 0.25) * -3 + 7.4"
x = 5
y = 9.4
a@host:python$ python rego.py -d examples/bodies.rego -i examples/input1.json data.bodies.e
{"one":15, "two": 15}
The Rust example is another simple command line tool that takes zero or more Rego data, module, and input files and evaluates a query against them.
Examples:
a@host:rust$ cargo run -- -d examples/scalars.rego data.scalars.greeting
"Hello"
a@host:rust$ cargo run -- -d examples/objects.rego data.objects.sites[1].name
"smoke1"
a@host:rust$ cargo run -- -d examples/data0.json -d examples/data1.json -d examples/objects.rego -i examples/input0.json "[data.one, input.b, data.objects.sites[1]]"
[{"bar":"Foo", "baz":5, "be":true, "bop":23.4}, "20", {"name":"smoke1"}]
a@host:rust$ cargo run -- "x=5; y=x + (2 - 4 * 0.25) * -3 + 7.4"
x = 5
y = 9.4
a@host:rust$ cargo run -- -d examples/bodies.rego -i examples/input1.json data.bodies.e
{"one":15, "two": 15}