Function regorust::regoNewInput

source ยท
pub unsafe extern "C" fn regoNewInput() -> *mut regoInput
Expand description

@brief Allocates and initializes a new Rego Input object. @details Rego Input objects can be used to build input documents programmatically. An input is built up by making a series of calls to the API to add values to the stack. Some operations use the values on the stack to create composite values (e.g. objects, arrays, sets) which are then pushed back onto the stack. Once the input is built, it can be validated and then set on the interpreter with regoSetInput. While you can test the result of every operation, you can continue to use a broken Input object and the error will be returned when you call regoInputValidate.

// Example of building the following input document:
// {
//   "user": {
//     "name": "alice",
//     "age": 30,
//     "active": true
//   },
//   "roles": ["admin", "user"]
// }
regoInput* input = regoNewInput();
regoInputString(input, "user");     // Push "user"
regoInputString(input, "name");     // Push "name"
regoInputString(input, "alice");    // Push "alice"
regoInputObjectItem(input);         // Create {"name": "alice"}
regoInputString(input, "age");      // Push "age"
regoInputInt(input, 30);            // Push 30
regoInputObjectItem(input);         // Create {"age": 30}
regoInputString(input, "active");   // Push "active"
regoInputBoolean(input, true);      // Push true
regoInputObjectItem(input);         // Create {"active": true}
regoInputObject(input, 3);          // Create {"name": "alice", "age": 30, "active": true}
regoInputObjectItem(input);         // Create {"user": {...}}
regoInputString(input, "roles");    // Push "roles"
regoInputString(input, "admin");    // Push "admin"
regoInputString(input, "user");     // Push "user"
regoInputArray(input, 2);           // Create ["admin", "user"]
regoInputObjectItem(input);         // Create {"roles": [...]}
regoInputObject(input, 2);          // Create the final object
regoInputValidate(input);           // Validate the input
regoSetInput(rego, input);          // Set the input on the interpreter

@note The caller is responsible for freeing the input object with regoFreeInput. @return A pointer to the new input object.