/src/open5gs/lib/sbi/openapi/model/locality_description.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "locality_description.h" |
6 | | |
7 | | OpenAPI_locality_description_t *OpenAPI_locality_description_create( |
8 | | OpenAPI_locality_type_e locality_type, |
9 | | char *locality_value, |
10 | | OpenAPI_list_t *addl_loc_descr_items |
11 | | ) |
12 | 0 | { |
13 | 0 | OpenAPI_locality_description_t *locality_description_local_var = ogs_malloc(sizeof(OpenAPI_locality_description_t)); |
14 | 0 | ogs_assert(locality_description_local_var); |
15 | | |
16 | 0 | locality_description_local_var->locality_type = locality_type; |
17 | 0 | locality_description_local_var->locality_value = locality_value; |
18 | 0 | locality_description_local_var->addl_loc_descr_items = addl_loc_descr_items; |
19 | |
|
20 | 0 | return locality_description_local_var; |
21 | 0 | } |
22 | | |
23 | | void OpenAPI_locality_description_free(OpenAPI_locality_description_t *locality_description) |
24 | 0 | { |
25 | 0 | OpenAPI_lnode_t *node = NULL; |
26 | |
|
27 | 0 | if (NULL == locality_description) { |
28 | 0 | return; |
29 | 0 | } |
30 | 0 | if (locality_description->locality_value) { |
31 | 0 | ogs_free(locality_description->locality_value); |
32 | 0 | locality_description->locality_value = NULL; |
33 | 0 | } |
34 | 0 | if (locality_description->addl_loc_descr_items) { |
35 | 0 | OpenAPI_list_for_each(locality_description->addl_loc_descr_items, node) { |
36 | 0 | OpenAPI_locality_description_item_free(node->data); |
37 | 0 | } |
38 | 0 | OpenAPI_list_free(locality_description->addl_loc_descr_items); |
39 | 0 | locality_description->addl_loc_descr_items = NULL; |
40 | 0 | } |
41 | 0 | ogs_free(locality_description); |
42 | 0 | } |
43 | | |
44 | | cJSON *OpenAPI_locality_description_convertToJSON(OpenAPI_locality_description_t *locality_description) |
45 | 0 | { |
46 | 0 | cJSON *item = NULL; |
47 | 0 | OpenAPI_lnode_t *node = NULL; |
48 | |
|
49 | 0 | if (locality_description == NULL) { |
50 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [LocalityDescription]"); |
51 | 0 | return NULL; |
52 | 0 | } |
53 | | |
54 | 0 | item = cJSON_CreateObject(); |
55 | 0 | if (locality_description->locality_type == OpenAPI_locality_type_NULL) { |
56 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [locality_type]"); |
57 | 0 | return NULL; |
58 | 0 | } |
59 | 0 | if (cJSON_AddStringToObject(item, "localityType", OpenAPI_locality_type_ToString(locality_description->locality_type)) == NULL) { |
60 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [locality_type]"); |
61 | 0 | goto end; |
62 | 0 | } |
63 | | |
64 | 0 | if (!locality_description->locality_value) { |
65 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [locality_value]"); |
66 | 0 | return NULL; |
67 | 0 | } |
68 | 0 | if (cJSON_AddStringToObject(item, "localityValue", locality_description->locality_value) == NULL) { |
69 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [locality_value]"); |
70 | 0 | goto end; |
71 | 0 | } |
72 | | |
73 | 0 | if (locality_description->addl_loc_descr_items) { |
74 | 0 | cJSON *addl_loc_descr_itemsList = cJSON_AddArrayToObject(item, "addlLocDescrItems"); |
75 | 0 | if (addl_loc_descr_itemsList == NULL) { |
76 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [addl_loc_descr_items]"); |
77 | 0 | goto end; |
78 | 0 | } |
79 | 0 | OpenAPI_list_for_each(locality_description->addl_loc_descr_items, node) { |
80 | 0 | cJSON *itemLocal = OpenAPI_locality_description_item_convertToJSON(node->data); |
81 | 0 | if (itemLocal == NULL) { |
82 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed [addl_loc_descr_items]"); |
83 | 0 | goto end; |
84 | 0 | } |
85 | 0 | cJSON_AddItemToArray(addl_loc_descr_itemsList, itemLocal); |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | 0 | end: |
90 | 0 | return item; |
91 | 0 | } |
92 | | |
93 | | OpenAPI_locality_description_t *OpenAPI_locality_description_parseFromJSON(cJSON *locality_descriptionJSON) |
94 | 0 | { |
95 | 0 | OpenAPI_locality_description_t *locality_description_local_var = NULL; |
96 | 0 | OpenAPI_lnode_t *node = NULL; |
97 | 0 | cJSON *locality_type = NULL; |
98 | 0 | OpenAPI_locality_type_e locality_typeVariable = 0; |
99 | 0 | cJSON *locality_value = NULL; |
100 | 0 | cJSON *addl_loc_descr_items = NULL; |
101 | 0 | OpenAPI_list_t *addl_loc_descr_itemsList = NULL; |
102 | 0 | locality_type = cJSON_GetObjectItemCaseSensitive(locality_descriptionJSON, "localityType"); |
103 | 0 | if (!locality_type) { |
104 | 0 | ogs_error("OpenAPI_locality_description_parseFromJSON() failed [locality_type]"); |
105 | 0 | goto end; |
106 | 0 | } |
107 | 0 | if (!cJSON_IsString(locality_type)) { |
108 | 0 | ogs_error("OpenAPI_locality_description_parseFromJSON() failed [locality_type]"); |
109 | 0 | goto end; |
110 | 0 | } |
111 | 0 | locality_typeVariable = OpenAPI_locality_type_FromString(locality_type->valuestring); |
112 | |
|
113 | 0 | locality_value = cJSON_GetObjectItemCaseSensitive(locality_descriptionJSON, "localityValue"); |
114 | 0 | if (!locality_value) { |
115 | 0 | ogs_error("OpenAPI_locality_description_parseFromJSON() failed [locality_value]"); |
116 | 0 | goto end; |
117 | 0 | } |
118 | 0 | if (!cJSON_IsString(locality_value)) { |
119 | 0 | ogs_error("OpenAPI_locality_description_parseFromJSON() failed [locality_value]"); |
120 | 0 | goto end; |
121 | 0 | } |
122 | | |
123 | 0 | addl_loc_descr_items = cJSON_GetObjectItemCaseSensitive(locality_descriptionJSON, "addlLocDescrItems"); |
124 | 0 | if (addl_loc_descr_items) { |
125 | 0 | cJSON *addl_loc_descr_items_local = NULL; |
126 | 0 | if (!cJSON_IsArray(addl_loc_descr_items)) { |
127 | 0 | ogs_error("OpenAPI_locality_description_parseFromJSON() failed [addl_loc_descr_items]"); |
128 | 0 | goto end; |
129 | 0 | } |
130 | | |
131 | 0 | addl_loc_descr_itemsList = OpenAPI_list_create(); |
132 | |
|
133 | 0 | cJSON_ArrayForEach(addl_loc_descr_items_local, addl_loc_descr_items) { |
134 | 0 | if (!cJSON_IsObject(addl_loc_descr_items_local)) { |
135 | 0 | ogs_error("OpenAPI_locality_description_parseFromJSON() failed [addl_loc_descr_items]"); |
136 | 0 | goto end; |
137 | 0 | } |
138 | 0 | OpenAPI_locality_description_item_t *addl_loc_descr_itemsItem = OpenAPI_locality_description_item_parseFromJSON(addl_loc_descr_items_local); |
139 | 0 | if (!addl_loc_descr_itemsItem) { |
140 | 0 | ogs_error("No addl_loc_descr_itemsItem"); |
141 | 0 | goto end; |
142 | 0 | } |
143 | 0 | OpenAPI_list_add(addl_loc_descr_itemsList, addl_loc_descr_itemsItem); |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | 0 | locality_description_local_var = OpenAPI_locality_description_create ( |
148 | 0 | locality_typeVariable, |
149 | 0 | ogs_strdup(locality_value->valuestring), |
150 | 0 | addl_loc_descr_items ? addl_loc_descr_itemsList : NULL |
151 | 0 | ); |
152 | |
|
153 | 0 | return locality_description_local_var; |
154 | 0 | end: |
155 | 0 | if (addl_loc_descr_itemsList) { |
156 | 0 | OpenAPI_list_for_each(addl_loc_descr_itemsList, node) { |
157 | 0 | OpenAPI_locality_description_item_free(node->data); |
158 | 0 | } |
159 | 0 | OpenAPI_list_free(addl_loc_descr_itemsList); |
160 | 0 | addl_loc_descr_itemsList = NULL; |
161 | 0 | } |
162 | 0 | return NULL; |
163 | 0 | } |
164 | | |
165 | | OpenAPI_locality_description_t *OpenAPI_locality_description_copy(OpenAPI_locality_description_t *dst, OpenAPI_locality_description_t *src) |
166 | 0 | { |
167 | 0 | cJSON *item = NULL; |
168 | 0 | char *content = NULL; |
169 | |
|
170 | 0 | ogs_assert(src); |
171 | 0 | item = OpenAPI_locality_description_convertToJSON(src); |
172 | 0 | if (!item) { |
173 | 0 | ogs_error("OpenAPI_locality_description_convertToJSON() failed"); |
174 | 0 | return NULL; |
175 | 0 | } |
176 | | |
177 | 0 | content = cJSON_Print(item); |
178 | 0 | cJSON_Delete(item); |
179 | |
|
180 | 0 | if (!content) { |
181 | 0 | ogs_error("cJSON_Print() failed"); |
182 | 0 | return NULL; |
183 | 0 | } |
184 | | |
185 | 0 | item = cJSON_Parse(content); |
186 | 0 | ogs_free(content); |
187 | 0 | if (!item) { |
188 | 0 | ogs_error("cJSON_Parse() failed"); |
189 | 0 | return NULL; |
190 | 0 | } |
191 | | |
192 | 0 | OpenAPI_locality_description_free(dst); |
193 | 0 | dst = OpenAPI_locality_description_parseFromJSON(item); |
194 | 0 | cJSON_Delete(item); |
195 | |
|
196 | 0 | return dst; |
197 | 0 | } |
198 | | |