/src/open5gs/lib/sbi/openapi/model/notify_item.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "notify_item.h" |
6 | | |
7 | | OpenAPI_notify_item_t *OpenAPI_notify_item_create( |
8 | | char *resource_id, |
9 | | OpenAPI_list_t *changes |
10 | | ) |
11 | 0 | { |
12 | 0 | OpenAPI_notify_item_t *notify_item_local_var = ogs_malloc(sizeof(OpenAPI_notify_item_t)); |
13 | 0 | ogs_assert(notify_item_local_var); |
14 | | |
15 | 0 | notify_item_local_var->resource_id = resource_id; |
16 | 0 | notify_item_local_var->changes = changes; |
17 | |
|
18 | 0 | return notify_item_local_var; |
19 | 0 | } |
20 | | |
21 | | void OpenAPI_notify_item_free(OpenAPI_notify_item_t *notify_item) |
22 | 0 | { |
23 | 0 | OpenAPI_lnode_t *node = NULL; |
24 | |
|
25 | 0 | if (NULL == notify_item) { |
26 | 0 | return; |
27 | 0 | } |
28 | 0 | if (notify_item->resource_id) { |
29 | 0 | ogs_free(notify_item->resource_id); |
30 | 0 | notify_item->resource_id = NULL; |
31 | 0 | } |
32 | 0 | if (notify_item->changes) { |
33 | 0 | OpenAPI_list_for_each(notify_item->changes, node) { |
34 | 0 | OpenAPI_change_item_free(node->data); |
35 | 0 | } |
36 | 0 | OpenAPI_list_free(notify_item->changes); |
37 | 0 | notify_item->changes = NULL; |
38 | 0 | } |
39 | 0 | ogs_free(notify_item); |
40 | 0 | } |
41 | | |
42 | | cJSON *OpenAPI_notify_item_convertToJSON(OpenAPI_notify_item_t *notify_item) |
43 | 0 | { |
44 | 0 | cJSON *item = NULL; |
45 | 0 | OpenAPI_lnode_t *node = NULL; |
46 | |
|
47 | 0 | if (notify_item == NULL) { |
48 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed [NotifyItem]"); |
49 | 0 | return NULL; |
50 | 0 | } |
51 | | |
52 | 0 | item = cJSON_CreateObject(); |
53 | 0 | if (!notify_item->resource_id) { |
54 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed [resource_id]"); |
55 | 0 | return NULL; |
56 | 0 | } |
57 | 0 | if (cJSON_AddStringToObject(item, "resourceId", notify_item->resource_id) == NULL) { |
58 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed [resource_id]"); |
59 | 0 | goto end; |
60 | 0 | } |
61 | | |
62 | 0 | if (!notify_item->changes) { |
63 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed [changes]"); |
64 | 0 | return NULL; |
65 | 0 | } |
66 | 0 | cJSON *changesList = cJSON_AddArrayToObject(item, "changes"); |
67 | 0 | if (changesList == NULL) { |
68 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed [changes]"); |
69 | 0 | goto end; |
70 | 0 | } |
71 | 0 | OpenAPI_list_for_each(notify_item->changes, node) { |
72 | 0 | cJSON *itemLocal = OpenAPI_change_item_convertToJSON(node->data); |
73 | 0 | if (itemLocal == NULL) { |
74 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed [changes]"); |
75 | 0 | goto end; |
76 | 0 | } |
77 | 0 | cJSON_AddItemToArray(changesList, itemLocal); |
78 | 0 | } |
79 | | |
80 | 0 | end: |
81 | 0 | return item; |
82 | 0 | } |
83 | | |
84 | | OpenAPI_notify_item_t *OpenAPI_notify_item_parseFromJSON(cJSON *notify_itemJSON) |
85 | 0 | { |
86 | 0 | OpenAPI_notify_item_t *notify_item_local_var = NULL; |
87 | 0 | OpenAPI_lnode_t *node = NULL; |
88 | 0 | cJSON *resource_id = NULL; |
89 | 0 | cJSON *changes = NULL; |
90 | 0 | OpenAPI_list_t *changesList = NULL; |
91 | 0 | resource_id = cJSON_GetObjectItemCaseSensitive(notify_itemJSON, "resourceId"); |
92 | 0 | if (!resource_id) { |
93 | 0 | ogs_error("OpenAPI_notify_item_parseFromJSON() failed [resource_id]"); |
94 | 0 | goto end; |
95 | 0 | } |
96 | 0 | if (!cJSON_IsString(resource_id)) { |
97 | 0 | ogs_error("OpenAPI_notify_item_parseFromJSON() failed [resource_id]"); |
98 | 0 | goto end; |
99 | 0 | } |
100 | | |
101 | 0 | changes = cJSON_GetObjectItemCaseSensitive(notify_itemJSON, "changes"); |
102 | 0 | if (!changes) { |
103 | 0 | ogs_error("OpenAPI_notify_item_parseFromJSON() failed [changes]"); |
104 | 0 | goto end; |
105 | 0 | } |
106 | 0 | cJSON *changes_local = NULL; |
107 | 0 | if (!cJSON_IsArray(changes)) { |
108 | 0 | ogs_error("OpenAPI_notify_item_parseFromJSON() failed [changes]"); |
109 | 0 | goto end; |
110 | 0 | } |
111 | | |
112 | 0 | changesList = OpenAPI_list_create(); |
113 | |
|
114 | 0 | cJSON_ArrayForEach(changes_local, changes) { |
115 | 0 | if (!cJSON_IsObject(changes_local)) { |
116 | 0 | ogs_error("OpenAPI_notify_item_parseFromJSON() failed [changes]"); |
117 | 0 | goto end; |
118 | 0 | } |
119 | 0 | OpenAPI_change_item_t *changesItem = OpenAPI_change_item_parseFromJSON(changes_local); |
120 | 0 | if (!changesItem) { |
121 | 0 | ogs_error("No changesItem"); |
122 | 0 | goto end; |
123 | 0 | } |
124 | 0 | OpenAPI_list_add(changesList, changesItem); |
125 | 0 | } |
126 | | |
127 | 0 | notify_item_local_var = OpenAPI_notify_item_create ( |
128 | 0 | ogs_strdup(resource_id->valuestring), |
129 | 0 | changesList |
130 | 0 | ); |
131 | |
|
132 | 0 | return notify_item_local_var; |
133 | 0 | end: |
134 | 0 | if (changesList) { |
135 | 0 | OpenAPI_list_for_each(changesList, node) { |
136 | 0 | OpenAPI_change_item_free(node->data); |
137 | 0 | } |
138 | 0 | OpenAPI_list_free(changesList); |
139 | 0 | changesList = NULL; |
140 | 0 | } |
141 | 0 | return NULL; |
142 | 0 | } |
143 | | |
144 | | OpenAPI_notify_item_t *OpenAPI_notify_item_copy(OpenAPI_notify_item_t *dst, OpenAPI_notify_item_t *src) |
145 | 0 | { |
146 | 0 | cJSON *item = NULL; |
147 | 0 | char *content = NULL; |
148 | |
|
149 | 0 | ogs_assert(src); |
150 | 0 | item = OpenAPI_notify_item_convertToJSON(src); |
151 | 0 | if (!item) { |
152 | 0 | ogs_error("OpenAPI_notify_item_convertToJSON() failed"); |
153 | 0 | return NULL; |
154 | 0 | } |
155 | | |
156 | 0 | content = cJSON_Print(item); |
157 | 0 | cJSON_Delete(item); |
158 | |
|
159 | 0 | if (!content) { |
160 | 0 | ogs_error("cJSON_Print() failed"); |
161 | 0 | return NULL; |
162 | 0 | } |
163 | | |
164 | 0 | item = cJSON_Parse(content); |
165 | 0 | ogs_free(content); |
166 | 0 | if (!item) { |
167 | 0 | ogs_error("cJSON_Parse() failed"); |
168 | 0 | return NULL; |
169 | 0 | } |
170 | | |
171 | 0 | OpenAPI_notify_item_free(dst); |
172 | 0 | dst = OpenAPI_notify_item_parseFromJSON(item); |
173 | 0 | cJSON_Delete(item); |
174 | |
|
175 | 0 | return dst; |
176 | 0 | } |
177 | | |