OpenAPI: Generate patch_item.[ch] from OpenAPI specifications

This commit is contained in:
mitmitmitm 2022-11-10 10:47:58 +01:00 committed by Sukchan Lee
parent 36734cac7c
commit b85ad61e2b
14 changed files with 204 additions and 173 deletions

View file

@ -2,63 +2,88 @@
bool OpenAPI_IsInvalid(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_Invalid;
return item && cJSON_IsInvalid(item->json);
}
bool OpenAPI_IsFalse(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_False;
return item && cJSON_IsFalse(item->json);
}
bool OpenAPI_IsTrue(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xff) == OpenAPI_True;
return item && cJSON_IsTrue(item->json);
}
bool OpenAPI_IsBool(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & (OpenAPI_True | OpenAPI_False)) != 0;
return item && cJSON_IsBool(item->json);
}
bool OpenAPI_IsNull(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_NULL;
return item && cJSON_IsNull(item->json);
}
bool OpenAPI_IsNumber(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_Number;
return item && cJSON_IsNumber(item->json);
}
bool OpenAPI_IsString(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_String;
return item && cJSON_IsString(item->json);
}
bool OpenAPI_IsArray(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_Array;
return item && cJSON_IsArray(item->json);
}
bool OpenAPI_IsObject(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return (item->type & 0xFF) == OpenAPI_Object;
return item && cJSON_IsObject(item->json);
}
bool OpenAPI_IsRaw(const OpenAPI_any_type_t * const item)
{
if (item == NULL) return false;
return item && cJSON_IsRaw(item->json);
}
return (item->type & 0xFF) == OpenAPI_Raw;
static OpenAPI_any_type_t *any_create(cJSON *json) {
OpenAPI_any_type_t *any_type = NULL;
if (!json) {
return NULL;
}
any_type = ogs_calloc(1, sizeof(*any_type));
if (!any_type) {
cJSON_Delete(json);
}
any_type->json = json;
return any_type;
}
OpenAPI_any_type_t *OpenAPI_any_type_create(cJSON *json) {
OpenAPI_any_type_t *any_type_local_var =
any_create(cJSON_Duplicate(json, true));
ogs_assert(any_type_local_var);
return any_type_local_var;
}
OpenAPI_any_type_t *OpenAPI_any_type_parseFromJSON(cJSON *json) {
return OpenAPI_any_type_create(json);
}
cJSON *OpenAPI_any_type_convertToJSON(OpenAPI_any_type_t *any_type) {
cJSON *item = NULL;
if (any_type == NULL) {
ogs_error("OpenAPI_any_type_convertToJSON() failed [AnyType]");
return NULL;
}
item = cJSON_Duplicate(any_type->json, true);
if (item == NULL) {
ogs_error("OpenAPI_any_type_convertToJSON() failed [AnyType]");
return NULL;
}
return item;
}
OpenAPI_any_type_t *OpenAPI_any_type_create_true(void)
@ -73,48 +98,24 @@ OpenAPI_any_type_t *OpenAPI_any_type_create_false(void)
OpenAPI_any_type_t *OpenAPI_any_type_create_bool(bool boolean)
{
OpenAPI_any_type_t *any_type = NULL;
any_type = ogs_calloc(1, sizeof(*any_type));
if (any_type) {
any_type->type = boolean ? OpenAPI_True : OpenAPI_False;
}
return any_type;
return any_create(boolean ? cJSON_CreateTrue() : cJSON_CreateFalse());
}
OpenAPI_any_type_t *OpenAPI_any_type_create_number(double num)
{
OpenAPI_any_type_t *any_type = NULL;
any_type = ogs_calloc(1, sizeof(*any_type));
if (any_type) {
any_type->type = OpenAPI_Number;
any_type->valuedouble = num;
}
return any_type;
return any_create(cJSON_CreateNumber(num));
}
OpenAPI_any_type_t *OpenAPI_any_type_create_string(const char *string)
{
OpenAPI_any_type_t *any_type = NULL;
any_type = ogs_calloc(1, sizeof(*any_type));
if (any_type) {
any_type->type = OpenAPI_String;
any_type->valuestring = ogs_strdup(string);
}
return any_type;
return any_create(cJSON_CreateString(string));
}
void OpenAPI_any_type_free(OpenAPI_any_type_t *any_type)
{
if (any_type) {
if (any_type->type == OpenAPI_String) {
ogs_free(any_type->valuestring);
}
cJSON_Delete(any_type->json);
ogs_free(any_type);
}
}

