Support the type "IntOrString" (Manually added part)
This commit is contained in:
@@ -23,6 +23,7 @@ list(APPEND SRCS
|
||||
watch/watch_util.c
|
||||
websocket/wsclient.c
|
||||
websocket/kube_exec.c
|
||||
model/int_or_string.c
|
||||
src/generic.c
|
||||
src/utils.c)
|
||||
|
||||
@@ -39,6 +40,7 @@ list(APPEND HDRS
|
||||
watch/watch_util.h
|
||||
websocket/wsclient.h
|
||||
websocket/kube_exec.h
|
||||
model/int_or_string.h
|
||||
include/generic.h
|
||||
include/utils.h)
|
||||
|
||||
|
||||
56
kubernetes/model/int_or_string.c
Normal file
56
kubernetes/model/int_or_string.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "int_or_string.h"
|
||||
|
||||
int_or_string_t *int_or_string_create() {
|
||||
int_or_string_t *ios = calloc(sizeof(int_or_string_t), 1);
|
||||
if (!ios) {
|
||||
return NULL;
|
||||
}
|
||||
ios->type = IOS_DATA_TYPE_INT;
|
||||
return ios;
|
||||
}
|
||||
|
||||
void int_or_string_free(int_or_string_t *ios) {
|
||||
if (!ios) {
|
||||
return ;
|
||||
}
|
||||
if (IOS_DATA_TYPE_STRING == ios->type && ios->s) {
|
||||
free(ios->s);
|
||||
ios->s = NULL;
|
||||
}
|
||||
free(ios);
|
||||
}
|
||||
|
||||
cJSON *int_or_string_convertToJSON(int_or_string_t *ios) {
|
||||
if (IOS_DATA_TYPE_INT == ios->type) {
|
||||
return cJSON_CreateNumber(ios->i);
|
||||
}
|
||||
if (IOS_DATA_TYPE_STRING == ios->type && ios->s){
|
||||
return cJSON_CreateString(ios->s);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int_or_string_t *int_or_string_parseFromJSON(cJSON *cjson){
|
||||
int_or_string_t *ios = int_or_string_create();
|
||||
if (!ios) {
|
||||
return NULL;
|
||||
}
|
||||
if(cJSON_IsNumber(cjson)) {
|
||||
ios->i = cjson->valuedouble;
|
||||
ios->type = IOS_DATA_TYPE_INT;
|
||||
return ios;
|
||||
}
|
||||
if (cJSON_IsString(cjson)) {
|
||||
ios->s = strdup(cjson->valuestring);
|
||||
ios->type = IOS_DATA_TYPE_STRING;
|
||||
return ios;
|
||||
}
|
||||
if (ios) {
|
||||
int_or_string_free(ios);
|
||||
ios = NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
40
kubernetes/model/int_or_string.h
Normal file
40
kubernetes/model/int_or_string.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* int_or_string.h
|
||||
*/
|
||||
|
||||
#ifndef _INT_OR_STRING_H_
|
||||
#define _INT_OR_STRING_H_
|
||||
|
||||
#include <string.h>
|
||||
#include "../external/cJSON.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
IOS_DATA_TYPE_INT = 1,
|
||||
IOS_DATA_TYPE_STRING = 2
|
||||
} ios_data_type_t;
|
||||
|
||||
typedef struct int_or_string_t {
|
||||
ios_data_type_t type;
|
||||
union {
|
||||
int i;
|
||||
char *s;
|
||||
};
|
||||
} int_or_string_t;
|
||||
|
||||
int_or_string_t *int_or_string_create();
|
||||
|
||||
void int_or_string_free(int_or_string_t *ios);
|
||||
|
||||
int_or_string_t *int_or_string_parseFromJSON(cJSON *cjson);
|
||||
|
||||
cJSON *int_or_string_convertToJSON(int_or_string_t *ios);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _INT_OR_STRING_H_ */
|
||||
68
kubernetes/unit-test/manual_test_v1_service_port.c
Normal file
68
kubernetes/unit-test/manual_test_v1_service_port.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef v1_service_port_TEST
|
||||
#define v1_service_port_TEST
|
||||
|
||||
// the following is to include only the main from the first c file
|
||||
#ifndef TEST_MAIN
|
||||
#define TEST_MAIN
|
||||
#define v1_service_port_MAIN
|
||||
#endif // TEST_MAIN
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "../external/cJSON.h"
|
||||
|
||||
#include "../model/v1_service_port.h"
|
||||
#include "../model/int_or_string.h"
|
||||
v1_service_port_t* instantiate_v1_service_port();
|
||||
|
||||
v1_service_port_t* instantiate_v1_service_port() {
|
||||
v1_service_port_t* v1_service_port = NULL;
|
||||
int_or_string_t *ios = int_or_string_create();
|
||||
ios->type = IOS_DATA_TYPE_STRING;
|
||||
ios->s = strdup("port_name");
|
||||
|
||||
v1_service_port = v1_service_port_create(
|
||||
strdup("0"),
|
||||
strdup("0"),
|
||||
56,
|
||||
56,
|
||||
strdup("0"),
|
||||
ios
|
||||
);
|
||||
|
||||
return v1_service_port;
|
||||
}
|
||||
|
||||
|
||||
#ifdef v1_service_port_MAIN
|
||||
|
||||
void test_v1_service_port() {
|
||||
v1_service_port_t* v1_service_port_1 = instantiate_v1_service_port();
|
||||
|
||||
cJSON* jsonv1_service_port_1 = v1_service_port_convertToJSON(v1_service_port_1);
|
||||
char *service_port_1_string = cJSON_Print(jsonv1_service_port_1);
|
||||
printf("v1_service_port :\n%s\n", service_port_1_string);
|
||||
free(service_port_1_string);
|
||||
|
||||
v1_service_port_t* v1_service_port_2 = v1_service_port_parseFromJSON(jsonv1_service_port_1);
|
||||
cJSON* jsonv1_service_port_2 = v1_service_port_convertToJSON(v1_service_port_2);
|
||||
char *service_port_2_string = cJSON_Print(jsonv1_service_port_2);
|
||||
printf("repeating v1_service_port:\n%s\n", service_port_2_string);
|
||||
free(service_port_2_string);
|
||||
|
||||
cJSON_Delete(jsonv1_service_port_2);
|
||||
cJSON_Delete(jsonv1_service_port_1);
|
||||
v1_service_port_free(v1_service_port_2);
|
||||
v1_service_port_free(v1_service_port_1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_v1_service_port();
|
||||
printf("Hello world \n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // v1_service_port_MAIN
|
||||
#endif // v1_service_port_TEST
|
||||
Reference in New Issue
Block a user