forked from loafle/openapi-generator-original
* [C] Deprecate *_create() to avoid *_free() confusion The behaviour of *_free() doesn't match *_create(), so the user should avoid using them together. But they still need *_free() to clean up library-allocated objects, so add a _library_owned flag to each struct as an attempt to tell them apart. This isn't perfect though, because the user may neglect to zero the field, but they would still see a warning once in a while so it serves its purpose. To prevent the new deprecation warnings (intended for the user) from showing up during the library build itself, define a new family of *_create_internal() functions, and turn *_create() into simple wrappers. * Update samples * add eafer to c technical committee --------- Co-authored-by: William Cheng <wing328hk@gmail.com>
143 lines
3.2 KiB
C
143 lines
3.2 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "api_response.h"
|
|
|
|
|
|
|
|
static api_response_t *api_response_create_internal(
|
|
int code,
|
|
char *type,
|
|
char *message
|
|
) {
|
|
api_response_t *api_response_local_var = malloc(sizeof(api_response_t));
|
|
if (!api_response_local_var) {
|
|
return NULL;
|
|
}
|
|
api_response_local_var->code = code;
|
|
api_response_local_var->type = type;
|
|
api_response_local_var->message = message;
|
|
|
|
api_response_local_var->_library_owned = 1;
|
|
return api_response_local_var;
|
|
}
|
|
|
|
__attribute__((deprecated)) api_response_t *api_response_create(
|
|
int code,
|
|
char *type,
|
|
char *message
|
|
) {
|
|
return api_response_create_internal (
|
|
code,
|
|
type,
|
|
message
|
|
);
|
|
}
|
|
|
|
void api_response_free(api_response_t *api_response) {
|
|
if(NULL == api_response){
|
|
return ;
|
|
}
|
|
if(api_response->_library_owned != 1){
|
|
fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "api_response_free");
|
|
return ;
|
|
}
|
|
listEntry_t *listEntry;
|
|
if (api_response->type) {
|
|
free(api_response->type);
|
|
api_response->type = NULL;
|
|
}
|
|
if (api_response->message) {
|
|
free(api_response->message);
|
|
api_response->message = NULL;
|
|
}
|
|
free(api_response);
|
|
}
|
|
|
|
cJSON *api_response_convertToJSON(api_response_t *api_response) {
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
// api_response->code
|
|
if(api_response->code) {
|
|
if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) {
|
|
goto fail; //Numeric
|
|
}
|
|
}
|
|
|
|
|
|
// api_response->type
|
|
if(api_response->type) {
|
|
if(cJSON_AddStringToObject(item, "type", api_response->type) == NULL) {
|
|
goto fail; //String
|
|
}
|
|
}
|
|
|
|
|
|
// api_response->message
|
|
if(api_response->message) {
|
|
if(cJSON_AddStringToObject(item, "message", api_response->message) == NULL) {
|
|
goto fail; //String
|
|
}
|
|
}
|
|
|
|
return item;
|
|
fail:
|
|
if (item) {
|
|
cJSON_Delete(item);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
|
|
|
|
api_response_t *api_response_local_var = NULL;
|
|
|
|
// api_response->code
|
|
cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code");
|
|
if (cJSON_IsNull(code)) {
|
|
code = NULL;
|
|
}
|
|
if (code) {
|
|
if(!cJSON_IsNumber(code))
|
|
{
|
|
goto end; //Numeric
|
|
}
|
|
}
|
|
|
|
// api_response->type
|
|
cJSON *type = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "type");
|
|
if (cJSON_IsNull(type)) {
|
|
type = NULL;
|
|
}
|
|
if (type) {
|
|
if(!cJSON_IsString(type) && !cJSON_IsNull(type))
|
|
{
|
|
goto end; //String
|
|
}
|
|
}
|
|
|
|
// api_response->message
|
|
cJSON *message = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "message");
|
|
if (cJSON_IsNull(message)) {
|
|
message = NULL;
|
|
}
|
|
if (message) {
|
|
if(!cJSON_IsString(message) && !cJSON_IsNull(message))
|
|
{
|
|
goto end; //String
|
|
}
|
|
}
|
|
|
|
|
|
api_response_local_var = api_response_create_internal (
|
|
code ? code->valuedouble : 0,
|
|
type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL,
|
|
message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL
|
|
);
|
|
|
|
return api_response_local_var;
|
|
end:
|
|
return NULL;
|
|
|
|
}
|