From 5eafd277d8cfcaaf6abce9c5e180c86dc37dc136 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Tue, 30 Nov 2021 16:20:12 +0800 Subject: [PATCH] Support the type "IntOrString" (Manually added part) --- kubernetes/PreTarget.cmake | 2 + kubernetes/model/int_or_string.c | 56 +++++++++++++++ kubernetes/model/int_or_string.h | 40 +++++++++++ .../unit-test/manual_test_v1_service_port.c | 68 +++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 kubernetes/model/int_or_string.c create mode 100644 kubernetes/model/int_or_string.h create mode 100644 kubernetes/unit-test/manual_test_v1_service_port.c diff --git a/kubernetes/PreTarget.cmake b/kubernetes/PreTarget.cmake index 93ea00e..9109654 100644 --- a/kubernetes/PreTarget.cmake +++ b/kubernetes/PreTarget.cmake @@ -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) diff --git a/kubernetes/model/int_or_string.c b/kubernetes/model/int_or_string.c new file mode 100644 index 0000000..6d7dcfc --- /dev/null +++ b/kubernetes/model/int_or_string.c @@ -0,0 +1,56 @@ +#include +#include +#include +#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; +} \ No newline at end of file diff --git a/kubernetes/model/int_or_string.h b/kubernetes/model/int_or_string.h new file mode 100644 index 0000000..e549b6c --- /dev/null +++ b/kubernetes/model/int_or_string.h @@ -0,0 +1,40 @@ +/* + * int_or_string.h + */ + +#ifndef _INT_OR_STRING_H_ +#define _INT_OR_STRING_H_ + +#include +#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_ */ diff --git a/kubernetes/unit-test/manual_test_v1_service_port.c b/kubernetes/unit-test/manual_test_v1_service_port.c new file mode 100644 index 0000000..2c7c401 --- /dev/null +++ b/kubernetes/unit-test/manual_test_v1_service_port.c @@ -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 +#include +#include +#include +#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