| 30 |
ashish |
1 |
/**
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
#include <struct.h>
|
|
|
21 |
#include <constants.h>
|
|
|
22 |
#include "macros.h"
|
|
|
23 |
|
|
|
24 |
#ifndef HAVE_STRLCPY
|
|
|
25 |
|
|
|
26 |
static
|
|
|
27 |
size_t
|
|
|
28 |
strlcpy (char *dst, const char *src, size_t dst_sz)
|
|
|
29 |
{
|
|
|
30 |
size_t n;
|
|
|
31 |
|
|
|
32 |
for (n = 0; n < dst_sz; n++) {
|
|
|
33 |
if ((*dst++ = *src++) == '\0')
|
|
|
34 |
break;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
if (n < dst_sz)
|
|
|
38 |
return n;
|
|
|
39 |
if (n > 0)
|
|
|
40 |
*(dst - 1) = '\0';
|
|
|
41 |
return n + strlen (src);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
#endif
|
|
|
45 |
|
|
|
46 |
static native_proto_method_table *mt;
|
|
|
47 |
static native_proto_method_table *default_mt;
|
|
|
48 |
// static VALUE last_proto_class = Qnil;
|
|
|
49 |
|
|
|
50 |
#define IS_CONTAINER(ttype) ((ttype) == TTYPE_MAP || (ttype) == TTYPE_LIST || (ttype) == TTYPE_SET)
|
|
|
51 |
#define STRUCT_FIELDS(obj) rb_const_get(CLASS_OF(obj), fields_const_id)
|
|
|
52 |
|
|
|
53 |
// static void set_native_proto_function_pointers(VALUE protocol) {
|
|
|
54 |
// VALUE method_table_object = rb_const_get(CLASS_OF(protocol), rb_intern("@native_method_table"));
|
|
|
55 |
// // TODO: check nil?
|
|
|
56 |
// Data_Get_Struct(method_table_object, native_proto_method_table, mt);
|
|
|
57 |
// }
|
|
|
58 |
|
|
|
59 |
// static void check_native_proto_method_table(VALUE protocol) {
|
|
|
60 |
// VALUE protoclass = CLASS_OF(protocol);
|
|
|
61 |
// if (protoclass != last_proto_class) {
|
|
|
62 |
// last_proto_class = protoclass;
|
|
|
63 |
// if (rb_funcall(protocol, native_qmark_method_id, 0) == Qtrue) {
|
|
|
64 |
// set_native_proto_function_pointers(protocol);
|
|
|
65 |
// } else {
|
|
|
66 |
// mt = default_mt;
|
|
|
67 |
// }
|
|
|
68 |
// }
|
|
|
69 |
// }
|
|
|
70 |
|
|
|
71 |
//-------------------------------------------
|
|
|
72 |
// Writing section
|
|
|
73 |
//-------------------------------------------
|
|
|
74 |
|
|
|
75 |
// default fn pointers for protocol stuff here
|
|
|
76 |
|
|
|
77 |
VALUE default_write_bool(VALUE protocol, VALUE value) {
|
|
|
78 |
rb_funcall(protocol, write_boolean_method_id, 1, value);
|
|
|
79 |
return Qnil;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
VALUE default_write_byte(VALUE protocol, VALUE value) {
|
|
|
83 |
rb_funcall(protocol, write_byte_method_id, 1, value);
|
|
|
84 |
return Qnil;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
VALUE default_write_i16(VALUE protocol, VALUE value) {
|
|
|
88 |
rb_funcall(protocol, write_i16_method_id, 1, value);
|
|
|
89 |
return Qnil;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
VALUE default_write_i32(VALUE protocol, VALUE value) {
|
|
|
93 |
rb_funcall(protocol, write_i32_method_id, 1, value);
|
|
|
94 |
return Qnil;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
VALUE default_write_i64(VALUE protocol, VALUE value) {
|
|
|
98 |
rb_funcall(protocol, write_i64_method_id, 1, value);
|
|
|
99 |
return Qnil;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
VALUE default_write_double(VALUE protocol, VALUE value) {
|
|
|
103 |
rb_funcall(protocol, write_double_method_id, 1, value);
|
|
|
104 |
return Qnil;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
VALUE default_write_string(VALUE protocol, VALUE value) {
|
|
|
108 |
rb_funcall(protocol, write_string_method_id, 1, value);
|
|
|
109 |
return Qnil;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
VALUE default_write_list_begin(VALUE protocol, VALUE etype, VALUE length) {
|
|
|
113 |
rb_funcall(protocol, write_list_begin_method_id, 2, etype, length);
|
|
|
114 |
return Qnil;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
VALUE default_write_list_end(VALUE protocol) {
|
|
|
118 |
rb_funcall(protocol, write_list_end_method_id, 0);
|
|
|
119 |
return Qnil;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
VALUE default_write_set_begin(VALUE protocol, VALUE etype, VALUE length) {
|
|
|
123 |
rb_funcall(protocol, write_set_begin_method_id, 2, etype, length);
|
|
|
124 |
return Qnil;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
VALUE default_write_set_end(VALUE protocol) {
|
|
|
128 |
rb_funcall(protocol, write_set_end_method_id, 0);
|
|
|
129 |
return Qnil;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
VALUE default_write_map_begin(VALUE protocol, VALUE ktype, VALUE vtype, VALUE length) {
|
|
|
133 |
rb_funcall(protocol, write_map_begin_method_id, 3, ktype, vtype, length);
|
|
|
134 |
return Qnil;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
VALUE default_write_map_end(VALUE protocol) {
|
|
|
138 |
rb_funcall(protocol, write_map_end_method_id, 0);
|
|
|
139 |
return Qnil;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
VALUE default_write_struct_begin(VALUE protocol, VALUE struct_name) {
|
|
|
143 |
rb_funcall(protocol, write_struct_begin_method_id, 1, struct_name);
|
|
|
144 |
return Qnil;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
VALUE default_write_struct_end(VALUE protocol) {
|
|
|
148 |
rb_funcall(protocol, write_struct_end_method_id, 0);
|
|
|
149 |
return Qnil;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
VALUE default_write_field_begin(VALUE protocol, VALUE name, VALUE type, VALUE id) {
|
|
|
153 |
rb_funcall(protocol, write_field_begin_method_id, 3, name, type, id);
|
|
|
154 |
return Qnil;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
VALUE default_write_field_end(VALUE protocol) {
|
|
|
158 |
rb_funcall(protocol, write_field_end_method_id, 0);
|
|
|
159 |
return Qnil;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
VALUE default_write_field_stop(VALUE protocol) {
|
|
|
163 |
rb_funcall(protocol, write_field_stop_method_id, 0);
|
|
|
164 |
return Qnil;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
VALUE default_read_field_begin(VALUE protocol) {
|
|
|
168 |
return rb_funcall(protocol, read_field_begin_method_id, 0);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
VALUE default_read_field_end(VALUE protocol) {
|
|
|
172 |
return rb_funcall(protocol, read_field_end_method_id, 0);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
VALUE default_read_map_begin(VALUE protocol) {
|
|
|
176 |
return rb_funcall(protocol, read_map_begin_method_id, 0);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
VALUE default_read_map_end(VALUE protocol) {
|
|
|
180 |
return rb_funcall(protocol, read_map_end_method_id, 0);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
VALUE default_read_list_begin(VALUE protocol) {
|
|
|
184 |
return rb_funcall(protocol, read_list_begin_method_id, 0);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
VALUE default_read_list_end(VALUE protocol) {
|
|
|
188 |
return rb_funcall(protocol, read_list_end_method_id, 0);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
VALUE default_read_set_begin(VALUE protocol) {
|
|
|
192 |
return rb_funcall(protocol, read_set_begin_method_id, 0);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
VALUE default_read_set_end(VALUE protocol) {
|
|
|
196 |
return rb_funcall(protocol, read_set_end_method_id, 0);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
VALUE default_read_byte(VALUE protocol) {
|
|
|
200 |
return rb_funcall(protocol, read_byte_method_id, 0);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
VALUE default_read_bool(VALUE protocol) {
|
|
|
204 |
return rb_funcall(protocol, read_bool_method_id, 0);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
VALUE default_read_i16(VALUE protocol) {
|
|
|
208 |
return rb_funcall(protocol, read_i16_method_id, 0);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
VALUE default_read_i32(VALUE protocol) {
|
|
|
212 |
return rb_funcall(protocol, read_i32_method_id, 0);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
VALUE default_read_i64(VALUE protocol) {
|
|
|
216 |
return rb_funcall(protocol, read_i64_method_id, 0);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
VALUE default_read_double(VALUE protocol) {
|
|
|
220 |
return rb_funcall(protocol, read_double_method_id, 0);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
VALUE default_read_string(VALUE protocol) {
|
|
|
224 |
return rb_funcall(protocol, read_string_method_id, 0);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
VALUE default_read_struct_begin(VALUE protocol) {
|
|
|
228 |
return rb_funcall(protocol, read_struct_begin_method_id, 0);
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
VALUE default_read_struct_end(VALUE protocol) {
|
|
|
232 |
return rb_funcall(protocol, read_struct_end_method_id, 0);
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
static void set_default_proto_function_pointers() {
|
|
|
236 |
default_mt = ALLOC(native_proto_method_table);
|
|
|
237 |
|
|
|
238 |
default_mt->write_field_begin = default_write_field_begin;
|
|
|
239 |
default_mt->write_field_stop = default_write_field_stop;
|
|
|
240 |
default_mt->write_map_begin = default_write_map_begin;
|
|
|
241 |
default_mt->write_map_end = default_write_map_end;
|
|
|
242 |
default_mt->write_list_begin = default_write_list_begin;
|
|
|
243 |
default_mt->write_list_end = default_write_list_end;
|
|
|
244 |
default_mt->write_set_begin = default_write_set_begin;
|
|
|
245 |
default_mt->write_set_end = default_write_set_end;
|
|
|
246 |
default_mt->write_byte = default_write_byte;
|
|
|
247 |
default_mt->write_bool = default_write_bool;
|
|
|
248 |
default_mt->write_i16 = default_write_i16;
|
|
|
249 |
default_mt->write_i32 = default_write_i32;
|
|
|
250 |
default_mt->write_i64 = default_write_i64;
|
|
|
251 |
default_mt->write_double = default_write_double;
|
|
|
252 |
default_mt->write_string = default_write_string;
|
|
|
253 |
default_mt->write_struct_begin = default_write_struct_begin;
|
|
|
254 |
default_mt->write_struct_end = default_write_struct_end;
|
|
|
255 |
default_mt->write_field_end = default_write_field_end;
|
|
|
256 |
|
|
|
257 |
default_mt->read_struct_begin = default_read_struct_begin;
|
|
|
258 |
default_mt->read_struct_end = default_read_struct_end;
|
|
|
259 |
default_mt->read_field_begin = default_read_field_begin;
|
|
|
260 |
default_mt->read_field_end = default_read_field_end;
|
|
|
261 |
default_mt->read_map_begin = default_read_map_begin;
|
|
|
262 |
default_mt->read_map_end = default_read_map_end;
|
|
|
263 |
default_mt->read_list_begin = default_read_list_begin;
|
|
|
264 |
default_mt->read_list_end = default_read_list_end;
|
|
|
265 |
default_mt->read_set_begin = default_read_set_begin;
|
|
|
266 |
default_mt->read_set_end = default_read_set_end;
|
|
|
267 |
default_mt->read_byte = default_read_byte;
|
|
|
268 |
default_mt->read_bool = default_read_bool;
|
|
|
269 |
default_mt->read_i16 = default_read_i16;
|
|
|
270 |
default_mt->read_i32 = default_read_i32;
|
|
|
271 |
default_mt->read_i64 = default_read_i64;
|
|
|
272 |
default_mt->read_double = default_read_double;
|
|
|
273 |
default_mt->read_string = default_read_string;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
// end default protocol methods
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol);
|
|
|
280 |
static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info);
|
|
|
281 |
|
|
|
282 |
VALUE get_field_value(VALUE obj, VALUE field_name) {
|
|
|
283 |
char name_buf[RSTRING_LEN(field_name) + 1];
|
|
|
284 |
|
|
|
285 |
name_buf[0] = '@';
|
|
|
286 |
strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf));
|
|
|
287 |
|
|
|
288 |
VALUE value = rb_ivar_get(obj, rb_intern(name_buf));
|
|
|
289 |
|
|
|
290 |
return value;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
static void write_container(int ttype, VALUE field_info, VALUE value, VALUE protocol) {
|
|
|
294 |
int sz, i;
|
|
|
295 |
|
|
|
296 |
if (ttype == TTYPE_MAP) {
|
|
|
297 |
VALUE keys;
|
|
|
298 |
VALUE key;
|
|
|
299 |
VALUE val;
|
|
|
300 |
|
|
|
301 |
Check_Type(value, T_HASH);
|
|
|
302 |
|
|
|
303 |
VALUE key_info = rb_hash_aref(field_info, key_sym);
|
|
|
304 |
VALUE keytype_value = rb_hash_aref(key_info, type_sym);
|
|
|
305 |
int keytype = FIX2INT(keytype_value);
|
|
|
306 |
|
|
|
307 |
VALUE value_info = rb_hash_aref(field_info, value_sym);
|
|
|
308 |
VALUE valuetype_value = rb_hash_aref(value_info, type_sym);
|
|
|
309 |
int valuetype = FIX2INT(valuetype_value);
|
|
|
310 |
|
|
|
311 |
keys = rb_funcall(value, keys_method_id, 0);
|
|
|
312 |
|
|
|
313 |
sz = RARRAY_LEN(keys);
|
|
|
314 |
|
|
|
315 |
mt->write_map_begin(protocol, keytype_value, valuetype_value, INT2FIX(sz));
|
|
|
316 |
|
|
|
317 |
for (i = 0; i < sz; i++) {
|
|
|
318 |
key = rb_ary_entry(keys, i);
|
|
|
319 |
val = rb_hash_aref(value, key);
|
|
|
320 |
|
|
|
321 |
if (IS_CONTAINER(keytype)) {
|
|
|
322 |
write_container(keytype, key_info, key, protocol);
|
|
|
323 |
} else {
|
|
|
324 |
write_anything(keytype, key, protocol, key_info);
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
if (IS_CONTAINER(valuetype)) {
|
|
|
328 |
write_container(valuetype, value_info, val, protocol);
|
|
|
329 |
} else {
|
|
|
330 |
write_anything(valuetype, val, protocol, value_info);
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
mt->write_map_end(protocol);
|
|
|
335 |
} else if (ttype == TTYPE_LIST) {
|
|
|
336 |
Check_Type(value, T_ARRAY);
|
|
|
337 |
|
|
|
338 |
sz = RARRAY_LEN(value);
|
|
|
339 |
|
|
|
340 |
VALUE element_type_info = rb_hash_aref(field_info, element_sym);
|
|
|
341 |
VALUE element_type_value = rb_hash_aref(element_type_info, type_sym);
|
|
|
342 |
int element_type = FIX2INT(element_type_value);
|
|
|
343 |
|
|
|
344 |
mt->write_list_begin(protocol, element_type_value, INT2FIX(sz));
|
|
|
345 |
for (i = 0; i < sz; ++i) {
|
|
|
346 |
VALUE val = rb_ary_entry(value, i);
|
|
|
347 |
if (IS_CONTAINER(element_type)) {
|
|
|
348 |
write_container(element_type, element_type_info, val, protocol);
|
|
|
349 |
} else {
|
|
|
350 |
write_anything(element_type, val, protocol, element_type_info);
|
|
|
351 |
}
|
|
|
352 |
}
|
|
|
353 |
mt->write_list_end(protocol);
|
|
|
354 |
} else if (ttype == TTYPE_SET) {
|
|
|
355 |
VALUE items;
|
|
|
356 |
|
|
|
357 |
if (TYPE(value) == T_ARRAY) {
|
|
|
358 |
items = value;
|
|
|
359 |
} else {
|
|
|
360 |
if (rb_cSet == CLASS_OF(value)) {
|
|
|
361 |
items = rb_funcall(value, entries_method_id, 0);
|
|
|
362 |
} else {
|
|
|
363 |
Check_Type(value, T_HASH);
|
|
|
364 |
items = rb_funcall(value, keys_method_id, 0);
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
sz = RARRAY_LEN(items);
|
|
|
369 |
|
|
|
370 |
VALUE element_type_info = rb_hash_aref(field_info, element_sym);
|
|
|
371 |
VALUE element_type_value = rb_hash_aref(element_type_info, type_sym);
|
|
|
372 |
int element_type = FIX2INT(element_type_value);
|
|
|
373 |
|
|
|
374 |
mt->write_set_begin(protocol, element_type_value, INT2FIX(sz));
|
|
|
375 |
|
|
|
376 |
for (i = 0; i < sz; i++) {
|
|
|
377 |
VALUE val = rb_ary_entry(items, i);
|
|
|
378 |
if (IS_CONTAINER(element_type)) {
|
|
|
379 |
write_container(element_type, element_type_info, val, protocol);
|
|
|
380 |
} else {
|
|
|
381 |
write_anything(element_type, val, protocol, element_type_info);
|
|
|
382 |
}
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
mt->write_set_end(protocol);
|
|
|
386 |
} else {
|
|
|
387 |
rb_raise(rb_eNotImpError, "can't write container of type: %d", ttype);
|
|
|
388 |
}
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info) {
|
|
|
392 |
if (ttype == TTYPE_BOOL) {
|
|
|
393 |
mt->write_bool(protocol, value);
|
|
|
394 |
} else if (ttype == TTYPE_BYTE) {
|
|
|
395 |
mt->write_byte(protocol, value);
|
|
|
396 |
} else if (ttype == TTYPE_I16) {
|
|
|
397 |
mt->write_i16(protocol, value);
|
|
|
398 |
} else if (ttype == TTYPE_I32) {
|
|
|
399 |
mt->write_i32(protocol, value);
|
|
|
400 |
} else if (ttype == TTYPE_I64) {
|
|
|
401 |
mt->write_i64(protocol, value);
|
|
|
402 |
} else if (ttype == TTYPE_DOUBLE) {
|
|
|
403 |
mt->write_double(protocol, value);
|
|
|
404 |
} else if (ttype == TTYPE_STRING) {
|
|
|
405 |
mt->write_string(protocol, value);
|
|
|
406 |
} else if (IS_CONTAINER(ttype)) {
|
|
|
407 |
write_container(ttype, field_info, value, protocol);
|
|
|
408 |
} else if (ttype == TTYPE_STRUCT) {
|
|
|
409 |
rb_thrift_struct_write(value, protocol);
|
|
|
410 |
} else {
|
|
|
411 |
rb_raise(rb_eNotImpError, "Unknown type for binary_encoding: %d", ttype);
|
|
|
412 |
}
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) {
|
|
|
416 |
// call validate
|
|
|
417 |
rb_funcall(self, validate_method_id, 0);
|
|
|
418 |
|
|
|
419 |
// check_native_proto_method_table(protocol);
|
|
|
420 |
|
|
|
421 |
// write struct begin
|
|
|
422 |
mt->write_struct_begin(protocol, rb_class_name(CLASS_OF(self)));
|
|
|
423 |
|
|
|
424 |
// iterate through all the fields here
|
|
|
425 |
VALUE struct_fields = STRUCT_FIELDS(self);
|
|
|
426 |
VALUE struct_field_ids_unordered = rb_funcall(struct_fields, keys_method_id, 0);
|
|
|
427 |
VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0);
|
|
|
428 |
|
|
|
429 |
int i = 0;
|
|
|
430 |
for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) {
|
|
|
431 |
VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i);
|
|
|
432 |
VALUE field_info = rb_hash_aref(struct_fields, field_id);
|
|
|
433 |
|
|
|
434 |
VALUE ttype_value = rb_hash_aref(field_info, type_sym);
|
|
|
435 |
int ttype = FIX2INT(ttype_value);
|
|
|
436 |
VALUE field_name = rb_hash_aref(field_info, name_sym);
|
|
|
437 |
VALUE field_value = get_field_value(self, field_name);
|
|
|
438 |
|
|
|
439 |
if (!NIL_P(field_value)) {
|
|
|
440 |
mt->write_field_begin(protocol, field_name, ttype_value, field_id);
|
|
|
441 |
|
|
|
442 |
write_anything(ttype, field_value, protocol, field_info);
|
|
|
443 |
|
|
|
444 |
mt->write_field_end(protocol);
|
|
|
445 |
}
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
mt->write_field_stop(protocol);
|
|
|
449 |
|
|
|
450 |
// write struct end
|
|
|
451 |
mt->write_struct_end(protocol);
|
|
|
452 |
|
|
|
453 |
return Qnil;
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
//-------------------------------------------
|
|
|
457 |
// Reading section
|
|
|
458 |
//-------------------------------------------
|
|
|
459 |
|
|
|
460 |
static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol);
|
|
|
461 |
|
|
|
462 |
static void set_field_value(VALUE obj, VALUE field_name, VALUE value) {
|
|
|
463 |
char name_buf[RSTRING_LEN(field_name) + 1];
|
|
|
464 |
|
|
|
465 |
name_buf[0] = '@';
|
|
|
466 |
strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf));
|
|
|
467 |
|
|
|
468 |
rb_ivar_set(obj, rb_intern(name_buf), value);
|
|
|
469 |
}
|
|
|
470 |
|
|
|
471 |
static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) {
|
|
|
472 |
VALUE result = Qnil;
|
|
|
473 |
|
|
|
474 |
if (ttype == TTYPE_BOOL) {
|
|
|
475 |
result = mt->read_bool(protocol);
|
|
|
476 |
} else if (ttype == TTYPE_BYTE) {
|
|
|
477 |
result = mt->read_byte(protocol);
|
|
|
478 |
} else if (ttype == TTYPE_I16) {
|
|
|
479 |
result = mt->read_i16(protocol);
|
|
|
480 |
} else if (ttype == TTYPE_I32) {
|
|
|
481 |
result = mt->read_i32(protocol);
|
|
|
482 |
} else if (ttype == TTYPE_I64) {
|
|
|
483 |
result = mt->read_i64(protocol);
|
|
|
484 |
} else if (ttype == TTYPE_STRING) {
|
|
|
485 |
result = mt->read_string(protocol);
|
|
|
486 |
} else if (ttype == TTYPE_DOUBLE) {
|
|
|
487 |
result = mt->read_double(protocol);
|
|
|
488 |
} else if (ttype == TTYPE_STRUCT) {
|
|
|
489 |
VALUE klass = rb_hash_aref(field_info, class_sym);
|
|
|
490 |
result = rb_class_new_instance(0, NULL, klass);
|
|
|
491 |
rb_thrift_struct_read(result, protocol);
|
|
|
492 |
} else if (ttype == TTYPE_MAP) {
|
|
|
493 |
int i;
|
|
|
494 |
|
|
|
495 |
VALUE map_header = mt->read_map_begin(protocol);
|
|
|
496 |
int key_ttype = FIX2INT(rb_ary_entry(map_header, 0));
|
|
|
497 |
int value_ttype = FIX2INT(rb_ary_entry(map_header, 1));
|
|
|
498 |
int num_entries = FIX2INT(rb_ary_entry(map_header, 2));
|
|
|
499 |
|
|
|
500 |
VALUE key_info = rb_hash_aref(field_info, key_sym);
|
|
|
501 |
VALUE value_info = rb_hash_aref(field_info, value_sym);
|
|
|
502 |
|
|
|
503 |
result = rb_hash_new();
|
|
|
504 |
|
|
|
505 |
for (i = 0; i < num_entries; ++i) {
|
|
|
506 |
VALUE key, val;
|
|
|
507 |
|
|
|
508 |
key = read_anything(protocol, key_ttype, key_info);
|
|
|
509 |
val = read_anything(protocol, value_ttype, value_info);
|
|
|
510 |
|
|
|
511 |
rb_hash_aset(result, key, val);
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
mt->read_map_end(protocol);
|
|
|
515 |
} else if (ttype == TTYPE_LIST) {
|
|
|
516 |
int i;
|
|
|
517 |
|
|
|
518 |
VALUE list_header = mt->read_list_begin(protocol);
|
|
|
519 |
int element_ttype = FIX2INT(rb_ary_entry(list_header, 0));
|
|
|
520 |
int num_elements = FIX2INT(rb_ary_entry(list_header, 1));
|
|
|
521 |
result = rb_ary_new2(num_elements);
|
|
|
522 |
|
|
|
523 |
for (i = 0; i < num_elements; ++i) {
|
|
|
524 |
rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym)));
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
|
|
|
528 |
mt->read_list_end(protocol);
|
|
|
529 |
} else if (ttype == TTYPE_SET) {
|
|
|
530 |
VALUE items;
|
|
|
531 |
int i;
|
|
|
532 |
|
|
|
533 |
VALUE set_header = mt->read_set_begin(protocol);
|
|
|
534 |
int element_ttype = FIX2INT(rb_ary_entry(set_header, 0));
|
|
|
535 |
int num_elements = FIX2INT(rb_ary_entry(set_header, 1));
|
|
|
536 |
items = rb_ary_new2(num_elements);
|
|
|
537 |
|
|
|
538 |
for (i = 0; i < num_elements; ++i) {
|
|
|
539 |
rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym)));
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
|
|
|
543 |
mt->read_set_end(protocol);
|
|
|
544 |
|
|
|
545 |
result = rb_class_new_instance(1, &items, rb_cSet);
|
|
|
546 |
} else {
|
|
|
547 |
rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype);
|
|
|
548 |
}
|
|
|
549 |
|
|
|
550 |
return result;
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) {
|
|
|
554 |
// check_native_proto_method_table(protocol);
|
|
|
555 |
|
|
|
556 |
// read struct begin
|
|
|
557 |
mt->read_struct_begin(protocol);
|
|
|
558 |
|
|
|
559 |
VALUE struct_fields = STRUCT_FIELDS(self);
|
|
|
560 |
|
|
|
561 |
// read each field
|
|
|
562 |
while (true) {
|
|
|
563 |
VALUE field_header = mt->read_field_begin(protocol);
|
|
|
564 |
VALUE field_type_value = rb_ary_entry(field_header, 1);
|
|
|
565 |
int field_type = FIX2INT(field_type_value);
|
|
|
566 |
|
|
|
567 |
if (field_type == TTYPE_STOP) {
|
|
|
568 |
break;
|
|
|
569 |
}
|
|
|
570 |
|
|
|
571 |
// make sure we got a type we expected
|
|
|
572 |
VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2));
|
|
|
573 |
|
|
|
574 |
if (!NIL_P(field_info)) {
|
|
|
575 |
int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym));
|
|
|
576 |
if (field_type == specified_type) {
|
|
|
577 |
// read the value
|
|
|
578 |
VALUE name = rb_hash_aref(field_info, name_sym);
|
|
|
579 |
set_field_value(self, name, read_anything(protocol, field_type, field_info));
|
|
|
580 |
} else {
|
|
|
581 |
rb_funcall(protocol, skip_method_id, 1, field_type_value);
|
|
|
582 |
}
|
|
|
583 |
} else {
|
|
|
584 |
rb_funcall(protocol, skip_method_id, 1, field_type_value);
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
// read field end
|
|
|
588 |
mt->read_field_end(protocol);
|
|
|
589 |
}
|
|
|
590 |
|
|
|
591 |
// read struct end
|
|
|
592 |
mt->read_struct_end(protocol);
|
|
|
593 |
|
|
|
594 |
// call validate
|
|
|
595 |
rb_funcall(self, validate_method_id, 0);
|
|
|
596 |
|
|
|
597 |
return Qnil;
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
void Init_struct() {
|
|
|
601 |
VALUE struct_module = rb_const_get(thrift_module, rb_intern("Struct"));
|
|
|
602 |
|
|
|
603 |
rb_define_method(struct_module, "write", rb_thrift_struct_write, 1);
|
|
|
604 |
rb_define_method(struct_module, "read", rb_thrift_struct_read, 1);
|
|
|
605 |
|
|
|
606 |
set_default_proto_function_pointers();
|
|
|
607 |
mt = default_mt;
|
|
|
608 |
}
|
|
|
609 |
|