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