![]() |
rego-cpp 1.2.0
A C++ implementation of the Rego language and runtime
|
Encapsulates the output of a Rego query. More...
#include <rego.hh>
Public Member Functions | |
| Output (Node node) | |
| Constructor. | |
| bool | ok () const |
| Whether the Output is OK (i.e., has not resulted in one or more errors) | |
| Node | node () const |
| Gets the underlying node. | |
| size_t | size () const |
| Gets the number of results. | |
| Node | expressions_at (size_t index) const |
| Gets the expressions for the result at the specified index. | |
| Node | expressions () const |
| Gets the expressions for the first result. | |
| Node | binding_at (size_t index, const std::string &name) const |
| Gets the binding for the specified name at the specified result index. | |
| Node | binding (const std::string &name) const |
| Gets the binding for the specified name at the first result. | |
| const std::string & | json () const |
| Gets the JSON representation of the output. | |
| std::vector< std::string > | errors () const |
| Gets the errors in the output. | |
Encapsulates the output of a Rego query.
The Output class encapsulates the output of a Rego query. A Rego query result can be either undefined (no results), one or more errors, or one or more results. The fact that a query can result in one or more results may seem surprising. The query a = [1, 2, 3, 4]; x = a[_]; x % 2 / == 0; x / 2 results in two distinct solutions: json {"expressions":[true, true, true, 1], "bindings":{"a":[1,2,3,4], "x":2}} {"expressions":[true, true, true, 2], "bindings":{"a":[1,2,3,4], "x":4}} Each result consists of a list of expressions (one for each statement in the query) and a lookup table of bindings, with assigned values for each named local. This class provides methods for accessing these value. Using the output above: cpp rego::Node expressions0 = output.expressions(); rego::Node expressions1 = output.expressions_at(1); rego::Node last0 = expressions0->back(); std::cout << rego::to_key(last0); // 1 rego::Node last1 = expressions1->back(); std::cout << rego::to_key(last1); // 2 Node a = output.binding("a"); std::cout << rego::to_key(a); // [1,2,3,4] auto x0 = rego::get_int(output.binding("x")); std::cout << x0; // 2 auto x1 = rego::get_int(output.binding_at(1, "x")); std::cout << x1; // 4
| rego::Output::Output | ( | Node | node | ) |
Constructor.
| node | A Results or ErrorSeq node. |
| Node rego::Output::binding | ( | const std::string & | name | ) | const |
Gets the binding for the specified name at the first result.
| name | The name of the binding. |
| Node rego::Output::binding_at | ( | size_t | index, |
| const std::string & | name ) const |
Gets the binding for the specified name at the specified result index.
| index | The index of the result. |
| name | The name of the binding. |
| std::vector< std::string > rego::Output::errors | ( | ) | const |
Gets the errors in the output.
If the output is not OK, this method can be used to retrieve the error messages.
| Node rego::Output::expressions | ( | ) | const |
Gets the expressions for the first result.
| Node rego::Output::expressions_at | ( | size_t | index | ) | const |
Gets the expressions for the result at the specified index.
| index | The index of the result. |
| const std::string & rego::Output::json | ( | ) | const |
Gets the JSON representation of the output.
| Node rego::Output::node | ( | ) | const |
Gets the underlying node.
| bool rego::Output::ok | ( | ) | const |
| size_t rego::Output::size | ( | ) | const |
Gets the number of results.
There will be one result per solution to the query.