-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcetcd_json_parser.h
322 lines (305 loc) · 8.99 KB
/
cetcd_json_parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <yajl/yajl_parse.h>
#include <string.h>
#include <stdlib.h>
#include "cetcd.h"
typedef struct yajl_parser_context_t {
void *userdata;
cetcd_array keystack;
cetcd_array nodestack;
} yajl_parser_context;
#define EQ(s, d) ((strncmp((s), (d), sizeof((d)) - 1) == 0) && (sdslen(s) == (sizeof((d))-1)))
#define UNUSED(v) (void)(v)
static int cetcd_parse_action(cetcd_string act) {
if (EQ(act, "set")) {
return ETCD_SET;
}
if (EQ(act, "update")) {
return ETCD_UPDATE;
}
if (EQ(act, "get")) {
return ETCD_GET;
}
if (EQ(act, "delete")) {
return ETCD_DELETE;
}
if (EQ(act, "create")) {
return ETCD_CREATE;
}
if (EQ(act, "expire")) {
return ETCD_EXPIRE;
}
if (EQ(act, "compareAndSwap")) {
return ETCD_CAS;
}
if (EQ(act, "compareAndDelete")) {
return ETCD_CAD;
}
return -1;
}
static int yajl_parse_bool_cb(void *ctx, int val) {
yajl_parser_context *c = ctx;
cetcd_response_node *node;
cetcd_string key;
key = cetcd_array_pop(&c->keystack);
if (EQ(key, "dir")) {
node = cetcd_array_top(&c->nodestack);
node->dir = val;
}
sdsfree(key);
return 1;
}
static int yajl_parse_integer_cb(void *ctx, long long val) {
yajl_parser_context *c = ctx;
cetcd_response_node *node;
cetcd_string key;
key = cetcd_array_pop(&c->keystack);
node = cetcd_array_top(&c->nodestack);
if (EQ(key, "ttl")) {
node->ttl = (int64_t)val;
}
else if (EQ(key, "modifiedindex")) {
node->modified_index = (uint64_t) val;
}
else if (EQ(key, "createdindex")) {
node->created_index = (uint64_t) val;
}
else if (EQ(key, "expiration")) {
node->expiration = (uint64_t) val;
}
sdsfree(key);
return 1;
}
static int yajl_parse_string_cb(void *ctx, unsigned const char *val, size_t len) {
yajl_parser_context *c = ctx;
cetcd_response_node *node;
cetcd_response *resp;
cetcd_string key, value;
key = cetcd_array_pop(&c->keystack);
if (EQ(key, "key")) {
node = cetcd_array_top(&c->nodestack);
node->key = sdsnewlen(val, len);
}
else if (EQ(key, "value")) {
node = cetcd_array_top(&c->nodestack);
node->value = sdsnewlen(val, len);
}
else if (EQ(key, "action")) {
resp = c->userdata;
value = sdsnewlen(val, len);
resp->action = cetcd_parse_action(value);
sdsfree(value);
}
sdsfree(key);
return 1;
}
static int yajl_parse_start_map_cb(void *ctx) {
yajl_parser_context *c = ctx;
cetcd_string key;
cetcd_response_node *node, *child;
/*this is key of nodes*/
if (cetcd_array_size(&c->keystack) > 0) {
key = cetcd_array_top(&c->keystack);
node = cetcd_array_top(&c->nodestack);
if (EQ(key, "nodes")) {
child = calloc(1, sizeof(cetcd_response_node));
cetcd_array_append(node->nodes, child);
cetcd_array_append(&c->nodestack, child);
cetcd_array_append(&c->keystack, sdsnew("noname"));
}
return 1;
}
/*this is a cetcd_response*/
if (c->userdata == NULL) {
c->userdata = calloc(1, sizeof(cetcd_response));
}
return 1;
}
static int yajl_parse_map_key_cb(void *ctx, const unsigned char *key, size_t len) {
yajl_parser_context *c = ctx;
cetcd_response *resp = (cetcd_response *)c->userdata;
cetcd_string name = sdsnewlen(key, len);
sdstolower(name);
cetcd_array_append(&c->keystack, name);
if (EQ(name, "node")) {
resp->node = calloc(1, sizeof(cetcd_response_node));
cetcd_array_append(&c->nodestack, resp->node);
}
else if (EQ(name, "prevnode")) {
resp->prev_node = calloc(1, sizeof(cetcd_response_node));
cetcd_array_append(&c->nodestack, resp->prev_node);
}
return 1;
}
static int yajl_parse_end_map_cb(void *ctx) {
yajl_parser_context *c = ctx;
cetcd_string key = cetcd_array_pop(&c->keystack);
if (key) {
sdsfree(key);
}
cetcd_array_pop(&c->nodestack);
return 1;
}
static int yajl_parse_start_array_cb(void *ctx) {
yajl_parser_context *c = ctx;
cetcd_response_node *node;
cetcd_string key;
key = cetcd_array_top(&c->keystack);
node = (cetcd_response_node *)cetcd_array_top(&c->nodestack);
if (EQ(key, "nodes")) {
if (node) {
node->nodes = cetcd_array_create(10);
}
}
return 1;
}
static int yajl_parse_end_array_cb(void *ctx) {
yajl_parser_context *c = ctx;
cetcd_string key;
key = (cetcd_string)cetcd_array_top(&c->keystack);
if (EQ(key, "nodes")) {
sdsfree(cetcd_array_pop(&c->keystack));
}
return 1;
}
static int yajl_parse_null_ignore_cb(void *ctx) {
yajl_parser_context *c = ctx;
sdsfree(cetcd_array_pop(&c->keystack));
/*just ignore*/
return 1;
}
static int yajl_parse_double_ignore_cb(void *ctx, double val) {
UNUSED(val);
return yajl_parse_null_ignore_cb(ctx);
}
static int yajl_parse_bool_ignore_cb(void *ctx, int val) {
UNUSED(val);
return yajl_parse_null_ignore_cb(ctx);
}
static yajl_callbacks callbacks = {
yajl_parse_null_ignore_cb, //null
yajl_parse_bool_cb, //boolean
yajl_parse_integer_cb, //integer
yajl_parse_double_ignore_cb, //double
NULL, //number
yajl_parse_string_cb, //string
yajl_parse_start_map_cb, //start map
yajl_parse_map_key_cb, //map key
yajl_parse_end_map_cb, //end map
yajl_parse_start_array_cb, //start array
yajl_parse_end_array_cb //end array
};
/* Error message parse functions
* Parsing error response is more simple than a normal response,
* we do not have to handle the nested objects , so we do not need
* a nodestack.
* */
static int yajl_err_parse_integer_cb(void *ctx, long long val) {
yajl_parser_context *c = ctx;
cetcd_error *err = c->userdata;
cetcd_string key = cetcd_array_pop(&c->keystack);
if (EQ(key, "errorcode")) {
err->ecode = (int)val;
}
sdsfree(key);
return 1;
}
static int yajl_err_parse_string_cb(void *ctx, unsigned const char *val, size_t len) {
yajl_parser_context *c = ctx;
cetcd_error *err = c->userdata;
cetcd_string key = cetcd_array_pop(&c->keystack);
if (EQ(key, "message")) {
err->message = sdsnewlen(val, len);
}
else if (EQ(key, "cause")) {
err->cause = sdsnewlen(val, len);
}
sdsfree(key);
return 1;
}
static int yajl_err_parse_start_map_cb(void *ctx) {
yajl_parser_context *c = ctx;
if (c->userdata == NULL) {
c->userdata = calloc(1, sizeof(cetcd_error));
}
return 1;
}
static int yajl_err_parse_map_key_cb(void *ctx, unsigned const char *key, size_t len) {
yajl_parser_context *c = ctx;
cetcd_string name = sdsnewlen(key, len);
sdstolower(name);
cetcd_array_append(&c->keystack, name);
return 1;
}
static int yajl_err_parse_end_map_cb(void *ctx) {
UNUSED(ctx);
return 1;
}
static yajl_callbacks error_callbacks = {
yajl_parse_null_ignore_cb, //null
yajl_parse_bool_ignore_cb, //boolean
yajl_err_parse_integer_cb, //integer
yajl_parse_double_ignore_cb, //double
NULL, //number
yajl_err_parse_string_cb, //string
yajl_err_parse_start_map_cb, //start map
yajl_err_parse_map_key_cb, //map key
yajl_err_parse_end_map_cb, //end map
NULL, //start array
yajl_parse_null_ignore_cb //end array
};
static int yajl_sync_parse_string_cb(void *ctx, unsigned const char *val, size_t len) {
yajl_parser_context *c = ctx;
cetcd_array *array = c->userdata;
cetcd_string key = cetcd_array_top(&c->keystack);
if ( key && EQ(key, "clientURLs")) {
cetcd_array_append(array, sdsnewlen(val, len));
}
return 1;
}
static int yajl_sync_parse_start_map_cb(void *ctx) {
yajl_parser_context *c = ctx;
if (c->userdata == NULL) {
c->userdata = cetcd_array_create(10);
}
return 1;
}
static int yajl_sync_parse_map_key_cb(void *ctx, const unsigned char *key, size_t len) {
yajl_parser_context *c = ctx;
cetcd_string name = sdsnewlen(key, len);
if (EQ(name, "clientURLs")) {
cetcd_array_append(&c->keystack, name);
} else {
sdsfree(name);
}
return 1;
}
static int yajl_sync_parse_end_map_cb(void *ctx) {
yajl_parser_context *c = ctx;
cetcd_string key = cetcd_array_pop(&c->keystack);
if (key) {
sdsfree(key);
}
return 1;
}
static int yajl_sync_parse_end_array_cb(void *ctx) {
yajl_parser_context *c = ctx;
cetcd_string key;
key = cetcd_array_top(&c->keystack);
if (key && EQ(key, "clientURLs")) {
sdsfree(cetcd_array_pop(&c->keystack));
}
return 1;
}
static yajl_callbacks sync_callbacks = {
NULL, //null
NULL, //boolean
NULL, //integer
NULL, //double
NULL, //number
yajl_sync_parse_string_cb, //string
yajl_sync_parse_start_map_cb, //start map
yajl_sync_parse_map_key_cb, //map key
yajl_sync_parse_end_map_cb, //end map
NULL, //start array
yajl_sync_parse_end_array_cb //end array
};