-
Notifications
You must be signed in to change notification settings - Fork 0
gh #98 Extend kvp open function to also support URLs #99
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
129893f
2731cc3
82d755c
4d0dde5
f362689
0540b76
679e6f3
ff2c367
854e763
37babe1
889d5d5
5ea8838
9a66294
155307c
60483b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,55 +95,80 @@ void ut_kvp_destroyInstance(ut_kvp_instance_t *pInstance) | |
| pInternal = NULL; | ||
| } | ||
|
|
||
| ut_kvp_status_t ut_kvp_open(ut_kvp_instance_t *pInstance, char *fileName) | ||
| static bool is_url(const char *input) | ||
| { | ||
| struct fy_node *node; | ||
| ut_kvp_instance_internal_t *pInternal = validateInstance(pInstance); | ||
| if (input == NULL) { | ||
| return false; | ||
| } | ||
| return (strncmp(input, "http://", 7) == 0 || strncmp(input, "https://", 8) == 0); | ||
|
||
| } | ||
|
|
||
| if (pInstance == NULL) | ||
| static struct fy_document* build_yaml_from_source(const char *fileNameOrUrl, bool isUrl) | ||
| { | ||
| if (isUrl) | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return UT_KVP_STATUS_INVALID_INSTANCE; | ||
| char yaml[512]; | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| snprintf(yaml, sizeof(yaml), "include: %s\n", fileNameOrUrl); | ||
| return fy_document_build_from_string(NULL, (const char *)yaml, sizeof(yaml)); | ||
| } | ||
| else | ||
| { | ||
| return fy_document_build_from_file(NULL, fileNameOrUrl); | ||
| } | ||
| } | ||
|
|
||
| ut_kvp_status_t ut_kvp_open(ut_kvp_instance_t *pInstance, char *fileNameOrUrl) | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (pInstance == NULL) | ||
| return UT_KVP_STATUS_INVALID_INSTANCE; | ||
|
|
||
| if (fileName == NULL) | ||
| if (fileNameOrUrl == NULL) | ||
| { | ||
| UT_LOG_ERROR( "Invalid Param [fileName]" ); | ||
| UT_LOG_ERROR("Invalid Param [fileNameOrUrl]"); | ||
|
||
| return UT_KVP_STATUS_INVALID_PARAM; | ||
| } | ||
|
|
||
| if (access(fileName, F_OK) != 0) | ||
| ut_kvp_instance_internal_t *pInternal = validateInstance(pInstance); | ||
|
||
| bool url = is_url(fileNameOrUrl); | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (url == 0 && (access(fileNameOrUrl, F_OK) != 0)) | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| UT_LOG_ERROR("[%s] cannot be accesed", fileName); | ||
| UT_LOG_ERROR("[%s] cannot be accessed", fileNameOrUrl); | ||
| return UT_KVP_STATUS_FILE_OPEN_ERROR; | ||
| } | ||
|
|
||
| if(pInternal->fy_handle) | ||
| if (pInternal->fy_handle) | ||
| { | ||
| merge_nodes(fy_document_root(pInternal->fy_handle), fy_document_root(fy_document_build_from_file(NULL, fileName))); | ||
| merge_nodes(fy_document_root(pInternal->fy_handle), fy_document_root(build_yaml_from_source(fileNameOrUrl, url))); | ||
| } | ||
| else | ||
| { | ||
| pInternal->fy_handle = fy_document_create(NULL); | ||
| if (pInternal->fy_handle == NULL) | ||
kanjoe24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| UT_LOG_ERROR("Unable to create YAML handle"); | ||
| ut_kvp_close(pInstance); | ||
| return UT_KVP_STATUS_PARSING_ERROR; | ||
kanjoe24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| if (NULL == pInternal->fy_handle) | ||
| // Step 2: Build the actual srcDoc for processing | ||
| struct fy_document *srcDoc = build_yaml_from_source(fileNameOrUrl, url); | ||
| if (srcDoc == NULL) | ||
| { | ||
| UT_LOG_ERROR("Unable to parse file/memory"); | ||
| ut_kvp_close( pInstance ); | ||
| UT_LOG_ERROR("Failed to build srcDoc for processing"); | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ut_kvp_close(pInstance); | ||
| return UT_KVP_STATUS_PARSING_ERROR; | ||
| } | ||
|
|
||
| struct fy_document *srcDoc = fy_document_build_from_file(NULL, fileName); | ||
|
|
||
| if(fy_document_resolve(srcDoc) != 0) | ||
| if (fy_document_resolve(srcDoc) != 0) | ||
| { | ||
| UT_LOG_ERROR("Error resolving document for anchors, aliases and merge keys"); | ||
| ut_kvp_close(pInstance); | ||
| return UT_KVP_STATUS_PARSING_ERROR; | ||
| } | ||
|
|
||
| node = process_node_copy(fy_document_root(srcDoc), pInternal->fy_handle, 0); | ||
|
|
||
|
|
||
| struct fy_node *node = process_node_copy(fy_document_root(srcDoc), pInternal->fy_handle, 0); | ||
| if (node == NULL) | ||
| { | ||
| UT_LOG_ERROR("Unable to process node"); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.