Skip to content
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

Task: KVP: upgrade to retrieve kvp data in user requested format #67

Open
Ulrond opened this issue Dec 31, 2024 · 2 comments · May be fixed by #68
Open

Task: KVP: upgrade to retrieve kvp data in user requested format #67

Ulrond opened this issue Dec 31, 2024 · 2 comments · May be fixed by #68
Assignees
Labels
enhancement New feature or request

Comments

@Ulrond
Copy link
Collaborator

Ulrond commented Dec 31, 2024

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, "[email protected]")
    );

    // 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: [email protected]
roles:
  - admin
  - editor
instance = 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": "[email protected]",
    "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: [email protected]
roles:
  - admin
  - editor

GET Command returning JSON

curl -X GET http://example.com/api/resource?format=json

Reponse Expected

{
    "username": "johndoe",
    "email": "[email protected]",
    "roles": ["admin", "editor"]
}
@Ulrond Ulrond added the enhancement New feature or request label Dec 31, 2024
@Ulrond Ulrond moved this to Todo in UT-Core Roadmap Dec 31, 2024
@kanjoe24
Copy link
Contributor

kanjoe24 commented Jan 8, 2025

What will be the input for this? As in how is the user expected to provide the key and value ? @Ulrond @srinivasgtl .

@kanjoe24
Copy link
Contributor

I feel, set commands are not required for this requirement.
However, we need a KVP function() which will retrieve KVP data from KVP instance based on user requested type(say, json or yaml).
These changes are done here:
https://github.com/rdkcentral/ut-control/pull/68/files#diff-fb01b15dd8c47b47ec634fa48c076b164bf2545bf53edeb5b340ea9a34e2e4ed

https://github.com/rdkcentral/ut-control/pull/68/files#diff-b1fc14385e815cc7fbebd803034f86ee9b7b1ec909e9f25032c1f707f530097c

@kanjoe24 kanjoe24 self-assigned this Jan 16, 2025
@kanjoe24 kanjoe24 moved this from Todo to In Progress in UT-Core Roadmap Jan 16, 2025
@kanjoe24 kanjoe24 linked a pull request Jan 16, 2025 that will close this issue
@kanjoe24 kanjoe24 changed the title Task: KVP: upgrade to support creation of kvp data Task: KVP: upgrade to retrieve kvp data in user requested format Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

2 participants