View file

@ -1,6 +1,7 @@
#ifndef OGS_SBI_ANY_TYPE_H
#define OGS_SBI_ANY_TYPE_H
#include "../external/cJSON.h"
#include "ogs-core.h"
#ifdef __cplusplus
@ -22,10 +23,7 @@ extern "C" {
typedef struct OpenAPI_any_type_s OpenAPI_any_type_t;
typedef struct OpenAPI_any_type_s {
int type;
char *valuestring;
double valuedouble;
cJSON *json;
} OpenAPI_any_type_t;
bool OpenAPI_IsInvalid(const OpenAPI_any_type_t * const item);
@ -39,6 +37,10 @@ bool OpenAPI_IsArray(const OpenAPI_any_type_t * const item);
bool OpenAPI_IsObject(const OpenAPI_any_type_t * const item);
bool OpenAPI_IsRaw(const OpenAPI_any_type_t * const item);
OpenAPI_any_type_t *OpenAPI_any_type_create(cJSON *json);
OpenAPI_any_type_t *OpenAPI_any_type_parseFromJSON(cJSON *json);
cJSON *OpenAPI_any_type_convertToJSON(OpenAPI_any_type_t *any_type);
OpenAPI_any_type_t *OpenAPI_any_type_create_true(void);
OpenAPI_any_type_t *OpenAPI_any_type_create_false(void);
OpenAPI_any_type_t *OpenAPI_any_type_create_bool(bool boolean);

View file

@ -2193,6 +2193,10 @@ OpenAPI_nf_profile_t *OpenAPI_nf_profile_parseFromJSON(cJSON *nf_profileJSON)
OpenAPI_object_t *custom_info_local_object = NULL;
if (custom_info) {
if (!cJSON_IsObject(custom_info)) {
ogs_error("OpenAPI_nf_profile_parseFromJSON() failed [custom_info]");
goto end;
}
custom_info_local_object = OpenAPI_object_parseFromJSON(custom_info);
}

View file

@ -0,0 +1,169 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "patch_item.h"
OpenAPI_patch_item_t *OpenAPI_patch_item_create(
OpenAPI_patch_operation_e op,
char *path,
char *from,
OpenAPI_any_type_t *value
)
{
OpenAPI_patch_item_t *patch_item_local_var = ogs_malloc(sizeof(OpenAPI_patch_item_t));
ogs_assert(patch_item_local_var);
patch_item_local_var->op = op;
patch_item_local_var->path = path;
patch_item_local_var->from = from;
patch_item_local_var->value = value;
return patch_item_local_var;
}
void OpenAPI_patch_item_free(OpenAPI_patch_item_t *patch_item)
{
if (NULL == patch_item) {
return;
}
OpenAPI_lnode_t *node;
ogs_free(patch_item->path);
ogs_free(patch_item->from);
OpenAPI_any_type_free(patch_item->value);
ogs_free(patch_item);
}
cJSON *OpenAPI_patch_item_convertToJSON(OpenAPI_patch_item_t *patch_item)
{
cJSON *item = NULL;
if (patch_item == NULL) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed [PatchItem]");
return NULL;
}
item = cJSON_CreateObject();
if (cJSON_AddStringToObject(item, "op", OpenAPI_patch_operation_ToString(patch_item->op)) == NULL) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed [op]");
goto end;
}
if (cJSON_AddStringToObject(item, "path", patch_item->path) == NULL) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed [path]");
goto end;
}
if (patch_item->from) {
if (cJSON_AddStringToObject(item, "from", patch_item->from) == NULL) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed [from]");
goto end;
}
}
if (patch_item->value) {
cJSON *value_object = OpenAPI_any_type_convertToJSON(patch_item->value);
if (value_object == NULL) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed [value]");
goto end;
}
cJSON_AddItemToObject(item, "value", value_object);
if (item->child == NULL) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed [value]");
goto end;
}
}
end:
return item;
}
OpenAPI_patch_item_t *OpenAPI_patch_item_parseFromJSON(cJSON *patch_itemJSON)
{
OpenAPI_patch_item_t *patch_item_local_var = NULL;
cJSON *op = cJSON_GetObjectItemCaseSensitive(patch_itemJSON, "op");
if (!op) {
ogs_error("OpenAPI_patch_item_parseFromJSON() failed [op]");
goto end;
}
OpenAPI_patch_operation_e opVariable = 0;
if (!cJSON_IsString(op)) {
ogs_error("OpenAPI_patch_item_parseFromJSON() failed [op]");
goto end;
}
opVariable = OpenAPI_patch_operation_FromString(op->valuestring);
cJSON *path = cJSON_GetObjectItemCaseSensitive(patch_itemJSON, "path");
if (!path) {
ogs_error("OpenAPI_patch_item_parseFromJSON() failed [path]");
goto end;
}
if (!cJSON_IsString(path)) {
ogs_error("OpenAPI_patch_item_parseFromJSON() failed [path]");
goto end;
}
cJSON *from = cJSON_GetObjectItemCaseSensitive(patch_itemJSON, "from");
if (from) {
if (!cJSON_IsString(from)) {
ogs_error("OpenAPI_patch_item_parseFromJSON() failed [from]");
goto end;
}
}
cJSON *value = cJSON_GetObjectItemCaseSensitive(patch_itemJSON, "value");
OpenAPI_any_type_t *value_local_object = NULL;
if (value) {
value_local_object = OpenAPI_any_type_parseFromJSON(value);
}
patch_item_local_var = OpenAPI_patch_item_create (
opVariable,
ogs_strdup(path->valuestring),
from ? ogs_strdup(from->valuestring) : NULL,
value ? value_local_object : NULL
);
return patch_item_local_var;
end:
return NULL;
}
OpenAPI_patch_item_t *OpenAPI_patch_item_copy(OpenAPI_patch_item_t *dst, OpenAPI_patch_item_t *src)
{
cJSON *item = NULL;
char *content = NULL;
ogs_assert(src);
item = OpenAPI_patch_item_convertToJSON(src);
if (!item) {
ogs_error("OpenAPI_patch_item_convertToJSON() failed");
return NULL;
}
content = cJSON_Print(item);
cJSON_Delete(item);
if (!content) {
ogs_error("cJSON_Print() failed");
return NULL;
}
item = cJSON_Parse(content);
ogs_free(content);
if (!item) {
ogs_error("cJSON_Parse() failed");
return NULL;
}
OpenAPI_patch_item_free(dst);
dst = OpenAPI_patch_item_parseFromJSON(item);
cJSON_Delete(item);
return dst;
}

