-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Goal
- Add support for the creation of nodes & elements in ut_kvp module.
Example code for creation of data using libyaml.
#include <libfyaml.h>
#include <stdio.h>
int main() {
// Create a fy_document to hold the data
struct fy_document *doc = fy_document_create(NULL);
if (!doc) {
fprintf(stderr, "Failed to create fy_document\n");
return 1;
}
// Create a root mapping node
struct fy_node *root = fy_node_create_mapping(doc);
if (!root) {
fprintf(stderr, "Failed to create root mapping node\n");
fy_document_destroy(doc);
return 1;
}
// Set the root node for the document
fy_document_set_root(doc, root);
// Add key-value pairs to the root mapping
fy_node_mapping_append(doc, root,
fy_node_create_scalar(doc, "username"),
fy_node_create_scalar(doc, "johndoe")
);
fy_node_mapping_append(doc, root,
fy_node_create_scalar(doc, "email"),
fy_node_create_scalar(doc, "johndoe@example.com")
);
// Add an array to the mapping
struct fy_node *roles = fy_node_create_sequence(doc);
fy_node_sequence_append(doc, roles, fy_node_create_scalar(doc, "admin"));
fy_node_sequence_append(doc, roles, fy_node_create_scalar(doc, "editor"));
fy_node_mapping_append(doc, root,
fy_node_create_scalar(doc, "roles"),
roles
);
// Emit as YAML
printf("YAML Output:\n");
struct fy_emitter_cfg yaml_cfg = {
.flags = FYECF_DEFAULT
};
fy_emit_document_to_fp(doc, &yaml_cfg, stdout);
// Emit as JSON
printf("\nJSON Output:\n");
struct fy_emitter_cfg json_cfg = {
.flags = FYECF_JSON
};
fy_emit_document_to_fp(doc, &json_cfg, stdout); ## Emit json to stdout
// Clean up
fy_document_destroy(doc);
return 0;
}Example output to stdout
username: johndoe
email: johndoe@example.com
roles:
- admin
- editorinstance = ut_kvp_createInstance();
ut_kvp_setXX_Bool/Int_XXField(instance, pszkey, value );
ut_kvp_setStringField( instance, pzkey, value )
`username`, `johndoe`,
`roles.admin`, `True`
How to create a list, TBC
`
- admin
- editor
`Output the list to memory in a specific format.
memory = ut_kvp_getDataYaml( instance );
memory = ut_kvp_getDataJson( instance );GET Command Returning JSON
Example using curl:
curl -X GET http://example.com/api/resource \
-H "Accept: application/json"
-X GET: Specifies the HTTP GET request (optional as curl defaults to GET).
-H "Accept: application/json": Requests a JSON response by setting the Accept header.Example JSON Response:
{
"username": "johndoe",
"email": "johndoe@example.com",
"roles": ["admin", "editor"]
}Examples from the client side
These are examples of the client posting and therefore they would have to correctly return the data in either YAML or JSON format.
GET Command Returning YAML
Example using curl:
curl -X GET http://example.com/api/resource \
-H "Accept: application/x-yaml"
-H "Accept: application/x-yaml": Requests a YAML response by setting the Accept header to application/x-yaml.Example YAML Response:
username: johndoe
email: johndoe@example.com
roles:
- admin
- editorGET Command returning JSON
curl -X GET http://example.com/api/resource?format=jsonReponse Expected
{
"username": "johndoe",
"email": "johndoe@example.com",
"roles": ["admin", "editor"]
}Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Type
Projects
Status
Blocked