implemented enum serialization generation

This commit is contained in:
Sara 2023-09-13 10:40:39 +02:00
parent fdf11cec7a
commit 19deaea93e
5 changed files with 106 additions and 27 deletions

View file

@ -1,7 +1,24 @@
#include <string.h>
#define KWIL_GEN_IMPL(...)\
int struct_A_to_json(struct struct_A* src, char** out_json) {\
int json_capacity = 2; \
size_t enum_A_json_length(enum enum_A* src) {\
switch(*src) {\
case VALUE_A: return 7;\
case VALUE_B: return 7;\
case VALUE_C: return 7;\
}\
}\
size_t enum_A_to_json(enum enum_A* src, char* out) {\
switch(*src) {\
case VALUE_A:\
return sprintf(out, "\"VALUE_A\"");\
case VALUE_B:\
return sprintf(out, "\"VALUE_B\"");\
case VALUE_C:\
return sprintf(out, "\"VALUE_C\"");\
}\
}\
size_t struct_A_json_length(struct struct_A* src) {\
size_t json_capacity = 2;\
/* length of b */\
json_capacity += 5+ snprintf(NULL, 0, "%d", src->b);\
/* length of a */\
@ -10,10 +27,11 @@ int struct_A_to_json(struct struct_A* src, char** out_json) {\
json_capacity += 5 + snprintf(NULL, 0, "%du", src->u);\
/* length of dyn_str */\
json_capacity += 11 + strlen(src->dyn_str);\
char* json = malloc(json_capacity);\
return json_capacity;\
}\
int struct_A_to_json(struct struct_A* src, char* json) {\
int json_len = 1;\
strcpy(json, "{");\
*out_json = json;\
/* field: b */\
json_len += sprintf(json + json_len, "\"b\":");\
json_len += sprintf(json + json_len, "%d", src->b);\
@ -37,10 +55,10 @@ int struct_A_to_json(struct struct_A* src, char** out_json) {\
strcpy(json + json_len, ",");\
++json_len;\
strcpy(json + json_len - 1, "}");\
return json_capacity;\
return json_len;\
}\
int struct_B_to_json(struct struct_B* src, char** out_json) {\
int json_capacity = 2; \
size_t struct_B_json_length(struct struct_B* src) {\
size_t json_capacity = 2;\
/* length of f */\
json_capacity += 5 + snprintf(NULL, 0, "%f", src->f);\
/* length of i */\
@ -50,17 +68,18 @@ int struct_B_to_json(struct struct_B* src, char** out_json) {\
/* length of str_static */\
json_capacity += 14 + strlen(src->str_static);\
/* length of other_struct */\
json_capacity += 16;\
json_capacity += 16 + struct_A_json_length(&src->other_struct);\
/* length of other_struct_typedef */\
json_capacity += 24;\
json_capacity += 24 + struct_A_json_length(&src->other_struct_typedef);\
/* length of other_enum */\
json_capacity += 14;\
json_capacity += 14 + enum_A_json_length(&src->other_enum);\
/* length of other_enum_typedef */\
json_capacity += 22;\
char* json = malloc(json_capacity);\
json_capacity += 22 + enum_A_json_length(&src->other_enum_typedef);\
return json_capacity;\
}\
int struct_B_to_json(struct struct_B* src, char* json) {\
int json_len = 1;\
strcpy(json, "{");\
*out_json = json;\
/* field: f */\
json_len += sprintf(json + json_len, "\"f\":");\
json_len += sprintf(json + json_len, "%f", src->f);\
@ -87,21 +106,25 @@ int struct_B_to_json(struct struct_B* src, char** out_json) {\
++json_len;\
/* field: other_struct */\
json_len += sprintf(json + json_len, "\"other_struct\":");\
json_len += struct_A_to_json(&src->other_struct, json + json_len);\
strcpy(json + json_len, ",");\
++json_len;\
/* field: other_struct_typedef */\
json_len += sprintf(json + json_len, "\"other_struct_typedef\":");\
json_len += struct_A_to_json(&src->other_struct_typedef, json + json_len);\
strcpy(json + json_len, ",");\
++json_len;\
/* field: other_enum */\
json_len += sprintf(json + json_len, "\"other_enum\":");\
json_len += enum_A_to_json(&src->other_enum, json + json_len);\
strcpy(json + json_len, ",");\
++json_len;\
/* field: other_enum_typedef */\
json_len += sprintf(json + json_len, "\"other_enum_typedef\":");\
json_len += enum_A_to_json(&src->other_enum_typedef, json + json_len);\
strcpy(json + json_len, ",");\
++json_len;\
strcpy(json + json_len - 1, "}");\
return json_capacity;\
return json_len;\
}\

View file

@ -9,27 +9,42 @@ KWIL_GEN_IMPL()
int main(int argc, char* argv[]) {
printf("running kwil test\n");
struct struct_A b = {
struct struct_A a = {
.b = -10,
.u = 13,
.a = 1.0,
.dyn_str = "WHOOAAAAA"
};
char* json = NULL;
int json_len = struct_A_to_json(&b, &json);
printf("allocated %d bytes for json of struct_A\n", json_len);
printf("struct_A as json:\n%s\n", json);
struct struct_B b = {
.f = 123.0,
.f_NOT_KWIL = 0,
.i_NOT_KWIL = 0,
.i = 3,
.str = "SNALE!!",
.other_struct = a,
.other_struct_typedef = {
.b = -20, .u = 13, .a = -3.14, .dyn_str = "AWESOMEE"
}
};
int required = struct_B_json_length(&b);
char* json = malloc(required);
int json_len = struct_B_to_json(&b, json);
printf("allocated %d bytes for json of struct_B\n", required);
printf("struct_B as json:\n%s\n", json);
int real_len = strlen(json);
free(json);
if(real_len != json_len) {
printf("Json Length (%d) does not match allocated space %d\n", real_len, json_len);
printf("Json Length (%d) does not match allocated space (%d)\n", required, json_len);
return 1;
}
return 0;
}