2021-09-14 21:52:06 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "v1_endpoint_hints.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
static v1_endpoint_hints_t *v1_endpoint_hints_create_internal(
|
2025-06-08 08:13:38 +00:00
|
|
|
list_t *for_nodes,
|
2021-09-14 21:52:06 +08:00
|
|
|
list_t *for_zones
|
|
|
|
|
) {
|
|
|
|
|
v1_endpoint_hints_t *v1_endpoint_hints_local_var = malloc(sizeof(v1_endpoint_hints_t));
|
|
|
|
|
if (!v1_endpoint_hints_local_var) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-06-08 08:13:38 +00:00
|
|
|
v1_endpoint_hints_local_var->for_nodes = for_nodes;
|
2021-09-14 21:52:06 +08:00
|
|
|
v1_endpoint_hints_local_var->for_zones = for_zones;
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
v1_endpoint_hints_local_var->_library_owned = 1;
|
2021-09-14 21:52:06 +08:00
|
|
|
return v1_endpoint_hints_local_var;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
__attribute__((deprecated)) v1_endpoint_hints_t *v1_endpoint_hints_create(
|
2025-06-08 08:13:38 +00:00
|
|
|
list_t *for_nodes,
|
2025-02-11 19:36:46 +00:00
|
|
|
list_t *for_zones
|
|
|
|
|
) {
|
|
|
|
|
return v1_endpoint_hints_create_internal (
|
2025-06-08 08:13:38 +00:00
|
|
|
for_nodes,
|
2025-02-11 19:36:46 +00:00
|
|
|
for_zones
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
|
|
|
|
|
void v1_endpoint_hints_free(v1_endpoint_hints_t *v1_endpoint_hints) {
|
|
|
|
|
if(NULL == v1_endpoint_hints){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2025-02-11 19:36:46 +00:00
|
|
|
if(v1_endpoint_hints->_library_owned != 1){
|
|
|
|
|
fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "v1_endpoint_hints_free");
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
listEntry_t *listEntry;
|
2025-06-08 08:13:38 +00:00
|
|
|
if (v1_endpoint_hints->for_nodes) {
|
|
|
|
|
list_ForEach(listEntry, v1_endpoint_hints->for_nodes) {
|
|
|
|
|
v1_for_node_free(listEntry->data);
|
|
|
|
|
}
|
|
|
|
|
list_freeList(v1_endpoint_hints->for_nodes);
|
|
|
|
|
v1_endpoint_hints->for_nodes = NULL;
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
if (v1_endpoint_hints->for_zones) {
|
|
|
|
|
list_ForEach(listEntry, v1_endpoint_hints->for_zones) {
|
|
|
|
|
v1_for_zone_free(listEntry->data);
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
list_freeList(v1_endpoint_hints->for_zones);
|
2021-09-14 21:52:06 +08:00
|
|
|
v1_endpoint_hints->for_zones = NULL;
|
|
|
|
|
}
|
|
|
|
|
free(v1_endpoint_hints);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *v1_endpoint_hints_convertToJSON(v1_endpoint_hints_t *v1_endpoint_hints) {
|
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
|
|
2025-06-08 08:13:38 +00:00
|
|
|
// v1_endpoint_hints->for_nodes
|
|
|
|
|
if(v1_endpoint_hints->for_nodes) {
|
|
|
|
|
cJSON *for_nodes = cJSON_AddArrayToObject(item, "forNodes");
|
|
|
|
|
if(for_nodes == NULL) {
|
|
|
|
|
goto fail; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *for_nodesListEntry;
|
|
|
|
|
if (v1_endpoint_hints->for_nodes) {
|
|
|
|
|
list_ForEach(for_nodesListEntry, v1_endpoint_hints->for_nodes) {
|
|
|
|
|
cJSON *itemLocal = v1_for_node_convertToJSON(for_nodesListEntry->data);
|
|
|
|
|
if(itemLocal == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToArray(for_nodes, itemLocal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-14 21:52:06 +08:00
|
|
|
// v1_endpoint_hints->for_zones
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_endpoint_hints->for_zones) {
|
2021-09-14 21:52:06 +08:00
|
|
|
cJSON *for_zones = cJSON_AddArrayToObject(item, "forZones");
|
|
|
|
|
if(for_zones == NULL) {
|
|
|
|
|
goto fail; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *for_zonesListEntry;
|
|
|
|
|
if (v1_endpoint_hints->for_zones) {
|
|
|
|
|
list_ForEach(for_zonesListEntry, v1_endpoint_hints->for_zones) {
|
|
|
|
|
cJSON *itemLocal = v1_for_zone_convertToJSON(for_zonesListEntry->data);
|
|
|
|
|
if(itemLocal == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToArray(for_zones, itemLocal);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
fail:
|
|
|
|
|
if (item) {
|
|
|
|
|
cJSON_Delete(item);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v1_endpoint_hints_t *v1_endpoint_hints_parseFromJSON(cJSON *v1_endpoint_hintsJSON){
|
|
|
|
|
|
|
|
|
|
v1_endpoint_hints_t *v1_endpoint_hints_local_var = NULL;
|
|
|
|
|
|
2025-06-08 08:13:38 +00:00
|
|
|
// define the local list for v1_endpoint_hints->for_nodes
|
|
|
|
|
list_t *for_nodesList = NULL;
|
|
|
|
|
|
2022-03-29 10:12:05 +08:00
|
|
|
// define the local list for v1_endpoint_hints->for_zones
|
|
|
|
|
list_t *for_zonesList = NULL;
|
|
|
|
|
|
2025-06-08 08:13:38 +00:00
|
|
|
// v1_endpoint_hints->for_nodes
|
|
|
|
|
cJSON *for_nodes = cJSON_GetObjectItemCaseSensitive(v1_endpoint_hintsJSON, "forNodes");
|
|
|
|
|
if (cJSON_IsNull(for_nodes)) {
|
|
|
|
|
for_nodes = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (for_nodes) {
|
|
|
|
|
cJSON *for_nodes_local_nonprimitive = NULL;
|
|
|
|
|
if(!cJSON_IsArray(for_nodes)){
|
|
|
|
|
goto end; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for_nodesList = list_createList();
|
|
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(for_nodes_local_nonprimitive,for_nodes )
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsObject(for_nodes_local_nonprimitive)){
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
v1_for_node_t *for_nodesItem = v1_for_node_parseFromJSON(for_nodes_local_nonprimitive);
|
|
|
|
|
|
|
|
|
|
list_addElement(for_nodesList, for_nodesItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 21:52:06 +08:00
|
|
|
// v1_endpoint_hints->for_zones
|
|
|
|
|
cJSON *for_zones = cJSON_GetObjectItemCaseSensitive(v1_endpoint_hintsJSON, "forZones");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(for_zones)) {
|
|
|
|
|
for_zones = NULL;
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
if (for_zones) {
|
2022-03-29 10:12:05 +08:00
|
|
|
cJSON *for_zones_local_nonprimitive = NULL;
|
2021-09-14 21:52:06 +08:00
|
|
|
if(!cJSON_IsArray(for_zones)){
|
|
|
|
|
goto end; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 10:56:53 +08:00
|
|
|
for_zonesList = list_createList();
|
2021-09-14 21:52:06 +08:00
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(for_zones_local_nonprimitive,for_zones )
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsObject(for_zones_local_nonprimitive)){
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
v1_for_zone_t *for_zonesItem = v1_for_zone_parseFromJSON(for_zones_local_nonprimitive);
|
|
|
|
|
|
|
|
|
|
list_addElement(for_zonesList, for_zonesItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
v1_endpoint_hints_local_var = v1_endpoint_hints_create_internal (
|
2025-06-08 08:13:38 +00:00
|
|
|
for_nodes ? for_nodesList : NULL,
|
2021-09-14 21:52:06 +08:00
|
|
|
for_zones ? for_zonesList : NULL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return v1_endpoint_hints_local_var;
|
|
|
|
|
end:
|
2025-06-08 08:13:38 +00:00
|
|
|
if (for_nodesList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, for_nodesList) {
|
|
|
|
|
v1_for_node_free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(for_nodesList);
|
|
|
|
|
for_nodesList = NULL;
|
|
|
|
|
}
|
2022-03-29 10:12:05 +08:00
|
|
|
if (for_zonesList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, for_zonesList) {
|
|
|
|
|
v1_for_zone_free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(for_zonesList);
|
|
|
|
|
for_zonesList = NULL;
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|