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 | 09d13c2 | 2010-08-11 18:37:25 +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 | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 24 | VALUE thrift_union_class; |
| 25 | |
| 26 | ID setfield_id; |
| 27 | ID setvalue_id; |
| 28 | |
| 29 | ID to_s_method_id; |
| 30 | ID name_to_id_method_id; |
Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 31 | static ID sorted_field_ids_method_id; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 32 | |
| 33 | #define IS_CONTAINER(ttype) ((ttype) == TTYPE_MAP || (ttype) == TTYPE_LIST || (ttype) == TTYPE_SET) |
| 34 | #define STRUCT_FIELDS(obj) rb_const_get(CLASS_OF(obj), fields_const_id) |
| 35 | |
| 36 | //------------------------------------------- |
| 37 | // Writing section |
| 38 | //------------------------------------------- |
| 39 | |
| 40 | // default fn pointers for protocol stuff here |
| 41 | |
| 42 | VALUE default_write_bool(VALUE protocol, VALUE value) { |
| 43 | rb_funcall(protocol, write_boolean_method_id, 1, value); |
| 44 | return Qnil; |
| 45 | } |
| 46 | |
| 47 | VALUE default_write_byte(VALUE protocol, VALUE value) { |
| 48 | rb_funcall(protocol, write_byte_method_id, 1, value); |
| 49 | return Qnil; |
| 50 | } |
| 51 | |
| 52 | VALUE default_write_i16(VALUE protocol, VALUE value) { |
| 53 | rb_funcall(protocol, write_i16_method_id, 1, value); |
| 54 | return Qnil; |
| 55 | } |
| 56 | |
| 57 | VALUE default_write_i32(VALUE protocol, VALUE value) { |
| 58 | rb_funcall(protocol, write_i32_method_id, 1, value); |
| 59 | return Qnil; |
| 60 | } |
| 61 | |
| 62 | VALUE default_write_i64(VALUE protocol, VALUE value) { |
| 63 | rb_funcall(protocol, write_i64_method_id, 1, value); |
| 64 | return Qnil; |
| 65 | } |
| 66 | |
| 67 | VALUE default_write_double(VALUE protocol, VALUE value) { |
| 68 | rb_funcall(protocol, write_double_method_id, 1, value); |
| 69 | return Qnil; |
| 70 | } |
| 71 | |
| 72 | VALUE default_write_string(VALUE protocol, VALUE value) { |
| 73 | rb_funcall(protocol, write_string_method_id, 1, value); |
| 74 | return Qnil; |
| 75 | } |
| 76 | |
| 77 | VALUE default_write_list_begin(VALUE protocol, VALUE etype, VALUE length) { |
| 78 | rb_funcall(protocol, write_list_begin_method_id, 2, etype, length); |
| 79 | return Qnil; |
| 80 | } |
| 81 | |
| 82 | VALUE default_write_list_end(VALUE protocol) { |
| 83 | rb_funcall(protocol, write_list_end_method_id, 0); |
| 84 | return Qnil; |
| 85 | } |
| 86 | |
| 87 | VALUE default_write_set_begin(VALUE protocol, VALUE etype, VALUE length) { |
| 88 | rb_funcall(protocol, write_set_begin_method_id, 2, etype, length); |
| 89 | return Qnil; |
| 90 | } |
| 91 | |
| 92 | VALUE default_write_set_end(VALUE protocol) { |
| 93 | rb_funcall(protocol, write_set_end_method_id, 0); |
| 94 | return Qnil; |
| 95 | } |
| 96 | |
| 97 | VALUE default_write_map_begin(VALUE protocol, VALUE ktype, VALUE vtype, VALUE length) { |
| 98 | rb_funcall(protocol, write_map_begin_method_id, 3, ktype, vtype, length); |
| 99 | return Qnil; |
| 100 | } |
| 101 | |
| 102 | VALUE default_write_map_end(VALUE protocol) { |
| 103 | rb_funcall(protocol, write_map_end_method_id, 0); |
| 104 | return Qnil; |
| 105 | } |
| 106 | |
| 107 | VALUE default_write_struct_begin(VALUE protocol, VALUE struct_name) { |
| 108 | rb_funcall(protocol, write_struct_begin_method_id, 1, struct_name); |
| 109 | return Qnil; |
| 110 | } |
| 111 | |
| 112 | VALUE default_write_struct_end(VALUE protocol) { |
| 113 | rb_funcall(protocol, write_struct_end_method_id, 0); |
| 114 | return Qnil; |
| 115 | } |
| 116 | |
| 117 | VALUE default_write_field_begin(VALUE protocol, VALUE name, VALUE type, VALUE id) { |
| 118 | rb_funcall(protocol, write_field_begin_method_id, 3, name, type, id); |
| 119 | return Qnil; |
| 120 | } |
| 121 | |
| 122 | VALUE default_write_field_end(VALUE protocol) { |
| 123 | rb_funcall(protocol, write_field_end_method_id, 0); |
| 124 | return Qnil; |
| 125 | } |
| 126 | |
| 127 | VALUE default_write_field_stop(VALUE protocol) { |
| 128 | rb_funcall(protocol, write_field_stop_method_id, 0); |
| 129 | return Qnil; |
| 130 | } |
| 131 | |
| 132 | VALUE default_read_field_begin(VALUE protocol) { |
| 133 | return rb_funcall(protocol, read_field_begin_method_id, 0); |
| 134 | } |
| 135 | |
| 136 | VALUE default_read_field_end(VALUE protocol) { |
| 137 | return rb_funcall(protocol, read_field_end_method_id, 0); |
| 138 | } |
| 139 | |
| 140 | VALUE default_read_map_begin(VALUE protocol) { |
| 141 | return rb_funcall(protocol, read_map_begin_method_id, 0); |
| 142 | } |
| 143 | |
| 144 | VALUE default_read_map_end(VALUE protocol) { |
| 145 | return rb_funcall(protocol, read_map_end_method_id, 0); |
| 146 | } |
| 147 | |
| 148 | VALUE default_read_list_begin(VALUE protocol) { |
| 149 | return rb_funcall(protocol, read_list_begin_method_id, 0); |
| 150 | } |
| 151 | |
| 152 | VALUE default_read_list_end(VALUE protocol) { |
| 153 | return rb_funcall(protocol, read_list_end_method_id, 0); |
| 154 | } |
| 155 | |
| 156 | VALUE default_read_set_begin(VALUE protocol) { |
| 157 | return rb_funcall(protocol, read_set_begin_method_id, 0); |
| 158 | } |
| 159 | |
| 160 | VALUE default_read_set_end(VALUE protocol) { |
| 161 | return rb_funcall(protocol, read_set_end_method_id, 0); |
| 162 | } |
| 163 | |
| 164 | VALUE default_read_byte(VALUE protocol) { |
| 165 | return rb_funcall(protocol, read_byte_method_id, 0); |
| 166 | } |
| 167 | |
| 168 | VALUE default_read_bool(VALUE protocol) { |
| 169 | return rb_funcall(protocol, read_bool_method_id, 0); |
| 170 | } |
| 171 | |
| 172 | VALUE default_read_i16(VALUE protocol) { |
| 173 | return rb_funcall(protocol, read_i16_method_id, 0); |
| 174 | } |
| 175 | |
| 176 | VALUE default_read_i32(VALUE protocol) { |
| 177 | return rb_funcall(protocol, read_i32_method_id, 0); |
| 178 | } |
| 179 | |
| 180 | VALUE default_read_i64(VALUE protocol) { |
| 181 | return rb_funcall(protocol, read_i64_method_id, 0); |
| 182 | } |
| 183 | |
| 184 | VALUE default_read_double(VALUE protocol) { |
| 185 | return rb_funcall(protocol, read_double_method_id, 0); |
| 186 | } |
| 187 | |
| 188 | VALUE default_read_string(VALUE protocol) { |
| 189 | return rb_funcall(protocol, read_string_method_id, 0); |
| 190 | } |
| 191 | |
| 192 | VALUE default_read_struct_begin(VALUE protocol) { |
| 193 | return rb_funcall(protocol, read_struct_begin_method_id, 0); |
| 194 | } |
| 195 | |
| 196 | VALUE default_read_struct_end(VALUE protocol) { |
| 197 | return rb_funcall(protocol, read_struct_end_method_id, 0); |
| 198 | } |
| 199 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 200 | // end default protocol methods |
| 201 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 202 | static VALUE rb_thrift_union_write (VALUE self, VALUE protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 203 | static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol); |
| 204 | static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info); |
| 205 | |
| 206 | VALUE get_field_value(VALUE obj, VALUE field_name) { |
Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 207 | char name_buf[RSTRING_LEN(field_name) + 2]; |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 208 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 209 | name_buf[0] = '@'; |
Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 210 | strlcpy(&name_buf[1], RSTRING_PTR(field_name), RSTRING_LEN(field_name) + 1); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 211 | |
| 212 | VALUE value = rb_ivar_get(obj, rb_intern(name_buf)); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 213 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 214 | return value; |
| 215 | } |
| 216 | |
| 217 | static void write_container(int ttype, VALUE field_info, VALUE value, VALUE protocol) { |
| 218 | int sz, i; |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 219 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 220 | if (ttype == TTYPE_MAP) { |
| 221 | VALUE keys; |
| 222 | VALUE key; |
| 223 | VALUE val; |
| 224 | |
| 225 | Check_Type(value, T_HASH); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 226 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 227 | VALUE key_info = rb_hash_aref(field_info, key_sym); |
| 228 | VALUE keytype_value = rb_hash_aref(key_info, type_sym); |
| 229 | int keytype = FIX2INT(keytype_value); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 230 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 231 | VALUE value_info = rb_hash_aref(field_info, value_sym); |
| 232 | VALUE valuetype_value = rb_hash_aref(value_info, type_sym); |
| 233 | int valuetype = FIX2INT(valuetype_value); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 234 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 235 | keys = rb_funcall(value, keys_method_id, 0); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 236 | |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 237 | sz = RARRAY_LEN(keys); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 238 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 239 | default_write_map_begin(protocol, keytype_value, valuetype_value, INT2FIX(sz)); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 240 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 241 | for (i = 0; i < sz; i++) { |
| 242 | key = rb_ary_entry(keys, i); |
| 243 | val = rb_hash_aref(value, key); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 244 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 245 | if (IS_CONTAINER(keytype)) { |
| 246 | write_container(keytype, key_info, key, protocol); |
| 247 | } else { |
| 248 | write_anything(keytype, key, protocol, key_info); |
| 249 | } |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 250 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 251 | if (IS_CONTAINER(valuetype)) { |
| 252 | write_container(valuetype, value_info, val, protocol); |
| 253 | } else { |
| 254 | write_anything(valuetype, val, protocol, value_info); |
| 255 | } |
| 256 | } |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 257 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 258 | default_write_map_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 259 | } else if (ttype == TTYPE_LIST) { |
| 260 | Check_Type(value, T_ARRAY); |
| 261 | |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 262 | sz = RARRAY_LEN(value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 263 | |
| 264 | VALUE element_type_info = rb_hash_aref(field_info, element_sym); |
| 265 | VALUE element_type_value = rb_hash_aref(element_type_info, type_sym); |
| 266 | int element_type = FIX2INT(element_type_value); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 267 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 268 | default_write_list_begin(protocol, element_type_value, INT2FIX(sz)); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 269 | for (i = 0; i < sz; ++i) { |
| 270 | VALUE val = rb_ary_entry(value, i); |
| 271 | if (IS_CONTAINER(element_type)) { |
| 272 | write_container(element_type, element_type_info, val, protocol); |
| 273 | } else { |
| 274 | write_anything(element_type, val, protocol, element_type_info); |
| 275 | } |
| 276 | } |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 277 | default_write_list_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 278 | } else if (ttype == TTYPE_SET) { |
| 279 | VALUE items; |
| 280 | |
| 281 | if (TYPE(value) == T_ARRAY) { |
| 282 | items = value; |
| 283 | } else { |
| 284 | if (rb_cSet == CLASS_OF(value)) { |
| 285 | items = rb_funcall(value, entries_method_id, 0); |
| 286 | } else { |
| 287 | Check_Type(value, T_HASH); |
| 288 | items = rb_funcall(value, keys_method_id, 0); |
| 289 | } |
| 290 | } |
| 291 | |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 292 | sz = RARRAY_LEN(items); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 293 | |
| 294 | VALUE element_type_info = rb_hash_aref(field_info, element_sym); |
| 295 | VALUE element_type_value = rb_hash_aref(element_type_info, type_sym); |
| 296 | int element_type = FIX2INT(element_type_value); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 297 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 298 | default_write_set_begin(protocol, element_type_value, INT2FIX(sz)); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 299 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 300 | for (i = 0; i < sz; i++) { |
| 301 | VALUE val = rb_ary_entry(items, i); |
| 302 | if (IS_CONTAINER(element_type)) { |
| 303 | write_container(element_type, element_type_info, val, protocol); |
| 304 | } else { |
| 305 | write_anything(element_type, val, protocol, element_type_info); |
| 306 | } |
| 307 | } |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 308 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 309 | default_write_set_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 310 | } else { |
| 311 | rb_raise(rb_eNotImpError, "can't write container of type: %d", ttype); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info) { |
| 316 | if (ttype == TTYPE_BOOL) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 317 | default_write_bool(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 318 | } else if (ttype == TTYPE_BYTE) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 319 | default_write_byte(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 320 | } else if (ttype == TTYPE_I16) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 321 | default_write_i16(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 322 | } else if (ttype == TTYPE_I32) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 323 | default_write_i32(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 324 | } else if (ttype == TTYPE_I64) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 325 | default_write_i64(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 326 | } else if (ttype == TTYPE_DOUBLE) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 327 | default_write_double(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 328 | } else if (ttype == TTYPE_STRING) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 329 | default_write_string(protocol, value); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 330 | } else if (IS_CONTAINER(ttype)) { |
| 331 | write_container(ttype, field_info, value, protocol); |
| 332 | } else if (ttype == TTYPE_STRUCT) { |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 333 | if (rb_obj_is_kind_of(value, thrift_union_class)) { |
| 334 | rb_thrift_union_write(value, protocol); |
| 335 | } else { |
| 336 | rb_thrift_struct_write(value, protocol); |
| 337 | } |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 338 | } else { |
| 339 | rb_raise(rb_eNotImpError, "Unknown type for binary_encoding: %d", ttype); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) { |
| 344 | // call validate |
| 345 | rb_funcall(self, validate_method_id, 0); |
| 346 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 347 | // write struct begin |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 348 | default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 349 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 350 | // iterate through all the fields here |
| 351 | VALUE struct_fields = STRUCT_FIELDS(self); |
Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 352 | VALUE sorted_field_ids = rb_funcall(self, sorted_field_ids_method_id, 0); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 353 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 354 | int i = 0; |
Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 355 | for (i=0; i < RARRAY_LEN(sorted_field_ids); i++) { |
| 356 | VALUE field_id = rb_ary_entry(sorted_field_ids, i); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 357 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 358 | VALUE field_info = rb_hash_aref(struct_fields, field_id); |
| 359 | |
| 360 | VALUE ttype_value = rb_hash_aref(field_info, type_sym); |
| 361 | int ttype = FIX2INT(ttype_value); |
| 362 | VALUE field_name = rb_hash_aref(field_info, name_sym); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 363 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 364 | VALUE field_value = get_field_value(self, field_name); |
| 365 | |
| 366 | if (!NIL_P(field_value)) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 367 | default_write_field_begin(protocol, field_name, ttype_value, field_id); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 368 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 369 | write_anything(ttype, field_value, protocol, field_info); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 370 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 371 | default_write_field_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 374 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 375 | default_write_field_stop(protocol); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 376 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 377 | // write struct end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 378 | default_write_struct_end(protocol); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 379 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 380 | return Qnil; |
| 381 | } |
| 382 | |
| 383 | //------------------------------------------- |
| 384 | // Reading section |
| 385 | //------------------------------------------- |
| 386 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 387 | static VALUE rb_thrift_union_read(VALUE self, VALUE protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 388 | static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol); |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 389 | static void skip_map_contents(VALUE protocol, VALUE key_type_value, VALUE value_type_value, int size); |
| 390 | static void skip_list_or_set_contents(VALUE protocol, VALUE element_type_value, int size); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 391 | |
| 392 | static void set_field_value(VALUE obj, VALUE field_name, VALUE value) { |
Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 393 | char name_buf[RSTRING_LEN(field_name) + 2]; |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 394 | |
| 395 | name_buf[0] = '@'; |
Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 396 | strlcpy(&name_buf[1], RSTRING_PTR(field_name), RSTRING_LEN(field_name)+1); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 397 | |
| 398 | rb_ivar_set(obj, rb_intern(name_buf), value); |
| 399 | } |
| 400 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 401 | // Helper method to skip the contents of a map (assumes the map header has been read). |
| 402 | static void skip_map_contents(VALUE protocol, VALUE key_type_value, VALUE value_type_value, int size) { |
| 403 | int i; |
| 404 | for (i = 0; i < size; i++) { |
| 405 | rb_funcall(protocol, skip_method_id, 1, key_type_value); |
| 406 | rb_funcall(protocol, skip_method_id, 1, value_type_value); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Helper method to skip the contents of a list or set (assumes the list/set header has been read). |
| 411 | static void skip_list_or_set_contents(VALUE protocol, VALUE element_type_value, int size) { |
| 412 | int i; |
| 413 | for (i = 0; i < size; i++) { |
| 414 | rb_funcall(protocol, skip_method_id, 1, element_type_value); |
| 415 | } |
| 416 | } |
| 417 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 418 | static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { |
| 419 | VALUE result = Qnil; |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 420 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 421 | if (ttype == TTYPE_BOOL) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 422 | result = default_read_bool(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 423 | } else if (ttype == TTYPE_BYTE) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 424 | result = default_read_byte(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 425 | } else if (ttype == TTYPE_I16) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 426 | result = default_read_i16(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 427 | } else if (ttype == TTYPE_I32) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 428 | result = default_read_i32(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 429 | } else if (ttype == TTYPE_I64) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 430 | result = default_read_i64(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 431 | } else if (ttype == TTYPE_STRING) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 432 | result = default_read_string(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 433 | } else if (ttype == TTYPE_DOUBLE) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 434 | result = default_read_double(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 435 | } else if (ttype == TTYPE_STRUCT) { |
| 436 | VALUE klass = rb_hash_aref(field_info, class_sym); |
| 437 | result = rb_class_new_instance(0, NULL, klass); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 438 | |
| 439 | if (rb_obj_is_kind_of(result, thrift_union_class)) { |
| 440 | rb_thrift_union_read(result, protocol); |
| 441 | } else { |
| 442 | rb_thrift_struct_read(result, protocol); |
| 443 | } |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 444 | } else if (ttype == TTYPE_MAP) { |
| 445 | int i; |
| 446 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 447 | VALUE map_header = default_read_map_begin(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 448 | int key_ttype = FIX2INT(rb_ary_entry(map_header, 0)); |
| 449 | int value_ttype = FIX2INT(rb_ary_entry(map_header, 1)); |
| 450 | int num_entries = FIX2INT(rb_ary_entry(map_header, 2)); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 451 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 452 | // Check the declared key and value types against the expected ones and skip the map contents |
| 453 | // if the types don't match. |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 454 | VALUE key_info = rb_hash_aref(field_info, key_sym); |
| 455 | VALUE value_info = rb_hash_aref(field_info, value_sym); |
| 456 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 457 | if (!NIL_P(key_info) && !NIL_P(value_info)) { |
| 458 | int specified_key_type = FIX2INT(rb_hash_aref(key_info, type_sym)); |
| 459 | int specified_value_type = FIX2INT(rb_hash_aref(value_info, type_sym)); |
Bryan Duxbury | e80a194 | 2011-09-20 18:45:56 +0000 | [diff] [blame] | 460 | if (num_entries == 0 || (specified_key_type == key_ttype && specified_value_type == value_ttype)) { |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 461 | result = rb_hash_new(); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 462 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 463 | for (i = 0; i < num_entries; ++i) { |
| 464 | VALUE key, val; |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 465 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 466 | key = read_anything(protocol, key_ttype, key_info); |
| 467 | val = read_anything(protocol, value_ttype, value_info); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 468 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 469 | rb_hash_aset(result, key, val); |
| 470 | } |
| 471 | } else { |
| 472 | skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries); |
| 473 | } |
| 474 | } else { |
| 475 | skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 476 | } |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 477 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 478 | default_read_map_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 479 | } else if (ttype == TTYPE_LIST) { |
| 480 | int i; |
| 481 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 482 | VALUE list_header = default_read_list_begin(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 483 | int element_ttype = FIX2INT(rb_ary_entry(list_header, 0)); |
| 484 | int num_elements = FIX2INT(rb_ary_entry(list_header, 1)); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 485 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 486 | // Check the declared element type against the expected one and skip the list contents |
| 487 | // if the types don't match. |
| 488 | VALUE element_info = rb_hash_aref(field_info, element_sym); |
| 489 | if (!NIL_P(element_info)) { |
| 490 | int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym)); |
| 491 | if (specified_element_type == element_ttype) { |
| 492 | result = rb_ary_new2(num_elements); |
| 493 | |
| 494 | for (i = 0; i < num_elements; ++i) { |
| 495 | rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); |
| 496 | } |
| 497 | } else { |
| 498 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
| 499 | } |
| 500 | } else { |
| 501 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 504 | default_read_list_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 505 | } else if (ttype == TTYPE_SET) { |
| 506 | VALUE items; |
| 507 | int i; |
| 508 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 509 | VALUE set_header = default_read_set_begin(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 510 | int element_ttype = FIX2INT(rb_ary_entry(set_header, 0)); |
| 511 | int num_elements = FIX2INT(rb_ary_entry(set_header, 1)); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 512 | |
Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 513 | // Check the declared element type against the expected one and skip the set contents |
| 514 | // if the types don't match. |
| 515 | VALUE element_info = rb_hash_aref(field_info, element_sym); |
| 516 | if (!NIL_P(element_info)) { |
| 517 | int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym)); |
| 518 | if (specified_element_type == element_ttype) { |
| 519 | items = rb_ary_new2(num_elements); |
| 520 | |
| 521 | for (i = 0; i < num_elements; ++i) { |
| 522 | rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); |
| 523 | } |
| 524 | |
| 525 | result = rb_class_new_instance(1, &items, rb_cSet); |
| 526 | } else { |
| 527 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
| 528 | } |
| 529 | } else { |
| 530 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 531 | } |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 532 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 533 | default_read_set_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 534 | } else { |
| 535 | rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype); |
| 536 | } |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 537 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 538 | return result; |
| 539 | } |
| 540 | |
| 541 | static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) { |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 542 | // read struct begin |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 543 | default_read_struct_begin(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 544 | |
| 545 | VALUE struct_fields = STRUCT_FIELDS(self); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 546 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 547 | // read each field |
| 548 | while (true) { |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 549 | VALUE field_header = default_read_field_begin(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 550 | VALUE field_type_value = rb_ary_entry(field_header, 1); |
| 551 | int field_type = FIX2INT(field_type_value); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 552 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 553 | if (field_type == TTYPE_STOP) { |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | // make sure we got a type we expected |
| 558 | VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2)); |
| 559 | |
| 560 | if (!NIL_P(field_info)) { |
| 561 | int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym)); |
| 562 | if (field_type == specified_type) { |
| 563 | // read the value |
| 564 | VALUE name = rb_hash_aref(field_info, name_sym); |
| 565 | set_field_value(self, name, read_anything(protocol, field_type, field_info)); |
| 566 | } else { |
| 567 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 568 | } |
| 569 | } else { |
| 570 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 571 | } |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 572 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 573 | // read field end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 574 | default_read_field_end(protocol); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 575 | } |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 576 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 577 | // read struct end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 578 | default_read_struct_end(protocol); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 579 | |
Bryan Duxbury | 834895d | 2009-10-15 01:20:34 +0000 | [diff] [blame] | 580 | // call validate |
| 581 | rb_funcall(self, validate_method_id, 0); |
| 582 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 583 | return Qnil; |
| 584 | } |
| 585 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 586 | |
| 587 | // -------------------------------- |
| 588 | // Union section |
| 589 | // -------------------------------- |
| 590 | |
| 591 | static VALUE rb_thrift_union_read(VALUE self, VALUE protocol) { |
| 592 | // read struct begin |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 593 | default_read_struct_begin(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 594 | |
| 595 | VALUE struct_fields = STRUCT_FIELDS(self); |
| 596 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 597 | VALUE field_header = default_read_field_begin(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 598 | VALUE field_type_value = rb_ary_entry(field_header, 1); |
| 599 | int field_type = FIX2INT(field_type_value); |
| 600 | |
| 601 | // make sure we got a type we expected |
| 602 | VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2)); |
| 603 | |
| 604 | if (!NIL_P(field_info)) { |
| 605 | int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym)); |
| 606 | if (field_type == specified_type) { |
| 607 | // read the value |
| 608 | VALUE name = rb_hash_aref(field_info, name_sym); |
| 609 | rb_iv_set(self, "@setfield", ID2SYM(rb_intern(RSTRING_PTR(name)))); |
| 610 | rb_iv_set(self, "@value", read_anything(protocol, field_type, field_info)); |
| 611 | } else { |
| 612 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 613 | } |
| 614 | } else { |
| 615 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 616 | } |
| 617 | |
| 618 | // read field end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 619 | default_read_field_end(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 620 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 621 | field_header = default_read_field_begin(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 622 | field_type_value = rb_ary_entry(field_header, 1); |
| 623 | field_type = FIX2INT(field_type_value); |
| 624 | |
| 625 | if (field_type != TTYPE_STOP) { |
| 626 | rb_raise(rb_eRuntimeError, "too many fields in union!"); |
| 627 | } |
| 628 | |
| 629 | // read field end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 630 | default_read_field_end(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 631 | |
| 632 | // read struct end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 633 | default_read_struct_end(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 634 | |
| 635 | // call validate |
| 636 | rb_funcall(self, validate_method_id, 0); |
| 637 | |
| 638 | return Qnil; |
| 639 | } |
| 640 | |
| 641 | static VALUE rb_thrift_union_write(VALUE self, VALUE protocol) { |
| 642 | // call validate |
| 643 | rb_funcall(self, validate_method_id, 0); |
| 644 | |
| 645 | // write struct begin |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 646 | default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 647 | |
| 648 | VALUE struct_fields = STRUCT_FIELDS(self); |
| 649 | |
| 650 | VALUE setfield = rb_ivar_get(self, setfield_id); |
| 651 | VALUE setvalue = rb_ivar_get(self, setvalue_id); |
| 652 | VALUE field_id = rb_funcall(self, name_to_id_method_id, 1, rb_funcall(setfield, to_s_method_id, 0)); |
| 653 | |
| 654 | VALUE field_info = rb_hash_aref(struct_fields, field_id); |
| 655 | |
| 656 | VALUE ttype_value = rb_hash_aref(field_info, type_sym); |
| 657 | int ttype = FIX2INT(ttype_value); |
| 658 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 659 | default_write_field_begin(protocol, setfield, ttype_value, field_id); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 660 | |
| 661 | write_anything(ttype, setvalue, protocol, field_info); |
| 662 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 663 | default_write_field_end(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 664 | |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 665 | default_write_field_stop(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 666 | |
| 667 | // write struct end |
Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 668 | default_write_struct_end(protocol); |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 669 | |
| 670 | return Qnil; |
| 671 | } |
| 672 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 673 | void Init_struct() { |
| 674 | VALUE struct_module = rb_const_get(thrift_module, rb_intern("Struct")); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 675 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 676 | rb_define_method(struct_module, "write", rb_thrift_struct_write, 1); |
| 677 | rb_define_method(struct_module, "read", rb_thrift_struct_read, 1); |
Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 678 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 679 | thrift_union_class = rb_const_get(thrift_module, rb_intern("Union")); |
| 680 | |
| 681 | rb_define_method(thrift_union_class, "write", rb_thrift_union_write, 1); |
| 682 | rb_define_method(thrift_union_class, "read", rb_thrift_union_read, 1); |
| 683 | |
| 684 | setfield_id = rb_intern("@setfield"); |
| 685 | setvalue_id = rb_intern("@value"); |
| 686 | |
| 687 | to_s_method_id = rb_intern("to_s"); |
| 688 | name_to_id_method_id = rb_intern("name_to_id"); |
Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 689 | sorted_field_ids_method_id = rb_intern("sorted_field_ids"); |
Bryan Duxbury | 65073e5 | 2010-02-23 15:46:46 +0000 | [diff] [blame] | 690 | } |