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