2020-03-18 17:24:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "v1_scope_selector.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
static v1_scope_selector_t *v1_scope_selector_create_internal(
|
2020-03-18 17:24:33 +08:00
|
|
|
list_t *match_expressions
|
|
|
|
|
) {
|
|
|
|
|
v1_scope_selector_t *v1_scope_selector_local_var = malloc(sizeof(v1_scope_selector_t));
|
|
|
|
|
if (!v1_scope_selector_local_var) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
v1_scope_selector_local_var->match_expressions = match_expressions;
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
v1_scope_selector_local_var->_library_owned = 1;
|
2020-03-18 17:24:33 +08:00
|
|
|
return v1_scope_selector_local_var;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
__attribute__((deprecated)) v1_scope_selector_t *v1_scope_selector_create(
|
|
|
|
|
list_t *match_expressions
|
|
|
|
|
) {
|
|
|
|
|
return v1_scope_selector_create_internal (
|
|
|
|
|
match_expressions
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
void v1_scope_selector_free(v1_scope_selector_t *v1_scope_selector) {
|
|
|
|
|
if(NULL == v1_scope_selector){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2025-02-11 19:36:46 +00:00
|
|
|
if(v1_scope_selector->_library_owned != 1){
|
|
|
|
|
fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "v1_scope_selector_free");
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
listEntry_t *listEntry;
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_scope_selector->match_expressions) {
|
|
|
|
|
list_ForEach(listEntry, v1_scope_selector->match_expressions) {
|
|
|
|
|
v1_scoped_resource_selector_requirement_free(listEntry->data);
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
list_freeList(v1_scope_selector->match_expressions);
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_scope_selector->match_expressions = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
free(v1_scope_selector);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *v1_scope_selector_convertToJSON(v1_scope_selector_t *v1_scope_selector) {
|
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
|
|
|
|
|
|
// v1_scope_selector->match_expressions
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_scope_selector->match_expressions) {
|
2020-03-18 17:24:33 +08:00
|
|
|
cJSON *match_expressions = cJSON_AddArrayToObject(item, "matchExpressions");
|
|
|
|
|
if(match_expressions == NULL) {
|
|
|
|
|
goto fail; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *match_expressionsListEntry;
|
|
|
|
|
if (v1_scope_selector->match_expressions) {
|
|
|
|
|
list_ForEach(match_expressionsListEntry, v1_scope_selector->match_expressions) {
|
|
|
|
|
cJSON *itemLocal = v1_scoped_resource_selector_requirement_convertToJSON(match_expressionsListEntry->data);
|
|
|
|
|
if(itemLocal == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToArray(match_expressions, itemLocal);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
fail:
|
|
|
|
|
if (item) {
|
|
|
|
|
cJSON_Delete(item);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v1_scope_selector_t *v1_scope_selector_parseFromJSON(cJSON *v1_scope_selectorJSON){
|
|
|
|
|
|
|
|
|
|
v1_scope_selector_t *v1_scope_selector_local_var = NULL;
|
|
|
|
|
|
2022-03-29 10:12:05 +08:00
|
|
|
// define the local list for v1_scope_selector->match_expressions
|
|
|
|
|
list_t *match_expressionsList = NULL;
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_scope_selector->match_expressions
|
|
|
|
|
cJSON *match_expressions = cJSON_GetObjectItemCaseSensitive(v1_scope_selectorJSON, "matchExpressions");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(match_expressions)) {
|
|
|
|
|
match_expressions = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
if (match_expressions) {
|
2022-03-29 10:12:05 +08:00
|
|
|
cJSON *match_expressions_local_nonprimitive = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
if(!cJSON_IsArray(match_expressions)){
|
|
|
|
|
goto end; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 10:56:53 +08:00
|
|
|
match_expressionsList = list_createList();
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(match_expressions_local_nonprimitive,match_expressions )
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsObject(match_expressions_local_nonprimitive)){
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
v1_scoped_resource_selector_requirement_t *match_expressionsItem = v1_scoped_resource_selector_requirement_parseFromJSON(match_expressions_local_nonprimitive);
|
|
|
|
|
|
|
|
|
|
list_addElement(match_expressionsList, match_expressionsItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
v1_scope_selector_local_var = v1_scope_selector_create_internal (
|
2020-03-18 17:24:33 +08:00
|
|
|
match_expressions ? match_expressionsList : NULL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return v1_scope_selector_local_var;
|
|
|
|
|
end:
|
2022-03-29 10:12:05 +08:00
|
|
|
if (match_expressionsList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, match_expressionsList) {
|
|
|
|
|
v1_scoped_resource_selector_requirement_free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(match_expressionsList);
|
|
|
|
|
match_expressionsList = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|