refactor(json): improve code and struct names

This commit is contained in:
2026-01-30 01:42:13 +01:00
parent 773dae4676
commit da146feec8
4 changed files with 51 additions and 28 deletions

View File

@@ -6,6 +6,7 @@
#define TOKEN_SIZE 128 #define TOKEN_SIZE 128
#define API_SIZE 512 #define API_SIZE 512
#define URL_LEN 1024
#define FIRSTNAME_SIZE 256 #define FIRSTNAME_SIZE 256
#define USERNAME_SIZE 32 #define USERNAME_SIZE 32
@@ -76,7 +77,7 @@ typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query);
/** /**
* @brief An enum to represent error codes. * @brief An enum to represent error codes.
*/ */
enum tgbot_rc_t { enum tgbot_rc {
TGBOT_OK = 0, TGBOT_OK = 0,
TGBOT_INIT_ERROR, TGBOT_INIT_ERROR,
TGBOT_REQUEST_ERROR, TGBOT_REQUEST_ERROR,
@@ -87,22 +88,22 @@ enum tgbot_rc_t {
TGBOT_SENDDICE_ERROR, TGBOT_SENDDICE_ERROR,
TGBOT_TELEGRAM_OK_ERROR, TGBOT_TELEGRAM_OK_ERROR,
}; };
typedef enum tgbot_rc_t tgbot_rc; typedef enum tgbot_rc tgbot_rc;
enum tgbot_json_opt_type_t { enum tgbot_opt_type {
tgbot_opt_int, tgbot_opt_int,
tgbot_opt_int64, tgbot_opt_int64,
tgbot_opt_string, tgbot_opt_string,
tgbot_opt_inlinekeyboard, tgbot_opt_inlinekeyboard,
tgbot_opt_null, tgbot_opt_null,
}; };
typedef enum tgbot_json_opt_type_t tgbot_json_opt_type; typedef enum tgbot_opt_type tgbot_opt_type_e;
struct tgbot_json_option_t { struct tgbot_option {
char key[32]; char key[32];
void *value; void *value;
tgbot_json_opt_type type; tgbot_opt_type_e type;
}; };
typedef struct tgbot_json_option_t tgbot_json_option; typedef struct tgbot_option tgbot_option_s;
#endif #endif

View File

@@ -4,7 +4,7 @@
#include "types.h" #include "types.h"
#include <json-c/json.h> #include <json-c/json.h>
json_object *json_builder(tgbot_json_option *options, size_t optionslen); json_object *json_builder(tgbot_option_s *options, size_t optionslen);
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard); json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard);

View File

@@ -4,12 +4,8 @@
#include "types.h" #include "types.h"
#include <json-c/json.h> #include <json-c/json.h>
/* Retrieve update */
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler); tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result);
tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback query_handler);
/* Request */
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json);
/* Methods */ /* Methods */
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me); tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me);

View File

@@ -1,27 +1,41 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include "json.h" #include "json.h"
json_object *json_builder(tgbot_json_option *options, size_t optionslen) { json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
json_object *rjson = json_object_new_object(); json_object *rjson = json_object_new_object();
if (!rjson) {
return NULL;
}
for (size_t i = 0; i < optionslen; ++i) { for (size_t i = 0; i < optionslen; ++i) {
if (options[i].type == tgbot_opt_int) { switch (options[i].type) {
case tgbot_opt_int: {
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value))); json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
} else if (options[i].type == tgbot_opt_string) { break;
}
case tgbot_opt_string: {
if (!options[i].value) { if (!options[i].value) {
continue; break;
} }
json_object_object_add(rjson, options[i].key, json_object_new_string((char *)options[i].value)); json_object_object_add(rjson, options[i].key, json_object_new_string((char *)options[i].value));
} else if (options[i].type == tgbot_opt_int64) { break;
}
case tgbot_opt_int64: {
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value))); json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
} else if (options[i].type == tgbot_opt_inlinekeyboard) { break;
}
case tgbot_opt_inlinekeyboard: {
if (options[i].value != NULL) { if (options[i].value != NULL) {
json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value); json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value);
json_object_object_add(rjson, "reply_markup", reply_markup); json_object_object_add(rjson, options[i].key, reply_markup);
}
break;
}
case tgbot_opt_null: {
break;
} }
} else if (options[i].type == tgbot_opt_null) {
continue;
} }
} }
@@ -30,7 +44,15 @@ json_object *json_builder(tgbot_json_option *options, size_t optionslen) {
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) { json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
json_object *reply_markup = json_object_new_object(); json_object *reply_markup = json_object_new_object();
if (!reply_markup) {
return NULL;
}
json_object *inline_keyboard_array = json_object_new_array(); json_object *inline_keyboard_array = json_object_new_array();
if (!inline_keyboard_array) {
json_object_put(reply_markup);
return NULL;
}
for (size_t i = 0; i < keyboard->rows; ++i) { for (size_t i = 0; i < keyboard->rows; ++i) {
json_object *row = json_object_new_array(); json_object *row = json_object_new_array();
@@ -41,6 +63,10 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
} }
json_object *button = json_object_new_object(); json_object *button = json_object_new_object();
if (!button) {
continue;
}
json_object_object_add(button, "text", json_object_new_string(kbbutton->text)); json_object_object_add(button, "text", json_object_new_string(kbbutton->text));
json_object_object_add(button, "url", json_object_new_string(kbbutton->url)); json_object_object_add(button, "url", json_object_new_string(kbbutton->url));
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data)); json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));