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