Skip to content

Commit b47d6f8

Browse files
authored
Merge pull request #128 from ityuhui/yh-freefrom-object-0614
Support free-form objects
2 parents 74e4fcc + 878fde5 commit b47d6f8

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Diff for: kubernetes/model/object.c

+28-8
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,48 @@
44
#include "object.h"
55

66
object_t *object_create() {
7-
object_t *object = malloc(sizeof(object_t));
7+
object_t *object = calloc(1, sizeof(object_t));
88

99
return object;
1010
}
1111

1212
void object_free(object_t *object) {
13+
if (!object) {
14+
return ;
15+
}
16+
17+
if (object->temporary) {
18+
free(object->temporary);
19+
object->temporary = NULL;
20+
}
21+
1322
free (object);
1423
}
1524

1625
cJSON *object_convertToJSON(object_t *object) {
17-
cJSON *item = cJSON_CreateObject();
26+
if (!object) {
27+
return NULL;
28+
}
1829

19-
return item;
20-
fail:
21-
cJSON_Delete(item);
22-
return NULL;
30+
if (!object->temporary) {
31+
return cJSON_Parse("null");
32+
}
33+
34+
return cJSON_Parse(object->temporary);
2335
}
2436

25-
object_t *object_parseFromJSON(char *jsonString){
26-
object_t *object = NULL;
37+
object_t *object_parseFromJSON(cJSON *json){
38+
if (!json) {
39+
goto end;
40+
}
2741

42+
object_t *object = object_create();
43+
if (!object) {
44+
goto end;
45+
}
46+
object->temporary = cJSON_Print(json);
2847
return object;
48+
2949
end:
3050
return NULL;
3151
}

Diff for: kubernetes/model/object.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object_t *object_create();
2020

2121
void object_free(object_t *object);
2222

23-
object_t *object_parseFromJSON(char *jsonString);
23+
object_t *object_parseFromJSON(cJSON *json);
2424

2525
cJSON *object_convertToJSON(object_t *object);
2626

0 commit comments

Comments
 (0)