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