Kevin Clark | 916f353 | 2009-03-20 04:21:39 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 20 | #include <struct.h> |
| 21 | #include <constants.h> |
Bryan Duxbury | 6b771d2 | 2009-03-26 04:55:34 +0000 | [diff] [blame^] | 22 | #include "macros.h" |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 23 | |
Bryan Duxbury | 1e80d44 | 2009-02-03 18:16:54 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 46 | static native_proto_method_table *mt; |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 47 | static native_proto_method_table *default_mt; |
| 48 | static VALUE last_proto_class = Qnil; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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() { |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 236 | default_mt = ALLOC(native_proto_method_table); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 237 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 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; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 256 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 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; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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) { |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 283 | char name_buf[RSTRING_LEN(field_name) + 1]; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 284 | |
| 285 | name_buf[0] = '@'; |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 286 | strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf)); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 313 | sz = RARRAY_LEN(keys); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 338 | sz = RARRAY_LEN(value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 368 | sz = RARRAY_LEN(items); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 419 | // if (rb_funcall(protocol, native_qmark_method_id, 0) == Qtrue) { |
| 420 | // set_native_proto_function_pointers(protocol); |
| 421 | // } else { |
| 422 | // set_default_proto_function_pointers(); |
| 423 | // } |
| 424 | check_native_proto_method_table(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 425 | |
| 426 | // write struct begin |
| 427 | mt->write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); |
| 428 | |
| 429 | // iterate through all the fields here |
| 430 | VALUE struct_fields = STRUCT_FIELDS(self); |
| 431 | VALUE struct_field_ids_unordered = rb_funcall(struct_fields, keys_method_id, 0); |
| 432 | VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0); |
| 433 | |
| 434 | int i = 0; |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 435 | for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) { |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 436 | VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i); |
| 437 | VALUE field_info = rb_hash_aref(struct_fields, field_id); |
| 438 | |
| 439 | VALUE ttype_value = rb_hash_aref(field_info, type_sym); |
| 440 | int ttype = FIX2INT(ttype_value); |
| 441 | VALUE field_name = rb_hash_aref(field_info, name_sym); |
| 442 | VALUE field_value = get_field_value(self, field_name); |
| 443 | |
| 444 | if (!NIL_P(field_value)) { |
| 445 | mt->write_field_begin(protocol, field_name, ttype_value, field_id); |
| 446 | |
| 447 | write_anything(ttype, field_value, protocol, field_info); |
| 448 | |
| 449 | mt->write_field_end(protocol); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | mt->write_field_stop(protocol); |
| 454 | |
| 455 | // write struct end |
| 456 | mt->write_struct_end(protocol); |
| 457 | |
| 458 | return Qnil; |
| 459 | } |
| 460 | |
| 461 | //------------------------------------------- |
| 462 | // Reading section |
| 463 | //------------------------------------------- |
| 464 | |
| 465 | static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol); |
| 466 | |
| 467 | static void set_field_value(VALUE obj, VALUE field_name, VALUE value) { |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 468 | char name_buf[RSTRING_LEN(field_name) + 1]; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 469 | |
| 470 | name_buf[0] = '@'; |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 471 | strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf)); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 472 | |
| 473 | rb_ivar_set(obj, rb_intern(name_buf), value); |
| 474 | } |
| 475 | |
| 476 | static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { |
| 477 | VALUE result = Qnil; |
| 478 | |
| 479 | if (ttype == TTYPE_BOOL) { |
| 480 | result = mt->read_bool(protocol); |
| 481 | } else if (ttype == TTYPE_BYTE) { |
| 482 | result = mt->read_byte(protocol); |
| 483 | } else if (ttype == TTYPE_I16) { |
| 484 | result = mt->read_i16(protocol); |
| 485 | } else if (ttype == TTYPE_I32) { |
| 486 | result = mt->read_i32(protocol); |
| 487 | } else if (ttype == TTYPE_I64) { |
| 488 | result = mt->read_i64(protocol); |
| 489 | } else if (ttype == TTYPE_STRING) { |
| 490 | result = mt->read_string(protocol); |
| 491 | } else if (ttype == TTYPE_DOUBLE) { |
| 492 | result = mt->read_double(protocol); |
| 493 | } else if (ttype == TTYPE_STRUCT) { |
| 494 | VALUE klass = rb_hash_aref(field_info, class_sym); |
| 495 | result = rb_class_new_instance(0, NULL, klass); |
| 496 | rb_thrift_struct_read(result, protocol); |
| 497 | } else if (ttype == TTYPE_MAP) { |
| 498 | int i; |
| 499 | |
| 500 | VALUE map_header = mt->read_map_begin(protocol); |
| 501 | int key_ttype = FIX2INT(rb_ary_entry(map_header, 0)); |
| 502 | int value_ttype = FIX2INT(rb_ary_entry(map_header, 1)); |
| 503 | int num_entries = FIX2INT(rb_ary_entry(map_header, 2)); |
| 504 | |
| 505 | VALUE key_info = rb_hash_aref(field_info, key_sym); |
| 506 | VALUE value_info = rb_hash_aref(field_info, value_sym); |
| 507 | |
| 508 | result = rb_hash_new(); |
| 509 | |
| 510 | for (i = 0; i < num_entries; ++i) { |
| 511 | VALUE key, val; |
| 512 | |
| 513 | key = read_anything(protocol, key_ttype, key_info); |
| 514 | val = read_anything(protocol, value_ttype, value_info); |
| 515 | |
| 516 | rb_hash_aset(result, key, val); |
| 517 | } |
| 518 | |
| 519 | mt->read_map_end(protocol); |
| 520 | } else if (ttype == TTYPE_LIST) { |
| 521 | int i; |
| 522 | |
| 523 | VALUE list_header = mt->read_list_begin(protocol); |
| 524 | int element_ttype = FIX2INT(rb_ary_entry(list_header, 0)); |
| 525 | int num_elements = FIX2INT(rb_ary_entry(list_header, 1)); |
| 526 | result = rb_ary_new2(num_elements); |
| 527 | |
| 528 | for (i = 0; i < num_elements; ++i) { |
| 529 | rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | mt->read_list_end(protocol); |
| 534 | } else if (ttype == TTYPE_SET) { |
| 535 | VALUE items; |
| 536 | int i; |
| 537 | |
| 538 | VALUE set_header = mt->read_set_begin(protocol); |
| 539 | int element_ttype = FIX2INT(rb_ary_entry(set_header, 0)); |
| 540 | int num_elements = FIX2INT(rb_ary_entry(set_header, 1)); |
| 541 | items = rb_ary_new2(num_elements); |
| 542 | |
| 543 | for (i = 0; i < num_elements; ++i) { |
| 544 | rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | mt->read_set_end(protocol); |
| 549 | |
| 550 | result = rb_class_new_instance(1, &items, rb_cSet); |
| 551 | } else { |
| 552 | rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype); |
| 553 | } |
| 554 | |
| 555 | return result; |
| 556 | } |
| 557 | |
| 558 | static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) { |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 559 | check_native_proto_method_table(protocol); |
| 560 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 561 | // read struct begin |
| 562 | mt->read_struct_begin(protocol); |
| 563 | |
| 564 | VALUE struct_fields = STRUCT_FIELDS(self); |
| 565 | |
| 566 | // read each field |
| 567 | while (true) { |
| 568 | VALUE field_header = rb_funcall(protocol, read_field_begin_method_id, 0); |
| 569 | VALUE field_type_value = rb_ary_entry(field_header, 1); |
| 570 | int field_type = FIX2INT(field_type_value); |
| 571 | |
| 572 | if (field_type == TTYPE_STOP) { |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | // make sure we got a type we expected |
| 577 | VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2)); |
| 578 | |
| 579 | if (!NIL_P(field_info)) { |
| 580 | int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym)); |
| 581 | if (field_type == specified_type) { |
| 582 | // read the value |
| 583 | VALUE name = rb_hash_aref(field_info, name_sym); |
| 584 | set_field_value(self, name, read_anything(protocol, field_type, field_info)); |
| 585 | } else { |
| 586 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 587 | } |
| 588 | } else { |
| 589 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 590 | } |
| 591 | |
| 592 | // read field end |
| 593 | mt->read_field_end(protocol); |
| 594 | } |
| 595 | |
| 596 | // read struct end |
| 597 | mt->read_struct_end(protocol); |
| 598 | |
| 599 | return Qnil; |
| 600 | } |
| 601 | |
| 602 | void Init_struct() { |
| 603 | VALUE struct_module = rb_const_get(thrift_module, rb_intern("Struct")); |
| 604 | |
| 605 | rb_define_method(struct_module, "write", rb_thrift_struct_write, 1); |
| 606 | rb_define_method(struct_module, "read", rb_thrift_struct_read, 1); |
| 607 | |
| 608 | set_default_proto_function_pointers(); |
| 609 | } |
| 610 | |