2020-03-18 17:24:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "v1_service_port.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
static v1_service_port_t *v1_service_port_create_internal(
|
2021-09-14 21:52:06 +08:00
|
|
|
char *app_protocol,
|
2020-03-18 17:24:33 +08:00
|
|
|
char *name,
|
|
|
|
|
int node_port,
|
|
|
|
|
int port,
|
2022-06-05 18:16:17 +00:00
|
|
|
char *protocol,
|
2021-12-20 17:14:46 +08:00
|
|
|
int_or_string_t *target_port
|
2020-03-18 17:24:33 +08:00
|
|
|
) {
|
|
|
|
|
v1_service_port_t *v1_service_port_local_var = malloc(sizeof(v1_service_port_t));
|
|
|
|
|
if (!v1_service_port_local_var) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
v1_service_port_local_var->app_protocol = app_protocol;
|
2020-03-18 17:24:33 +08:00
|
|
|
v1_service_port_local_var->name = name;
|
|
|
|
|
v1_service_port_local_var->node_port = node_port;
|
|
|
|
|
v1_service_port_local_var->port = port;
|
|
|
|
|
v1_service_port_local_var->protocol = protocol;
|
|
|
|
|
v1_service_port_local_var->target_port = target_port;
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
v1_service_port_local_var->_library_owned = 1;
|
2020-03-18 17:24:33 +08:00
|
|
|
return v1_service_port_local_var;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
__attribute__((deprecated)) v1_service_port_t *v1_service_port_create(
|
|
|
|
|
char *app_protocol,
|
|
|
|
|
char *name,
|
|
|
|
|
int node_port,
|
|
|
|
|
int port,
|
|
|
|
|
char *protocol,
|
|
|
|
|
int_or_string_t *target_port
|
|
|
|
|
) {
|
|
|
|
|
return v1_service_port_create_internal (
|
|
|
|
|
app_protocol,
|
|
|
|
|
name,
|
|
|
|
|
node_port,
|
|
|
|
|
port,
|
|
|
|
|
protocol,
|
|
|
|
|
target_port
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
void v1_service_port_free(v1_service_port_t *v1_service_port) {
|
|
|
|
|
if(NULL == v1_service_port){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2025-02-11 19:36:46 +00:00
|
|
|
if(v1_service_port->_library_owned != 1){
|
|
|
|
|
fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "v1_service_port_free");
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
listEntry_t *listEntry;
|
2021-09-14 21:52:06 +08:00
|
|
|
if (v1_service_port->app_protocol) {
|
|
|
|
|
free(v1_service_port->app_protocol);
|
|
|
|
|
v1_service_port->app_protocol = NULL;
|
|
|
|
|
}
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_service_port->name) {
|
|
|
|
|
free(v1_service_port->name);
|
|
|
|
|
v1_service_port->name = NULL;
|
|
|
|
|
}
|
2022-06-05 18:16:17 +00:00
|
|
|
if (v1_service_port->protocol) {
|
|
|
|
|
free(v1_service_port->protocol);
|
|
|
|
|
v1_service_port->protocol = NULL;
|
|
|
|
|
}
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_service_port->target_port) {
|
2021-12-20 17:14:46 +08:00
|
|
|
int_or_string_free(v1_service_port->target_port);
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_service_port->target_port = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
free(v1_service_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *v1_service_port_convertToJSON(v1_service_port_t *v1_service_port) {
|
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
|
|
2021-09-14 21:52:06 +08:00
|
|
|
// v1_service_port->app_protocol
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_service_port->app_protocol) {
|
2021-09-14 21:52:06 +08:00
|
|
|
if(cJSON_AddStringToObject(item, "appProtocol", v1_service_port->app_protocol) == NULL) {
|
|
|
|
|
goto fail; //String
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_service_port->name
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_service_port->name) {
|
2020-03-18 17:24:33 +08:00
|
|
|
if(cJSON_AddStringToObject(item, "name", v1_service_port->name) == NULL) {
|
|
|
|
|
goto fail; //String
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_service_port->node_port
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_service_port->node_port) {
|
2020-03-18 17:24:33 +08:00
|
|
|
if(cJSON_AddNumberToObject(item, "nodePort", v1_service_port->node_port) == NULL) {
|
|
|
|
|
goto fail; //Numeric
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_service_port->port
|
|
|
|
|
if (!v1_service_port->port) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
if(cJSON_AddNumberToObject(item, "port", v1_service_port->port) == NULL) {
|
|
|
|
|
goto fail; //Numeric
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_service_port->protocol
|
2022-06-05 18:16:17 +00:00
|
|
|
if(v1_service_port->protocol) {
|
|
|
|
|
if(cJSON_AddStringToObject(item, "protocol", v1_service_port->protocol) == NULL) {
|
|
|
|
|
goto fail; //String
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_service_port->target_port
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_service_port->target_port) {
|
2021-12-20 17:14:46 +08:00
|
|
|
cJSON *target_port_local_JSON = int_or_string_convertToJSON(v1_service_port->target_port);
|
|
|
|
|
if(target_port_local_JSON == NULL) {
|
|
|
|
|
goto fail; // custom
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
2021-12-20 17:14:46 +08:00
|
|
|
cJSON_AddItemToObject(item, "targetPort", target_port_local_JSON);
|
2020-03-18 17:24:33 +08:00
|
|
|
if(item->child == NULL) {
|
2021-12-20 17:14:46 +08:00
|
|
|
goto fail;
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
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_service_port_t *v1_service_port_parseFromJSON(cJSON *v1_service_portJSON){
|
|
|
|
|
|
|
|
|
|
v1_service_port_t *v1_service_port_local_var = NULL;
|
|
|
|
|
|
2021-12-20 17:14:46 +08:00
|
|
|
// define the local variable for v1_service_port->target_port
|
|
|
|
|
int_or_string_t *target_port_local_nonprim = NULL;
|
|
|
|
|
|
2021-09-14 21:52:06 +08:00
|
|
|
// v1_service_port->app_protocol
|
|
|
|
|
cJSON *app_protocol = cJSON_GetObjectItemCaseSensitive(v1_service_portJSON, "appProtocol");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(app_protocol)) {
|
|
|
|
|
app_protocol = NULL;
|
|
|
|
|
}
|
2021-09-14 21:52:06 +08:00
|
|
|
if (app_protocol) {
|
2022-12-29 10:58:47 +08:00
|
|
|
if(!cJSON_IsString(app_protocol) && !cJSON_IsNull(app_protocol))
|
2021-09-14 21:52:06 +08:00
|
|
|
{
|
|
|
|
|
goto end; //String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_service_port->name
|
|
|
|
|
cJSON *name = cJSON_GetObjectItemCaseSensitive(v1_service_portJSON, "name");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(name)) {
|
|
|
|
|
name = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
if (name) {
|
2022-12-29 10:58:47 +08:00
|
|
|
if(!cJSON_IsString(name) && !cJSON_IsNull(name))
|
2020-03-18 17:24:33 +08:00
|
|
|
{
|
|
|
|
|
goto end; //String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_service_port->node_port
|
|
|
|
|
cJSON *node_port = cJSON_GetObjectItemCaseSensitive(v1_service_portJSON, "nodePort");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(node_port)) {
|
|
|
|
|
node_port = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
if (node_port) {
|
|
|
|
|
if(!cJSON_IsNumber(node_port))
|
|
|
|
|
{
|
|
|
|
|
goto end; //Numeric
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_service_port->port
|
|
|
|
|
cJSON *port = cJSON_GetObjectItemCaseSensitive(v1_service_portJSON, "port");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(port)) {
|
|
|
|
|
port = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
if (!port) {
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!cJSON_IsNumber(port))
|
|
|
|
|
{
|
|
|
|
|
goto end; //Numeric
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_service_port->protocol
|
|
|
|
|
cJSON *protocol = cJSON_GetObjectItemCaseSensitive(v1_service_portJSON, "protocol");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(protocol)) {
|
|
|
|
|
protocol = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
if (protocol) {
|
2022-12-29 10:58:47 +08:00
|
|
|
if(!cJSON_IsString(protocol) && !cJSON_IsNull(protocol))
|
2020-03-18 17:24:33 +08:00
|
|
|
{
|
2022-06-05 18:16:17 +00:00
|
|
|
goto end; //String
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_service_port->target_port
|
|
|
|
|
cJSON *target_port = cJSON_GetObjectItemCaseSensitive(v1_service_portJSON, "targetPort");
|
2025-02-11 19:36:46 +00:00
|
|
|
if (cJSON_IsNull(target_port)) {
|
|
|
|
|
target_port = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
if (target_port) {
|
2021-12-20 17:14:46 +08:00
|
|
|
target_port_local_nonprim = int_or_string_parseFromJSON(target_port); //custom
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 19:36:46 +00:00
|
|
|
v1_service_port_local_var = v1_service_port_create_internal (
|
2022-12-29 10:58:47 +08:00
|
|
|
app_protocol && !cJSON_IsNull(app_protocol) ? strdup(app_protocol->valuestring) : NULL,
|
|
|
|
|
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL,
|
2020-03-18 17:24:33 +08:00
|
|
|
node_port ? node_port->valuedouble : 0,
|
|
|
|
|
port->valuedouble,
|
2022-12-29 10:58:47 +08:00
|
|
|
protocol && !cJSON_IsNull(protocol) ? strdup(protocol->valuestring) : NULL,
|
2021-12-20 17:14:46 +08:00
|
|
|
target_port ? target_port_local_nonprim : NULL
|
2020-03-18 17:24:33 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return v1_service_port_local_var;
|
|
|
|
|
end:
|
2021-12-20 17:14:46 +08:00
|
|
|
if (target_port_local_nonprim) {
|
|
|
|
|
int_or_string_free(target_port_local_nonprim);
|
|
|
|
|
target_port_local_nonprim = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|