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