View file

@ -0,0 +1,46 @@
/*
* patch_item.h
*
*
*/
#ifndef _OpenAPI_patch_item_H_
#define _OpenAPI_patch_item_H_
#include <string.h>
#include "../external/cJSON.h"
#include "../include/list.h"
#include "../include/keyValuePair.h"
#include "../include/binary.h"
#include "any_type.h"
#include "patch_operation.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct OpenAPI_patch_item_s OpenAPI_patch_item_t;
typedef struct OpenAPI_patch_item_s {
OpenAPI_patch_operation_e op;
char *path;
char *from;
OpenAPI_any_type_t *value;
} OpenAPI_patch_item_t;
OpenAPI_patch_item_t *OpenAPI_patch_item_create(
OpenAPI_patch_operation_e op,
char *path,
char *from,
OpenAPI_any_type_t *value
);
void OpenAPI_patch_item_free(OpenAPI_patch_item_t *patch_item);
OpenAPI_patch_item_t *OpenAPI_patch_item_parseFromJSON(cJSON *patch_itemJSON);
cJSON *OpenAPI_patch_item_convertToJSON(OpenAPI_patch_item_t *patch_item);
OpenAPI_patch_item_t *OpenAPI_patch_item_copy(OpenAPI_patch_item_t *dst, OpenAPI_patch_item_t *src);
#ifdef __cplusplus
}
#endif
#endif /* _OpenAPI_patch_item_H_ */